⚡️ Speed up method BytesValue.toString by 109%#24
Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Open
⚡️ Speed up method BytesValue.toString by 109%#24codeflash-ai[bot] wants to merge 1 commit intomasterfrom
BytesValue.toString by 109%#24codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Conversation
The optimized code achieves a **108% speedup** (from 35.5µs to 17.0µs) by replacing the delegated `Buffer.bytesToHexString()` call with an inline hex conversion implementation that minimizes object allocations and method call overhead. **Key Performance Improvements:** 1. **Direct char array allocation**: The optimized version pre-allocates exactly `bytes.length * 2` characters upfront (`char[] out = new char[len << 1]`), avoiding StringBuilder's internal buffer resizing and copying. 2. **Lookup table for hex digits**: Using a static `char[] HEX` array to map nibbles (0-15) to hex characters eliminates conditional branches and character arithmetic that would be present in methods like `Character.forDigit()`. 3. **Eliminated method call overhead**: The original code delegates to `Buffer.bytesToHexString()`, incurring method invocation cost plus whatever allocations/operations that method performs internally. The optimized version performs the conversion inline. 4. **Bitwise operations**: Using bit shifts (`>>> 4`) and masks (`& 0x0F`) to extract high/low nibbles is faster than division/modulo operations. 5. **Single String construction**: The final `new String(out)` creates the string from the pre-built char array in one operation, avoiding intermediate string concatenations or StringBuilder.toString() overhead. **Test Case Performance:** - The optimization particularly excels with the **large array test** (100k bytes), where the reduction in allocations and simpler per-byte operations compounds significantly - For **typical small inputs** (3-4 bytes), the speedup still applies due to avoided method call and simplified logic - **Empty array** handling is optimized with an early return - **Null handling** preserves compatibility by delegating to the original method only for this edge case This optimization is especially valuable if `toString()` is called frequently on BytesValue objects (e.g., in logging, debugging, or serialization paths), as the 2x runtime improvement directly translates to reduced CPU usage and better throughput in those scenarios.
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.
📄 109% (1.09x) speedup for
BytesValue.toStringinclient/src/com/aerospike/client/Value.java⏱️ Runtime :
35.5 microseconds→17.0 microseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 108% speedup (from 35.5µs to 17.0µs) by replacing the delegated
Buffer.bytesToHexString()call with an inline hex conversion implementation that minimizes object allocations and method call overhead.Key Performance Improvements:
Direct char array allocation: The optimized version pre-allocates exactly
bytes.length * 2characters upfront (char[] out = new char[len << 1]), avoiding StringBuilder's internal buffer resizing and copying.Lookup table for hex digits: Using a static
char[] HEXarray to map nibbles (0-15) to hex characters eliminates conditional branches and character arithmetic that would be present in methods likeCharacter.forDigit().Eliminated method call overhead: The original code delegates to
Buffer.bytesToHexString(), incurring method invocation cost plus whatever allocations/operations that method performs internally. The optimized version performs the conversion inline.Bitwise operations: Using bit shifts (
>>> 4) and masks (& 0x0F) to extract high/low nibbles is faster than division/modulo operations.Single String construction: The final
new String(out)creates the string from the pre-built char array in one operation, avoiding intermediate string concatenations or StringBuilder.toString() overhead.Test Case Performance:
This optimization is especially valuable if
toString()is called frequently on BytesValue objects (e.g., in logging, debugging, or serialization paths), as the 2x runtime improvement directly translates to reduced CPU usage and better throughput in those scenarios.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-BytesValue.toString-ml80cwdhand push.