Skip to content

Avoid allocations when comparing componentized DotNames - #738

Merged
Ladicek merged 1 commit into
smallrye:mainfrom
gsmet:optimi
Nov 9, 2026
Merged

Ladicek merged 1 commit into
smallrye:mainfrom
gsmet:optimi

Conversation

@gsmet

@gsmet gsmet commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

This was detected when benchmarking the Quarkus test suite for the Hibernate ORM extension. An inefficiency in the HV extension code path was making this a hotpath.

While I will work on addressing the HV issue. I thought it was worth reporting this issue here as it could affect other extensions.

Knowing you, you will probably come up with a completely different implementation :). But at least you have a benchmark and an idea of the problem :).

Full test suite passes.


DotName.compareTo() for two componentized names used to flatten both names into freshly allocated arrays of components before walking them in parallel. This is a hot path: the indexer sorts the annotations of every class, method and field by name, and frameworks then look up annotations by binary search over those sorted arrays, calling compareTo() several times per lookup. Two array allocations plus two prefix-chain walks per comparison add up to a lot of garbage on a large index.

Make the general case allocation-free. Instead of flattening into arrays, compute the depth of each name once and fetch the component at a given index by walking the (short) prefix chain on demand. The character-by-character comparison algorithm is unchanged, only the way components are obtained differs. The null sentinel that terminated the flattened arrays is now returned by componentAt() when the index is past the last component.


Results of the benchmark:

Benchmark Before (ops/s) After (ops/s) Change B/op before B/op after
AnnotationLookupBenchmark.lookupMissingSimpleNames (25 lookups) 482 763 +58% 5,536,494 273,135
AnnotationLookupBenchmark.lookupMissingComponentizedNames (25 lookups) 457 871 +90% 6,690,944 819,690
AnnotationLookupBenchmark.lookupPresentSimpleName 19,858 27,129 +37% 65,538 0
AnnotationLookupBenchmark.lookupPresentComponentizedName 16,558 21,848 +32% 103,769 0
AnnotationLookupBenchmark.sortIndexerNames (6 annotations) 7,472 21,526 +188% 442,390 40,961
DotNameCompareToBenchmark.componentizedComponentizedEqual 4,933 66,288 +1244% 524,347 0
DotNameCompareToBenchmark.componentizedComponentizedNotEqual 5,650 8,387 +48% 589,878 1
DotNameCompareToBenchmark.simpleComponentizedEqual 3,542 4,764 +34% 458,817 1
DotNameCompareToBenchmark.simpleComponentizedNotEqual 4,126 5,884 +43% 524,354 1
DotNameCompareToBenchmark.simpleSimpleEqual 65,405 66,469 +2% 0 0
DotNameCompareToBenchmark.simpleSimpleNotEqual 28,428 27,702 -3% 0 0

The reduction of allocations is what I was after. CPU seems to be relatively fine. Machine was mostly idle but not fully.

@Ladicek

Ladicek commented Nov 9, 2026

Copy link
Copy Markdown
Member

I wouldn't expect this to be faster than my version, but measures trump expectations any day (and night). The benchmark is good, let me rerun it locally just to confirm.

@Ladicek
Ladicek marked this pull request as ready for review November 9, 2026 14:57
DotName.compareTo() for two componentized names used to flatten both
names into freshly allocated arrays of components before walking them in
parallel. This is a hot path: the indexer sorts the annotations of every
class, method and field by name, and frameworks then look up annotations
by binary search over those sorted arrays, calling compareTo() several
times per lookup. Two array allocations plus two prefix-chain walks per
comparison add up to a lot of garbage on a large index.

Make the general case allocation-free. Instead of flattening into
arrays, compute the depth of each name once and fetch the component at
a given index by walking the (short) prefix chain on demand. The
character-by-character comparison algorithm is unchanged, only the way
components are obtained differs. The `null` sentinel that terminated
the flattened arrays is now returned by componentAt() when the index
is past the last component.

Also add a JMH benchmark simulating the typical framework pattern: look
up a couple dozen well-known annotation names, created with
DotName.createSimple() as framework constants usually are, as well as
componentized ones, on a method carrying a few indexer-created
annotations, and sort indexer-created annotations as the indexer does.

Co-Authored-By: Ladislav Thon <[email protected]>
@Ladicek

Ladicek commented Nov 9, 2026

Copy link
Copy Markdown
Member

OK, here are my results:

Benchmark this PR (ops/s) main (ops/s) Change
AnnotationLookupBenchmark.lookupMissingComponentizedNames 923.414 ± 9.925 531.930 ± 10.930 +73.6%
AnnotationLookupBenchmark.lookupMissingSimpleNames 708.488 ± 9.267 561.565 ± 2.279 +26.2%
AnnotationLookupBenchmark.lookupPresentComponentizedName 24643.850 ± 44.506 18713.936 ± 940.833 +31.7%
AnnotationLookupBenchmark.lookupPresentSimpleName 31587.645 ± 275.373 25697.879 ± 181.974 +22.9%
AnnotationLookupBenchmark.sortIndexerNames 19445.541 ± 430.711 8495.426 ± 222.206 +128.9%
DotNameCompareToBenchmark.componentizedComponentizedEqual 54230.140 ± 222.759 5149.783 ± 16.281 +953.0%
DotNameCompareToBenchmark.componentizedComponentizedNotEqual 8333.205 ± 144.874 5909.152 ± 60.647 +41.0%
DotNameCompareToBenchmark.simpleComponentizedEqual 5121.478 ± 41.955 3875.089 ± 33.631 +32.2%
DotNameCompareToBenchmark.simpleComponentizedNotEqual 6231.267 ± 130.147 4634.912 ± 4.769 +34.4%
DotNameCompareToBenchmark.simpleSimpleEqual 54220.658 ± 250.225 53066.469 ± 527.457 +2.2%
DotNameCompareToBenchmark.simpleSimpleNotEqual 23315.681 ± 311.986 22738.710 ± 340.768 +2.5%

I didn't measure allocations, but this is a clear improvement. Good work, thank you!

@Ladicek Ladicek added this to the 3.6.1 milestone Nov 9, 2026
@Ladicek
Ladicek merged commit ee08b2a into smallrye:main Nov 9, 2026
43 checks passed
@gsmet

gsmet commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Let's be honest: the implementation was mostly Claude :).

But it did take some work to identify all these areas of improvements and see which were promising enough.

@Ladicek

Ladicek commented Nov 9, 2026

Copy link
Copy Markdown
Member

I'll release right away.

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