Skip to content

Conversation

anuragdy
Copy link
Contributor

@anuragdy anuragdy commented Sep 25, 2025

This PR optimizes the DefaultThreadContextMap.getCopy() method to address performance issues related to megamorphic calls in the HashMap constructor, providing 30-50% performance improvements for non-empty maps.

The original implementation used new HashMap<>(map) which suffers from megamorphic call performance issues documented in:

The HashMap constructor with a Map parameter requires (3 + 4n) virtual method calls that become megamorphic when used with different map types, leading to 24-136% performance degradation compared to manual iteration.

Closes #3935.

@anuragdy anuragdy marked this pull request as ready for review September 25, 2025 04:59
@vy vy self-assigned this Sep 25, 2025
@vy vy added api Affects the public API performance Issues or PRs that affect performance, throughput, latency, etc. labels Sep 25, 2025
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, @anuragdy, thanks so much for the report and the fix. I see that you provided a benchmark in #3935. Curious: Have you checked the existing log4j-perf-test benchmarks on thread context map? Are you able to reproduce the improvement figures using one of the benchmarks there? If so, which one(s)? If not, would you mind updating existing MDC benchmarks accordingly, please?

@vy vy modified the milestone: 2.26.0 Sep 26, 2025
@vy vy enabled auto-merge (squash) October 10, 2025 08:24
Copy link

github-actions bot commented Oct 10, 2025

Job Requested goals Build Tool Version Build Outcome Build Scan®
build-macos-latest clean install 3.9.8 Build Scan PUBLISHED
build-ubuntu-latest clean install 3.9.8 Build Scan PUBLISHED
build-windows-latest clean install 3.9.8 Build Scan PUBLISHED
Generated by gradle/develocity-actions

@vy
Copy link
Member

vy commented Oct 10, 2025

@anuragdy, build is failing, would you mind fixing it, please? See the Log4j Development Guide for details.

auto-merge was automatically disabled October 10, 2025 09:07

Head branch was pushed to by a user without write access

anuragdy added a commit to anuragdy/logging-log4j2 that referenced this pull request Oct 10, 2025
- Fix incorrect indentation in the for loop that was causing compilation failures
- Remove redundant performance figures comment as intended in the original commit
- Fix duplicate JavaDoc comment that was causing subsequent compilation failures

This resolves the GitHub Actions CI build failures in PR apache#3939.
@vy
Copy link
Member

vy commented Oct 10, 2025

@anuragdy, please sign your commits.

anuragdy added a commit to anuragdy/logging-log4j2 that referenced this pull request Oct 10, 2025
- Fix incorrect indentation in the for loop that was causing compilation failures
- Remove redundant performance figures comment as intended in the original commit
- Fix duplicate JavaDoc comment that was causing subsequent compilation failures

This resolves the GitHub Actions CI build failures in PR apache#3939.
@anuragdy anuragdy force-pushed the 2.x branch 3 times, most recently from c81a658 to 4b981eb Compare October 10, 2025 09:52
@anuragdy anuragdy closed this Oct 10, 2025
@github-project-automation github-project-automation bot moved this from To triage to Done in Log4j bug tracker Oct 10, 2025
@anuragdy anuragdy reopened this Oct 10, 2025
@github-project-automation github-project-automation bot moved this from Done to Waiting for user in Log4j bug tracker Oct 10, 2025
@anuragdy
Copy link
Contributor Author

@anuragdy, build is failing, would you mind fixing it, please? See the Log4j Development Guide for details.

Thanks for the guidelines, updated. The build workflow is succeeding on the fork now.
https://github.com/anuragdy/logging-log4j2/actions/runs/18404707294/job/52441832298

@anuragdy, please sign your commits.

Updated the commits.

@vy vy enabled auto-merge (squash) October 10, 2025 13:20
@vy vy merged commit e9556bb into apache:2.x Oct 10, 2025
7 checks passed
@github-project-automation github-project-automation bot moved this from Waiting for user to Done in Log4j bug tracker Oct 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Affects the public API performance Issues or PRs that affect performance, throughput, latency, etc.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Performance improvement to DefaultThreadContextMap

2 participants