Repository navigation
Fix slow and misaligned Bignum loading in Gem::SafeMarshal - #10057
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adding a fresh power of two per byte made loading a Bignum grow faster than quadratically with its size, so a 40000-byte one took about 8 seconds. Reading the little-endian bytes as one hex string takes 0.3 ms for the same value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Marshal counts every Bignum as an object, so an object link written after one pointed one slot off. `[2**64, s, "t", s]` loaded as `[2**64, "s", "t", "t"]`, and `[2**64, s, s]` raised IndexError. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`String#to_i(16)` takes quadratic time on JRuby and TruffleRuby, where a 100000-byte Bignum took about 1.5 seconds and failed `test_load_bignum_in_linear_time`. Converting halves of at most 4096 bytes and joining them with shifts takes 0.05 seconds there and keeps CRuby as fast. Co-Authored-By: Claude Opus 5.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.
Gem::SafeMarshal.safe_loadrebuilt a Bignum by adding a fresh power of two for every byte of its data, so loading time grew faster than quadratically with its size. A 40000-byte Bignum took about 8 seconds, whileMarshal.loadreturns it at once. I now read the little-endian bytes as one hex string withto_i(16), which takes 0.3 ms for the same value, andassert_linear_performancekeeps it that way.While there, I found that a Bignum was never added to the object table, although
Marshalcounts it as an object. Any object link after a Bignum pointed one slot off, so[2**64, s, "t", s]loaded as[2**64, "s", "t", "t"], and[2**64, s, s]raisedIndexError.The other new tests compare long positive and negative Bignums with
Marshal.loadand pin the existingFormatErrorfor an unknown sign.Generated with Claude Code