Skip to content

Simplify BUFFER_COUNT in ConcurrentLruCache to a constant#36872

Merged
bclozel merged 1 commit into
spring-projects:mainfrom
s-chan-o:fix/36780-concurrent-lru-cache-buffer-count
Jun 4, 2026
Merged

Simplify BUFFER_COUNT in ConcurrentLruCache to a constant#36872
bclozel merged 1 commit into
spring-projects:mainfrom
s-chan-o:fix/36780-concurrent-lru-cache-buffer-count

Conversation

@s-chan-o

@s-chan-o s-chan-o commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Problem

The detectNumberOfBuffers() method in ConcurrentLruCache.ReadOperations attempts to scale the read buffer count based on the number of available processors:

private static int detectNumberOfBuffers() {
    int availableProcessors = Runtime.getRuntime().availableProcessors();
    int nextPowerOfTwo = 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(availableProcessors - 1));
    return Math.min(4, nextPowerOfTwo);
}

However, the use of Math.min(4, nextPowerOfTwo) caps the result at 4 for any system with 3 or more processors. For systems with 5+ processors, nextPowerOfTwo would be 8 or more, but the Math.min always returns 4. The CPU-detection logic adds complexity without any actual effect for the vast majority of deployment environments.

As noted in gh-36780, the value is "basically always just 4".

Fix

Simplify BUFFER_COUNT to a plain constant 4, removing the detectNumberOfBuffers() method entirely. This is consistent with the feedback from gh-29520 that informed the original cap at 4.

Closes gh-36780

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 4, 2026
@bclozel

bclozel commented Jun 4, 2026

Copy link
Copy Markdown
Member

@s-chan-o please sign off your commit, we cannot accept this contribution without it. Thanks

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Jun 4, 2026
@s-chan-o s-chan-o force-pushed the fix/36780-concurrent-lru-cache-buffer-count branch from 32f294a to 592ab02 Compare June 4, 2026 07:00
@s-chan-o

s-chan-o commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

@bclozel Added Signed-off-by in 592ab02. Thanks!

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 4, 2026
@bclozel bclozel self-assigned this Jun 4, 2026
@bclozel bclozel added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Jun 4, 2026
@bclozel bclozel added this to the 7.1.0-M1 milestone Jun 4, 2026
The detectNumberOfBuffers() method attempted to scale the read
buffer count based on the number of available processors, but used
Math.min(4, nextPowerOfTwo) which effectively caps the result at 4
regardless of CPU count. For systems with fewer than 3 processors,
the buffer count would be reduced below 4, but this edge case adds
complexity without measurable benefit.

Simplify BUFFER_COUNT to a constant value of 4, removing the
unnecessary CPU-detection logic.

Closes spring-projectsgh-36872

Signed-off-by: seungchan <s24041@gsm.hs.kr>
@bclozel bclozel force-pushed the fix/36780-concurrent-lru-cache-buffer-count branch from 592ab02 to 4c6194a Compare June 4, 2026 07:37
@bclozel bclozel closed this in 4c6194a Jun 4, 2026
@bclozel bclozel merged commit 4c6194a into spring-projects:main Jun 4, 2026
1 check passed
@bclozel

bclozel commented Jun 4, 2026

Copy link
Copy Markdown
Member

Thanks for your contribution @s-chan-o !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suspicious BUFFER_COUNT value in ConcurrentLruCache

3 participants