Conversation
value_to_decimal now checks the most common input types first (BigDecimal, Integer, String) and returns early from branches that cannot produce a negative zero, skipping the sign check there. value_to_currency handles all String sub-cases (empty, XXX, ISO code) inside a single String branch instead of running multiple case comparisons per call, and the default-currency path no longer recurses: Config#default_currency= already coerces to a Currency at set time. Benchmark (mixed-input Money.new): 787.6 -> 704.8 ns/op (-10.5%). Allocations unchanged (this is pure dispatch-order work).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Reorders the hot type-dispatch in
Money::Helpersand removes redundant work per call. No behavior change.value_to_decimalchecks the most common input types first (BigDecimal, Integer, String) and returns early from branches that cannot produce a negative zero, skipping thesigncheck there. Previously every call ran through awhen nil, 0, ''triple-comparison and an unconditional sign check.value_to_currencyhandles all String sub-cases (empty /XXX/ ISO code) inside a singleStringbranch instead of running up to fourcasecomparisons per call.value_to_currency:Config#default_currency=already coerces to aCurrency/NullCurrencyat set time, so the stored value can be returned directly.Float::DIG/BigDecimal::SIGN_NEGATIVE_ZEROhoisted into private constants.Performance
Measured on Ruby 4.0.1 (arm64-darwin), median of 7 trials, mixed-input
Money.newworkload:Part of a series of independent
Money.newoptimizations (currency cache, construction path,Config.current); combined they reach ~-38% wall time and -45% allocations on this workload.Correctness
Benchmark script (save as
bench.rb, runruby -Ilib bench.rbon each ref)