Skip to content

Reduce lock scope in StringHelper#randomId()#15631

Closed
easyice wants to merge 3 commits intoapache:mainfrom
easyice:reduce-lock-granularity
Closed

Reduce lock scope in StringHelper#randomId()#15631
easyice wants to merge 3 commits intoapache:mainfrom
easyice:reduce-lock-granularity

Conversation

@easyice
Copy link
Copy Markdown
Contributor

@easyice easyice commented Jan 29, 2026

This change moves BigInteger#toByteArray() outside the synchronized block, since it allocates and copies data. this method does not seem to be on a hot path, so the performance impact may be minimal.

Comment thread lucene/core/src/java/org/apache/lucene/util/StringHelper.java
@github-actions github-actions Bot added this to the 10.4.0 milestone Feb 1, 2026
@benwtrent benwtrent modified the milestones: 10.4.0, 10.5.0 Feb 6, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!

@github-actions github-actions Bot added the Stale label Feb 21, 2026
@msfroh
Copy link
Copy Markdown
Contributor

msfroh commented Feb 23, 2026

@easyice -- I don't know the Java lock semantics well enough to know which is more expensive, acquiring a mutex or using an AtomicReference. I get the impression that if you have contention, the mutex will do better (since the atomic ref may need to retry the update), but if you usually don't have contention, the atomic ref will be better.

I was able to get rid of the lock altogether (replacing it with optimistic locking from getAndUpdate) here: a946e65

I honestly don't know if that's any better though. As you said in the description, the impact may be minimal anyway.

@github-actions github-actions Bot removed the Stale label Feb 24, 2026
@easyice
Copy link
Copy Markdown
Contributor Author

easyice commented Mar 5, 2026

@msfroh This makes sense to me. My understanding is that CAS only becomes less efficient than locks under very high retry contention. For the randomId method, it should be a good fit, and it also makes the code more readable.

Could you open a new PR for this, or push the change here?

@msfroh
Copy link
Copy Markdown
Contributor

msfroh commented Mar 10, 2026

Sure thing, @easyice! I've opened a new PR here: #15805

@easyice easyice closed this Mar 12, 2026
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.

4 participants