Could round(n) and BigDecimal(bd, digits) return self when nothing changes? (1 → 0 objects, ~2.5x on round)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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 :)
- Ngôn ngữ chính
- C
- Star
- 152
- Fork
- 99
- Merge trung bình
- 12 giờ 41 phút
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ruby/bigdecimal
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
ruby/bigdecimal#506 · 4 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
ruby/bigdecimal#491 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
ruby/bigdecimal#444 · 1 bình luận ·
-
BigDecimal#whole?Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
ruby/bigdecimal#291 · 6 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
ruby/bigdecimal#268 · 1 bình luận · 1 reaction ·
Tất cả issue của ruby/bigdecimal
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
FujiNetWIFI/fujinet-firmware#2261 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area/infrared type/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
DarkFlippers/unleashed-firmware#1201 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
pocoproject/poco#5626 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 78/100
VMAFx/vmafx#3260 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
FujiNetWIFI/fujinet-lib#272 ·
Maintainer thường phản hồi trong vòng 1 ngày