Skip to content

core: improve ReflectionLongAdderCounter performance#12542

Closed
shivaspeaks wants to merge 1 commit intogrpc:masterfrom
shivaspeaks:performance-penalty
Closed

core: improve ReflectionLongAdderCounter performance#12542
shivaspeaks wants to merge 1 commit intogrpc:masterfrom
shivaspeaks:performance-penalty

Conversation

@shivaspeaks
Copy link
Copy Markdown
Member

Avoid reflective access check in ReflectionLongAdderCounter.

The ReflectionLongAdderCounter uses reflection to invoke the add and sum methods of java.util.concurrent.atomic.LongAdder.

Previously, setAccessible(true) was not called on the reflected Method objects. This resulted in a security access check being performed on every invocation, causing a performance penalty.

This change calls setAccessible(true) on the Method objects after they are retrieved, which bypasses the access check and improves performance.

Fixes #12541

cc: @qsLI

@panchenko
Copy link
Copy Markdown
Contributor

It should not use reflection at runtime at all, with code structure like in #11977

@ejona86
Copy link
Copy Markdown
Member

ejona86 commented Dec 1, 2025

setAccessible(true) has historically been a code smell. I've not seen a time I thought it was appropriate to use. After running a benchmark for #12541, this doesn't seem to be a time to use it either.

I don't see a need to avoid reflection here, since the API is provided by the JDK there's no R8 -keep issue. Although maybe desugaring could make it useful to avoid the reflection. In any case, there's no evidence it is a problem right now.

@panchenko
Copy link
Copy Markdown
Contributor

there's no evidence it is a problem right now

We can agree on that, however this code can be simplified by removing reflection.

@kannanjgithub
Copy link
Copy Markdown
Contributor

The reflection logic has been in vogue since 2017. In general we don't want to make changes unless there is a problem recognized with the code, which in this case doesn't exist.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReflectionLongAdderCounter method access check performance penalty

4 participants