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

Enhance performance of AbstractPatternJsonProvider #688

Merged
merged 7 commits into from
Nov 3, 2021

Conversation

brenuart
Copy link
Collaborator

This PR addresses multiple performance issues of the AbstractJsonPatternParser.

  • PatternLayoutBase creates a new StringBuilder for every event it must serialize causing garbage and unnecessary pressure on the GC. This PR tries to reuse a StringBuilder per thread.
  • Filtering of empty fields is retrieving values multiple times from the event. This may become become expensive when the value is computed by a PatternLayout. This PR uses Jackson's FilteringJsonGenerator instead.
  • Make use of the most appropriate JsonGenerator#write() method instead of using writeObject() for "simple" value types. Using writeObject() delegates to the underlying ObjectCodec which is slower and produces lot more garbage.
  • Refactor the code to make it easier to read and understand.

Benchmarks show that these changes make the AbstractPatternJsonProvider 4 times faster than LLE 6.6 and more importantly, it does not produce garbage anymore.

- PatternLayoutBase creates a new StringBuilder for every event it must serialize causing garbage and  unnecessary pressure on the GC. Try to reuse a StringBuilder per thread.
- Filtering of empty fields is getting values multiple times from the event which becomes expensive when the value is computed by a PatternLayout. Use Jackson's FilteringJsonGenerator instead.
- Make use of the most appropriate JsonGenerator#write() method instead of using writeObject() for "simple" value types. Using writeObject() delegates to the underlying ObjectCode which is slower and produces lot more garbage.
- Refactor the code to make it easier to read and understand.
Copy link
Collaborator

@philsttr philsttr left a comment

Choose a reason for hiding this comment

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

Do the existing unit tests provide sufficient test coverage to ensure these changes did not change the generated JSON?

@brenuart
Copy link
Collaborator Author

brenuart commented Oct 31, 2021

Do the existing unit tests provide sufficient test coverage to ensure these changes did not change the generated JSON?

They seem to have good coverage already... Do you have additional scenarios in mind?

@philsttr
Copy link
Collaborator

They seem to have good coverage already... Do you have additional scenarios in mind?

No. I haven't looked at them. I just wanted to confirm. I trust your judgement.

DelegatingNodeWriter is private and used only by OmitEmptyFieldWriter. Suppress the class and move the delegate field directly into the sub-class.
…s instead of logging an ERROR status

AbstractJsonPatternParser now throws a JsonPatternException in case of invalid pattern. It is up to the AbstractPatternLayoutJsonProvider to catch the exception, log an error status and decide how it should behave.

Invalid PatternLayout formats are now detected and throw a JsonPatternException as well. Detection is done by configuring the PatternLayout instances with a custom Logback context that throws an exception when an ERROR status is logged.

The former implementation used to ignore fields with an invalid pattern layout while keeping the other. This results in a partial JSON output with only the valid fields. This behaviour is now changed and the provider reverts to producing nothing in case of bad configuration.

These changes address issue #686.
@brenuart brenuart linked an issue Nov 3, 2021 that may be closed by this pull request
… patterns instead of logging an ERROR status"

This reverts commit 414f482.
This changes will be committed in a separate branch.
@brenuart
Copy link
Collaborator Author

brenuart commented Nov 3, 2021

I'm done with this PR.
@philsttr Are you ok with it?

@brenuart brenuart merged commit 9cd9d4c into main Nov 3, 2021
@brenuart brenuart deleted the patternlayout-refactoring branch November 3, 2021 21:03
@philsttr philsttr added this to the 7.0 milestone Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants