Skip to content

Commit b2afa8b

Browse files
Improve performance of AbstractController::Base#action_methods
I was benchmarking some specs in my app, and saw this code come up in the memory_profiler. It is by no means the biggest memory allocation, but it is straightforward to make a slight improvement. Primarily, this saves allocating one array by using concat instead of + to add public_instance_methods(false). That ends up being 10% less memory for my benchmark (3 actions), and 6% faster. ``` Calculating ------------------------------------- original 8.352k memsize ( 208.000 retained) 22.000 objects ( 2.000 retained) 6.000 strings ( 0.000 retained) refactored3 7.616k memsize ( 408.000 retained) 11.000 objects ( 7.000 retained) 3.000 strings ( 3.000 retained) Comparison: refactored3: 7616 allocated original: 8352 allocated - 1.10x more Warming up -------------------------------------- original 2.326k i/100ms refactored3 2.441k i/100ms Calculating ------------------------------------- original 23.336k (± 0.7%) i/s - 118.626k in 5.083658s refactored3 24.692k (± 1.2%) i/s - 124.491k in 5.042345s Comparison: original: 23336.0 i/s refactored3: 24692.5 i/s - 1.06x faster ``` Benchmark and results are also posted to https://gist.github.com/technicalpickles/4a4ae6a9e2c42963af43a89f75e768fe
1 parent a5f64bf commit b2afa8b

File tree

1 file changed

+8
-11
lines changed
  • actionpack/lib/abstract_controller

1 file changed

+8
-11
lines changed

actionpack/lib/abstract_controller/base.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,15 @@ def internal_methods
8989
# ==== Returns
9090
# * <tt>Set</tt> - A set of all methods that should be considered actions.
9191
def action_methods
92+
# All public instance methods of this class, including ancestors
9293
@action_methods ||= begin
93-
# All public instance methods of this class, including ancestors
94-
methods = (public_instance_methods(true) -
95-
# Except for public instance methods of Base and its ancestors
96-
internal_methods +
97-
# Be sure to include shadowed public instance methods of this class
98-
public_instance_methods(false))
99-
100-
methods.map!(&:to_s)
101-
102-
methods.to_set
103-
end
94+
# Except for public instance methods of Base and its ancestors
95+
methods = public_instance_methods(true) - internal_methods
96+
# Except for public instance methods of Base and its ancestors
97+
methods.concat(public_instance_methods(false))
98+
methods.map!(&:to_s)
99+
methods.to_set
100+
end
104101
end
105102

106103
# action_methods are cached and there is sometimes a need to refresh

0 commit comments

Comments
 (0)