Skip to content

Fix dump/load bigdecimal with few or large precs#362

Merged
tompng merged 1 commit intoruby:masterfrom
tompng:dump_load_allocation_fix
Jul 10, 2025
Merged

Fix dump/load bigdecimal with few or large precs#362
tompng merged 1 commit intoruby:masterfrom
tompng:dump_load_allocation_fix

Conversation

@tompng
Copy link
Member

@tompng tompng commented Jul 6, 2025

BigDecimal._load should not trust MaxPrec part of the dumped string.

Fixes this bug.

irb(main):001> x = BigDecimal(BigDecimal._load("30:"+'1'*100), 0)
=> 0.11111111111111111111586024528000051042000000087e100
irb(main):002> x
=> 0.111111111111111111100000000500000000042949672954294967295429496729542949672950000000050000000004294967295e100
irb(main):003> BigDecimal(BigDecimal._load("9999999999:"+'1'*100), 0)
# (slow, takes about a second)

Now that BigDecimal._load ignores precision part, there is no need to expose internal MaxPrec to the dumped string. Using Prec is better.

# Before
BigDecimal(0)._dump #=> "9:0.0"
BigDecimal('0')._dump #=> "18:0.0"
some_zero._dump #=> "123456789:0.0"

# After
zero._dump #=> always "9:0.0"

Dumped string should include Prec instead of MaxPrec.
Don't trust MaxPrec of the dumped string.
@tompng tompng force-pushed the dump_load_allocation_fix branch from 3b3ae41 to 8112094 Compare July 10, 2025 14:14
@tompng tompng merged commit 3005344 into ruby:master Jul 10, 2025
123 of 124 checks passed
@tompng tompng deleted the dump_load_allocation_fix branch July 10, 2025 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant