Repository navigation
refactor: look enums up with a switch, not values() - #546
Merged
Merged
Conversation
values() allocates a fresh array on every call (64 B for PType) and the JIT does not remove it: in isolation a values()[i] lookup is ~4x slower than a switch when the ordinals repeat, as in real files. PType.fromOrdinal, TimeUnit.fromTag, ZonedStatsSchema.Stat, DictLayoutDecoder and VariantEncodingDecoder now switch; proto-gen emits a switch for the generated ProtoPType/ProtoNullValue.fromValue instead of a linear scan over values(). VortexWriter and cli FileFormat iterate all constants, so they take the array once. No end-to-end effect: on a 300-column, 16-chunk file open and scan allocate and run the same before and after (six interleaved rounds), because these lookups run only a few times per column or segment. Round-trip tests pin each switch to its declaration order, and CLAUDE.md records the rule. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
values()allocates a fresh array on every call (64 B forPType), and the JIT does not remove it. Lookups by ordinal, tag or wire number now use aswitch; places that iterate every constant take the array once.PType.fromOrdinal,TimeUnit.fromTag,ZonedStatsSchema.Stat.fromOrdinal,DictLayoutDecoder,VariantEncodingDecoder:switchproto-genemits aswitchfor the generatedProtoPType/ProtoNullValuefromValue(was a linear scan over a freshvalues()); both files regenerated, nothing else changedVortexWriterandcliFileFormat:values()once into a static listMeasurements
In isolation (random ordinals, 2 forks):
values()[i]325 ops/µs and 64 B/call,switch535 ops/µs and 0 B/call. With repeating ordinals, as in real files:values()[i]336,switch1,431 ops/µs. A cached array is 1,590; theswitchloses to it only through jump mispredicts on random input.No end-to-end effect: a 300-column, 16-chunk file opens and scans with the same allocation and throughput before and after (six interleaved rounds), because these lookups run a few times per column or segment. So this is a cleanup and a rule, not a speedup, and there is no CHANGELOG entry.
Test plan
./mvnw verify(all modules, 2888 integration tests, javadoc)Stat,ProtoPType,ProtoNullValue;PTypeandTimeUnitalready had them)🤖 Generated with Claude Code