Skip to content
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

Freeze ActiveSupport::Duration#parts hash #41906

Merged
merged 1 commit into from
Apr 13, 2021
Merged

Freeze ActiveSupport::Duration#parts hash #41906

merged 1 commit into from
Apr 13, 2021

Conversation

pixeltrix
Copy link
Contributor

Durations are meant to be value objects and should not be mutated.

By doing this we can cache whether a duration is variable and give the + and - methods on ActiveSupport::Duration a performance boost. It also avoids two array allocations by avoiding the any? and the inline array definition. It's not going to make a big performance difference to apps but it makes the duration_of_variable_length? method 2.5 times faster:

Warming up --------------------------------------
             current   325.663k i/100ms
                 new   828.177k i/100ms
Calculating -------------------------------------
             current      3.274M (± 2.9%) i/s -     16.609M in   5.076744s
                 new      8.337M (± 1.9%) i/s -     42.237M in   5.067887s

Comparison:
                 new:  8337279.2 i/s
             current:  3274297.7 i/s - 2.55x  (± 0.00) slower

I've removed the parts= and value= methods since these should never be used on a duration.

I've also made parts return a copy of the frozen hash and added a _parts reader so that we can access the parts internally without paying the tax of the dup.

@pixeltrix pixeltrix force-pushed the freeze-duration branch 3 times, most recently from 43f0714 to 75e7b5f Compare April 11, 2021 10:09
@pixeltrix pixeltrix force-pushed the freeze-duration branch 2 times, most recently from 4d31b24 to ee876b8 Compare April 12, 2021 23:46
Durations are meant to be value objects and should not be mutated.
@pixeltrix pixeltrix merged commit d75dc72 into main Apr 13, 2021
@pixeltrix pixeltrix deleted the freeze-duration branch April 13, 2021 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants