Could round(n) and BigDecimal(bd, digits) return self when nothing changes? (1 → 0 objects, ~2.5x on round)
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 35/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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 :)
- Langage dominant
- C
- Étoiles
- 152
- Forks
- 99
- Merge moyen
- 12 h 41 min
- PR mergées (30 j)
- 1
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de ruby/bigdecimal
-
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
ruby/bigdecimal#506 · 4 commentaires ·
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 30/100
ruby/bigdecimal#491 ·
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
ruby/bigdecimal#444 · 1 commentaire ·
-
BigDecimal#whole?Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
ruby/bigdecimal#291 · 6 commentaires ·
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 20/100
ruby/bigdecimal#268 · 1 commentaire · 1 réaction ·
Toutes les issues de ruby/bigdecimal
Issues similaires
-
luaebpf: the by-example page's intro names two outcomes of a lowering and omits the stop with the default verdictPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouvertedocumentation severity: low
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
luainkernel/lunatik#2231 ·
Les mainteneurs répondent en général sous 1 jour
-
aspect ratio 16:10Ouverteenhancement
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Revivedx/SavageWindRecomp#402 ·
Les mainteneurs répondent en général sous 1 jour
-
cleanup help wanted
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
ESPresense/ESPresense#2602 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
apache/arrow-swift#209 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
libretiny-eu/libretiny#435 ·
Les mainteneurs répondent en général sous 4 jours