Could round(n) and BigDecimal(bd, digits) return self when nothing changes? (1 → 0 objects, ~2.5x on round)

オープン
#566 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
c, ruby
領域
backend

調査の方向性

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) and truncate(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) when bd has no more significant digits than digits: 52 to 29 ns, 1 to 0 objects. A longer bd is 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分
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ruby/bigdecimal のほかの issue

ruby/bigdecimal の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。