Skip to content

Conversation

@bigbear07
Copy link

@bigbear07 bigbear07 commented Sep 17, 2025

PR Title:
Optimize TransmittableThreadLocal.get() with Conditional Initialization via initialValueSupplierForQuickGet


PR Description:

Overview

This PR introduces a performance optimization for TransmittableThreadLocal by adding a new field initialValueSupplierForQuickGet and corresponding constructors, enabling lazy initialization of addThisToHolder() only when necessary.

Key Changes

  1. New Field & Constructors:
    • Added initialValueSupplierForQuickGet to conditionally trigger optimization logic.
    • Introduced overloaded constructors to support optional initialization with the supplier.
  2. Optimized get() Workflow:
    • Optimized Path (when initialValueSupplierForQuickGet != null):
      Calls addThisToHolder() only during value initialization (i.e., when value == null).
    • Legacy Path (when initialValueSupplierForQuickGet == null):
      Maintains original behavior for backward compatibility.

Performance Impact

Metric Before (Baseline) After (Optimized) Improvement
TPS 2.9B 8.3B 2.86x
1 2

Problem Solved:
The original implementation invoked addThisToHolder() on every get() call, incurring unnecessary overhead. The optimization reduces redundant synchronization by limiting addThisToHolder() to initialization phases.

Compatibility & Safety

  • Backward-Compatible: Existing usages remain unaffected when initialValueSupplierForQuickGet is not provided.
  • Thread Safety: Retains thread-safe guarantees via atomic initialization checks.

Verification

  • Added unit tests for both optimized and legacy paths.
  • Validated throughput using JMH benchmarks under high-concurrency workloads.
  • Confirmed no regressions in existing integration tests.

Why This Matters

This change significantly improves performance for frameworks relying on frequent TransmittableThreadLocal access (e.g., thread-pool wrappers, context propagation), while preserving flexibility for legacy use cases.


Review Focus Areas:

  • Conditional logic in get() method (optimized vs. legacy paths)
  • Constructor overloads and field initialization
  • Benchmark validity and thread-safety guarantees

…hreadLocal. Purpose: To reduce the overhead in get() method by calling addThisToHolder() only when value initialization is actually needed, rather than on every get() call
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


大熊 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


大熊 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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