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

Performance optimization in CloseableThreadContext.closeMap() (#2292) #2296

Merged
merged 5 commits into from Feb 19, 2024

Conversation

jengebr
Copy link
Contributor

@jengebr jengebr commented Feb 16, 2024

This substantially improves the performance of CloseableThreadContext.closeMap(), reducing the aggregate create/close cost by 40% or 20%, depending on the ThreadContext implementation in use. Further performance explanation is on the related issue,
#2292

Checklist

  • Base your changes on 2.x branch if you are targeting Log4j 2; use main otherwise
  • ./mvnw verify succeeds (if it fails due to code formatting issues reported by Spotless, simply run ./mvnw spotless:apply and retry)
  • Non-trivial changes contain an entry file in the src/changelog/.2.x.x directory
  • Tests for the changes are provided
  • Commits are signed (optional, but highly recommended)

Copy link
Member

@vy vy left a comment

Choose a reason for hiding this comment

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

@jengebr, thanks so much! Would you mind adding a changelog entry (i.e., src/changelog/.2.x.x/improve_CloseableThreadContext_closeMap.xml) too, please?

@vy vy added this to the 2.24.0 milestone Feb 19, 2024
@ppkarwasz
Copy link
Contributor

@jengebr,

Thank you for your contribution.

Personally I think there is an inherent problem in the way CloseableThreadContext.Instance works: it stores the difference between the context data available when it was created and the current context data.

Since you pointed out that Map or StringMap (a replacement of Java Map with garbage-free iteration) allocation is a performance bottleneck, maybe we could change the logic of CloseableThreadContext.Instance to:

  • take a snapshot of the current context data in its constructor,
  • restore the snapshot in Instance#close().

This might require API changes, that I am more than willing to accept, but it should give better results: closing Instance would be simple and garbage-free.

Unfortunately there are 3 implementations of ThreadContextMap:

In order to work with all 3, Instance would at least need to store the appropriate native format of the context data, to prevent conversions from Map to StringMap and viceversa.

@jengebr
Copy link
Contributor Author

jengebr commented Feb 19, 2024

@vy thanks for adding the finals! I've added the changelog entry under 2.24.0, which matches the milestone, hopefully that's correct.

@ppkarwasz I like the garbage-free proposal but I don't think I'm up for that - not enough familiarity with the log4j codebase or designs. I am however able to performance test and profile such changes in a real-life, heavy-load environment, and am happy to help in that way.

Copy link
Contributor

@ppkarwasz ppkarwasz left a comment

Choose a reason for hiding this comment

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

LGTM

@jengebr, even if we manage to further optimize the process, your implementation gives us a better alternative to compete with.

@vy vy added the api Affects the public API label Feb 19, 2024
@vy
Copy link
Member

vy commented Feb 19, 2024

@jengebr, could you move the changelog file to src/changelog/.2.x.x/improve_CloseableThreadContext_closeMap.xml, please? (.2.x.x is literally the folder name, not a placeholder. It denotes the changes that will be published with the next release. 2.24.0 folder will be created and populated during a release.)

@jengebr
Copy link
Contributor Author

jengebr commented Feb 19, 2024

@vy done, I think - and sorry, my IDE was hiding that folder and I misunderstood the intent. I kept the change you made (thanks for that!).

@vy vy merged commit b4ef5dd into apache:2.x Feb 19, 2024
9 checks passed
@ppkarwasz ppkarwasz removed this from the 2.24.0 milestone Feb 22, 2024
@jengebr
Copy link
Contributor Author

jengebr commented Feb 23, 2024

@ppkarwasz - fyi, our conversation above sparked an idea for improving DefaultThreadContextMap. I opened #2319 to discuss further, including an implementation and benchmark.

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

Successfully merging this pull request may close these issues.

None yet

3 participants