Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add position formatter #90

Merged
merged 2 commits into from
Feb 13, 2020
Merged

Conversation

iRevive
Copy link
Contributor

@iRevive iRevive commented Feb 11, 2020

This PR brings an additional way to configure the format of the position. (scope of #80)

Benchmarks

Master

Benchmark Mode Cnt Score Error Units
FormatterBenchmarks.defaultColorful avgt 25 8130.564 ± 7511.598 ns/op
FormatterBenchmarks.defaultFormatter avgt 25 3827.164 ± 266.007 ns/op
FormatterBenchmarks.defaultFormatterNoCtx avgt 25 3673.840 ± 154.153 ns/op
FormatterBenchmarks.defaultFormatterNoCtxThrowable avgt 25 537.797 ± 13.598 ns/op
FormatterBenchmarks.depthFormatter avgt 25 1106.039 ± 107.574 ns/op
FormatterBenchmarks.depthIndentFormatter avgt 25 1197.449 ± 88.011 ns/op
FormatterBenchmarks.jsonFormatter avgt 25 13811.366 ± 603.861 ns/op

Position format

Benchmark Mode Cnt Score Error Units
FormatterBenchmarks.abbreviatedPositionFormatter avgt 25 10586.138 ± 4255.005 ns/op
FormatterBenchmarks.defaultColorful avgt 25 7760.048 ± 3795.697 ns/op
FormatterBenchmarks.defaultFormatter avgt 25 3609.332 ± 510.637 ns/op
FormatterBenchmarks.defaultFormatterNoCtx avgt 25 3250.578 ± 94.497 ns/op
FormatterBenchmarks.defaultFormatterNoCtxThrowable avgt 25 508.383 ± 34.270 ns/op
FormatterBenchmarks.depthFormatter avgt 25 986.337 ± 32.192 ns/op
FormatterBenchmarks.depthIndentFormatter avgt 25 1038.359 ± 25.021 ns/op
FormatterBenchmarks.jsonFormatter avgt 25 13990.842 ± 1054.441 ns/op

Copy link
Contributor

@sergeykolbasov sergeykolbasov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @iRevive

I like how it looks in general! Have only a few notes on possible performance optimizations and API compatibility

* `ThrowableFormat.Depth.Fixed` - prints N elements of a stack trace
* `ThrowableFormat.Indent.NoIndent` - prints a stack trace without indentation
* `ThrowableFormat.Indent.Fixed` - prints a stack trace prepending every line with N spaces
* [[ThrowableFormat.Depth.Full]] - prints all elements of a stack trace
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[[]] brackets break the sbt doc (and therefore the release) unless it contains the absolute path with package, such as
io.odin.formatter.options.ThrowableFormat.Depth.Full

You can try it locally using sbt doc

input match {
case Nil => builder
case head :: Nil => builder.append(head)
case head :: tail => loop(tail, builder.append(head.headOption.getOrElse('?')).append('.'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can save an allocation of Option object by using a simple condition if (head.isEmpty) "?" else head.head.

}
}

loop(enclosure.split('.').toList, new StringBuilder).toString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid bouncing over Char by using string right away, as under the hood it still allocates yet another string object.

Also, I'd stay with Array and just check its size in loop to avoid List conversion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it would make sense to move "." to some constant private val dot inside of Formatter object to avoid spawning those String instances on each call.

* @param colorful use different color for thread name, level, position and throwable
*/
def create(throwableFormat: ThrowableFormat, colorful: Boolean): Formatter = {
def create(throwableFormat: ThrowableFormat, positionFormat: PositionFormat, colorful: Boolean): Formatter = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops, you're breaking already released API here. Either create overloaded method create that takes three arguments, or put it in the end of arguments list and give it a default value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added overloaded methods to Formatter and circe.Formatter.

@iRevive
Copy link
Contributor Author

iRevive commented Feb 13, 2020

Looks much better, thanks for the suggestion!

Benchmark Mode Cnt Score Error Units
FormatterBenchmarks.abbreviatedPositionFormatter avgt 25 4186.801 ± 965.447 ns/op
FormatterBenchmarks.defaultColorful avgt 25 3817.212 ± 333.669 ns/op
FormatterBenchmarks.defaultFormatter avgt 25 3835.369 ± 267.322 ns/op
FormatterBenchmarks.defaultFormatterNoCtx avgt 25 3712.681 ± 261.101 ns/op
FormatterBenchmarks.defaultFormatterNoCtxThrowable avgt 25 546.015 ± 31.465 ns/op
FormatterBenchmarks.depthFormatter avgt 25 998.626 ± 93.177 ns/op
FormatterBenchmarks.depthIndentFormatter avgt 25 1020.804 ± 9.673 ns/op
FormatterBenchmarks.jsonFormatter avgt 25 16102.712 ± 3180.347 ns/op

}
}

loop(enclosure.split('.'), new StringBuilder).toString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reuse dot here as well

Copy link
Contributor Author

@iRevive iRevive Feb 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split treats string argument as a regex. The suitable options are "\\." and '.'.
Should I change it to:

  loop(enclosure.split(packageSeparator), new StringBuilder).toString()
}

private val packageSeparator = "\\."

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, it's a special symbol. Nvm then, it's not worth it

@sergeykolbasov sergeykolbasov merged commit 8fdbfae into valskalla:master Feb 13, 2020
@sergeykolbasov
Copy link
Contributor

Lovely
Thanks @iRevive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants