Skip to content

Improve performance of find_by_iso_numeric method #1147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pranavbabu
Copy link
Contributor

ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [arm64-darwin23]
Warming up --------------------------------------
 Before optimization     2.135k i/100ms
  After optimization    55.681k i/100ms
Calculating -------------------------------------
 Before optimization     21.148k (± 0.8%) i/s   (47.29 μs/i) -    106.750k in   5.048173s
  After optimization    549.315k (± 0.6%) i/s    (1.82 μs/i) -      2.784M in   5.068425s

Comparison:
  After optimization:   549314.7 i/s
 Before optimization:    21147.5 i/s - 25.98x  slower

ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [arm64-darwin23]
Warming up --------------------------------------
 Before optimization     2.135k i/100ms
  After optimization    55.681k i/100ms
Calculating -------------------------------------
 Before optimization     21.148k (± 0.8%) i/s   (47.29 μs/i) -    106.750k in   5.048173s
  After optimization    549.315k (± 0.6%) i/s    (1.82 μs/i) -      2.784M in   5.068425s

Comparison:
  After optimization:   549314.7 i/s
 Before optimization:    21147.5 i/s - 25.98x  slower
Copy link
Contributor

@sunny sunny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice performance gains! 😍

@@ -215,6 +215,12 @@ def reset!
end

private

def iso_numeric_index
@iso_numeric_index ||= table.each_with_object({}) do |(id, attrs), index|
Copy link
Contributor

@sunny sunny Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t this memoisation be cleared/updated when registering new currencies?

E.g.:

unless Money::Currency.find_by_iso_numeric(250)
  # ^---- this would fill `@iso_numeric_index` from `@table`

  Money::Currency.register({
    priority:            1,
    iso_code:            "FRF",
    iso_numeric:         "250",
    name:                "French Francs",
    symbol:              "FR",
    subunit:             "Centimes",
    subunit_to_unit:     100,
    decimal_mark:        ",",
    thousands_separator: " "
  })
  # ^--- this would add a new entry to `@table`
end

Money::Currency.find_by_iso_numeric("FRA") # => nil
# ^--- this would use the `@iso_numeric_index` and not `@table`

Also, it should probably be cleared inside the reset! method as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants