Repository navigation
THRIFT-6428: Reduce allocations in Ruby memory buffer hex diagnostics - #4016
Merged
Merged
Conversation
Client: rb Co-Authored-By: OpenAI Codex (gpt-6.1-sol) <codex@openai.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.
MemoryBufferTransport#inspect_buffercreates two temporary Strings per byte and retains an Array of hexadecimal tokens before joining them. Large diagnostic dumps consequently allocate substantially more memory than the returned text requires.Reuse frozen byte spellings and append directly to the result String. Preserve the complete lowercase, unpadded dump, spacing, cursor positions, output encoding, and mutability without consuming the transport's contents.
Benchmarks
Compared master
5248fb28acda184580bd490cea8a1bea6933535awith this change using Ruby 4.0.7 on aarch64 Linux. The stdlib harness preserves master's original method forlegacyand calls the proposed public method forinstalled.Benchmark command:
The harness checks 32 B, 4 KiB, and 1 MiB buffers in native and forced-pure modes, with three fresh-process ordinary-GC trials and three separate GC-disabled trials per configuration. Every timed iteration performs identical SHA-256 validation work.
For a 1 MiB buffer cycling through all 256 byte values, with the cursor at zero:
These are medians of three fresh processes. In both modes, separate GC-disabled trials reduced median allocations from 2,097,165 objects to 11 (>99.999%) and the measured object footprint from 94.041 MiB to 4.006 MiB (−95.74%). Complete output matched at 3,080,193 bytes with identical SHA-256 digests.
The lookup table retains a fixed 12,328 bytes at load time on this Ruby. The full returned dump still grows with input size. Peak RSS includes setup and runtime; shared-machine scheduling limits timing precision. Other Ruby versions and JRuby were not measured.
[skip ci]anywhere in the commit message to free up build resources.