Skip to content

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
Member

@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! 😍

private

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

@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?

For example if I called this in my code, wouldn’t I get unexpected results?

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(250) # => 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