⚡️ Speed up method LocalRemoteArtifactCache.hashUri by 697%#27
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up method LocalRemoteArtifactCache.hashUri by 697%#27codeflash-ai[bot] wants to merge 1 commit intomainfrom
LocalRemoteArtifactCache.hashUri by 697%#27codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimization replaces two expensive per-call operations: creating a new `MessageDigest.getInstance("SHA-256")` on every invocation (which involves provider lookups and object allocation) and converting bytes to hex via `Integer.toHexString` plus conditional `StringBuilder.append` calls. Instead, a `ThreadLocal<MessageDigest>` reuses one digest per thread (reset before each use), eliminating ~22 µs of instantiation overhead per call, and a pre-allocated `HEX_ARRAY` lookup table converts bytes to hex via direct nibble indexing into a `char[]`, replacing 192 method calls and string allocations with simple array writes. Line profiler shows the digest line dropped from 8.0 ms to 410 µs, and the hex loop overhead nearly vanished, yielding a 696% speedup with no functional regressions across all test cases.
HeshamHM28
approved these changes
Apr 1, 2026
Owner
HeshamHM28
left a comment
There was a problem hiding this comment.
Optimization Verification Report
Method: LocalRemoteArtifactCache.hashUri
Benchmark Result: 70.8% speedup (784ms → 229ms per 500K iterations)
Verification
- Excellent performance improvement confirmed via independent benchmarking
- ThreadLocal MessageDigest caching + char-array hex conversion eliminates:
- Repeated
MessageDigest.getInstance()service lookups Integer.toHexString()+ StringBuilder overhead per byte
- Repeated
- The pre-allocated HEX_ARRAY lookup table is a well-known optimization pattern
Verdict: APPROVED — Significant verified speedup.
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.
📄 697% (6.97x) speedup for
LocalRemoteArtifactCache.hashUriinrewrite-core/src/main/java/org/openrewrite/remote/LocalRemoteArtifactCache.java⏱️ Runtime :
5.71 milliseconds→716 microseconds(best of119runs)📝 Explanation and details
The optimization replaces two expensive per-call operations: creating a new
MessageDigest.getInstance("SHA-256")on every invocation (which involves provider lookups and object allocation) and converting bytes to hex viaInteger.toHexStringplus conditionalStringBuilder.appendcalls. Instead, aThreadLocal<MessageDigest>reuses one digest per thread (reset before each use), eliminating ~22 µs of instantiation overhead per call, and a pre-allocatedHEX_ARRAYlookup table converts bytes to hex via direct nibble indexing into achar[], replacing 192 method calls and string allocations with simple array writes. Line profiler shows the digest line dropped from 8.0 ms to 410 µs, and the hex loop overhead nearly vanished, yielding a 696% speedup with no functional regressions across all test cases.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-LocalRemoteArtifactCache.hashUri-mmnkrljland push.