perf(abi): cache type bit-width parsing in TypeDecoder.getTypeLength#2280
Open
snuderl wants to merge 1 commit intoLFDT-web3j:mainfrom
Open
perf(abi): cache type bit-width parsing in TypeDecoder.getTypeLength#2280snuderl wants to merge 1 commit intoLFDT-web3j:mainfrom
snuderl wants to merge 1 commit intoLFDT-web3j:mainfrom
Conversation
getTypeLength is called on every decodeNumeric invocation and uses regex splitting (type.getSimpleName().split(regex)) to extract the bit width from class names like 'Uint256' or 'Int128'. Since the bit width for a given Class never changes, cache the result in a ConcurrentHashMap. This eliminates repeated regex compilation and string splitting on the hot path. The cache is thread-safe and bounded by the finite number of numeric type classes. Co-Authored-By: Blaz Snuderl <blaz.snuderl@gmail.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
TypeDecoder.getTypeLength(Class<T>)is called on everydecodeNumericinvocation. For each call it builds a regex ("(Uint|Int)"), compiles it viaString.split, splits the class's simple name, and parses an int. The result for a givenClass<?>never changes, so this PR memoizes it in aConcurrentHashMap.Why
String.split(regex)recompiles thePatternon every invocation (it does not match the single-character fast path), which is genuinely expensive on a hot path. CPU profiles of decoders processing many event logs showedPattern.compileandString.splitas a significant chunk of decode time.Benchmark
Same harness as #2279, single-JVM, 200k warmup + 2M measure ops, best of 3, OpenJDK 25 / arm64.
getTypeLength)This patch independently delivers larger numeric-type wins than #2279 (direct constructor), because regex compilation was the dominant cost, not reflection.
Compatibility
Class<?>. The value (bit width) is derived purely from the class identity, so the cache is safe under concurrent access and does not need invalidation.Test plan
./gradlew :abi:testpasses🤖 Generated with Claude Code