Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Fix slow and misaligned Bignum loading in Gem::SafeMarshal - #10057

Merged
hsbt merged 4 commits into
masterfrom
claude/elegant-lewin-84a1e8
Nov 13, 2026
Merged

hsbt merged 4 commits into
masterfrom
claude/elegant-lewin-84a1e8

Conversation

@hsbt

@hsbt hsbt commented Nov 12, 2026

Copy link
Copy Markdown
Member

Gem::SafeMarshal.safe_load rebuilt 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, while Marshal.load returns it at once. I now read the little-endian bytes as one hex string with to_i(16), which takes 0.3 ms for the same value, and assert_linear_performance keeps it that way.

While there, I found that a Bignum was never added to the object table, although Marshal counts 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] raised IndexError.

The other new tests compare long positive and negative Bignums with Marshal.load and pin the existing FormatError for an unknown sign.

Generated with Claude Code

hsbt and others added 4 commits November 12, 2026 17:33
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>
@hsbt
hsbt merged commit 18c2dae into master Nov 13, 2026
143 of 145 checks passed
@hsbt
hsbt deleted the claude/elegant-lewin-84a1e8 branch November 13, 2026 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant