Skip to content

Commit d09d580

Browse files
committed
Inline IsolatedExecutionState#state
Benchmark: https://gist.github.com/byroot/3d251c007c39f85b5e0c6cb268808887 The various IsolatedExecutionState methods are unsurprisingly the hottest spot in various Rails benchmarks, so it's worth micro-optimizing them a bit. With yjit: ``` ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +YJIT +PRISM [arm64-darwin23] Warming up -------------------------------------- orig 268.221k i/100ms opt 331.076k i/100ms Calculating ------------------------------------- orig 2.719M (± 0.9%) i/s (367.72 ns/i) - 13.679M in 5.030598s opt 3.278M (± 1.4%) i/s (305.08 ns/i) - 16.554M in 5.051307s Comparison: orig: 2719444.1 i/s opt: 3277778.4 i/s - 1.21x faster ``` Without YJIT: ``` ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin23] Warming up -------------------------------------- orig 133.735k i/100ms opt 260.734k i/100ms Calculating ------------------------------------- orig 1.343M (± 0.2%) i/s (744.37 ns/i) - 6.820M in 5.077015s opt 2.602M (± 0.7%) i/s (384.26 ns/i) - 13.037M in 5.009746s Comparison: orig: 1343411.1 i/s opt: 2602392.9 i/s - 1.94x faster ```
1 parent e03f459 commit d09d580

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

activesupport/lib/active_support/isolated_execution_state.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,26 @@ def unique_id
3333
end
3434

3535
def [](key)
36-
state[key]
36+
if state = @scope.current.active_support_execution_state
37+
state[key]
38+
end
3739
end
3840

3941
def []=(key, value)
42+
state = (@scope.current.active_support_execution_state ||= {})
4043
state[key] = value
4144
end
4245

4346
def key?(key)
44-
state.key?(key)
47+
@scope.current.active_support_execution_state&.key?(key)
4548
end
4649

4750
def delete(key)
48-
state.delete(key)
51+
@scope.current.active_support_execution_state&.delete(key)
4952
end
5053

5154
def clear
52-
state.clear
55+
@scope.current.active_support_execution_state&.clear
5356
end
5457

5558
def context
@@ -62,11 +65,6 @@ def share_with(other)
6265
# and streaming should be rethought.
6366
context.active_support_execution_state = other.active_support_execution_state.dup
6467
end
65-
66-
private
67-
def state
68-
context.active_support_execution_state ||= {}
69-
end
7068
end
7169

7270
self.isolation_level = :thread

0 commit comments

Comments
 (0)