Skip to content

perf(once-cell): avoid locking on uncontended initialization - #412

Merged
orthur2 merged 2 commits into
apache:mainfrom
Kaileshwar16:perf/398-once-cell-initialization
Nov 8, 2026
Merged

orthur2 merged 2 commits into
apache:mainfrom
Kaileshwar16:perf/398-once-cell-initialization

Conversation

@Kaileshwar16

Copy link
Copy Markdown
Contributor

Reduce fresh OnceCell initialization cost by acquiring two of three internal semaphore permits. The spare permit allows uncontended release to avoid locking the wait queue while preserving exclusive initialization and existing cancellation handling.

On Linux, ten alternating benchmark samples reduced median fresh initialization time from 352 ms to 236 ms for six million operations (33%). Already-initialized reads and four-waiter performance remained within measurement noise.

Add regression coverage for publication, cancellation, retry, and waiter progress, plus an already-initialized read benchmark.

Fixes #398.

@Kaileshwar16
Kaileshwar16 marked this pull request as draft November 7, 2026 12:40
@Kaileshwar16
Kaileshwar16 marked this pull request as ready for review November 7, 2026 13:11

@tisonkun tisonkun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution! LGTM.

@tisonkun
tisonkun requested a review from orthur2 November 7, 2026 13:45

@orthur2 orthur2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, good work.

@orthur2
orthur2 merged commit c6ec2a4 into apache:main Nov 8, 2026
9 checks passed
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.

Reduce the cost of initializing a fresh OnceCell

3 participants