Skip to content

Commit 8e1c1cc

Browse files
byrootMorriar
andcommitted
Optimize many delegated methods on ActiveSupport::Duration
Fix: rails#50136 Using delegate saves on the `method_missing + public_send` combo. I chose to delegate to all the methods I saw called in Rails own test suite, but there is likely a handful more candidates for explicit delegation. But also this rely on a new (private) parameter in `Module#delegate` to provide the expected delegated type and use that to define delegators with the extact required signature. This saves on array and hash allocations caused by splatting. In some ways it's a continuation of rails#46875 Note that I didn't make the new `as:` parameter public, as I fear it's a bit too brittle to be used. For the same reason I'm considering reverting the optimized path behavior on `to: :class` and requiring to explictly pass `as: self` for that optimized path. Co-Authored-By: Alexandre Terrasa <[email protected]>
1 parent e1800cd commit 8e1c1cc

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

activesupport/lib/active_support/duration.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module ActiveSupport
1414
class Duration
1515
class Scalar < Numeric # :nodoc:
1616
attr_reader :value
17-
delegate :to_i, :to_f, :to_s, to: :value
17+
delegate :to_i, :to_f, :to_s, to: :@value
1818

1919
def initialize(value)
2020
@value = value
@@ -221,6 +221,8 @@ def calculate_total_seconds(parts)
221221
end
222222
end
223223

224+
delegate :to_f, :positive?, :negative?, :zero?, :abs, to: :@value, as: Integer
225+
224226
def initialize(value, parts, variable = nil) # :nodoc:
225227
@value, @parts = value, parts
226228
@parts.reject! { |k, v| v.zero? } unless value == 0

0 commit comments

Comments
 (0)