Could round(n) and BigDecimal(bd, digits) return self when nothing changes? (1 → 0 objects, ~2.5x on round)
还没有人认领这个 Issue。
评估
调研方向
Read the implementations of BigDecimal#round, #floor, #ceil, and #truncate, plus BigDecimal(bd, digits), and the test_BigDecimal_bug7522 test mentioned in the issue. First compare their current behavior with the proposed identity-preserving cases, including the stated rounding modes and edge cases. Done means deciding whether either change is acceptable and, if so, adding coverage for value equivalence and object identity.
由索引模型根据 Issue 内容生成。
描述
Hi, a question before sending a PR, since it touches object identity.
In a money app most round(2) calls receive a value that already has at most two decimals: line totals, prices read from a decimal(10,2) column, and Rails' own apply_scale on every decimal attribute it casts. Each of those allocates a new, equal BigDecimal. On 2,000 real orders from our database, round(n) calls that drop no digit account for about 50k of the 326k BigDecimals allocated. BigDecimal(bd, digits) is similar: it always copies, even though it never rounds a BigDecimal argument today (the /* TODO: rounding */ is still there), and Rails calls it on every decimal attribute read.
I prototyped both returning self when nothing would change:
round(n),floor(n),ceil(n)andtruncate(n)with n >= 1, when no digit is dropped:BigDecimal("129.90").round(2)goes from 168 to 63 ns and 1 to 0 objects. n < 1 keeps returning an Integer as it does now.BigDecimal(bd, digits)whenbdhas no more significant digits thandigits: 52 to 29 ns, 1 to 0 objects. A longerbdis still copied, so the TODO stays open.
Both gave identical results to master on 5M fuzzed cases (every rounding mode and half: option, n from -3 to 40, ±0, NaN, ±Infinity, huge exponents), comparing to_s, sign, precision_scale and _dump. Ruby 4.0.9, macOS arm64.
The catch is that x.round(2).equal?(x) becomes true, as Integer#round already behaves, and test_BigDecimal_bug7522 asserts assert_not_same(bd, BigDecimal(bd, 1)), so that test would have to change for the second one. BigDecimals are frozen, so nothing can be mutated through the shared object. For the Rails case I'm also preparing a fix on the Rails side, which works with every bigdecimal version, but our own round(2) calls would only benefit from a change here.
Would you accept returning self in these cases, for one of them or both, or would you rather keep a fresh object every time?
🤖 Generated with Claude Code, but prompted, read, and verified by @czj, a caring human :)
- 主要语言
- C
- 星标
- 152
- 派生
- 99
- 平均合并
- 12 小时 41 分钟
- 30 天内合并 PR
- 1
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ruby/bigdecimal 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 45/100
ruby/bigdecimal#506 · 4 条评论 ·
-
难度 5/5 一周以上 新手友好度 30/100
ruby/bigdecimal#491 ·
-
难度 5/5 一周以上 新手友好度 35/100
ruby/bigdecimal#444 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 35/100
ruby/bigdecimal#291 · 6 条评论 ·
-
难度 5/5 一周以上 新手友好度 20/100
ruby/bigdecimal#268 · 1 条评论 · 1 个 reaction ·
相似的 Issue
-
难度 2/5 半天 新手友好度 72/100
vercel-labs/scriptc#2910 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
darktable-org/darktable#22939 ·
维护者通常 1 天内回复
-
os: input_password leaves terminal echo disabled when reading fails可能已有人在做 @davlgd 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
armv7l: `close`, `unlink` and `chroot` pass the kernel their argument's address可能已有人在做 @aoinoikaz 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 68/100
colbymchenry/codegraph#3223 ·
维护者通常 1 天内回复