From 0cbec58ae48279ae5e9fdf6bbdbceb32183215dd Mon Sep 17 00:00:00 2001 From: schneems Date: Fri, 24 Jul 2015 23:19:15 -0500 Subject: [PATCH] Decrease route_set allocations In handle_positional_args `Array#-=` is used which allocates a new array. Instead we can iterate through and delete elements, modifying the array in place. Also `Array#take` allocates a new array. We can build the same by iterating over the other element. This change buys us 106,470 bytes of memory and 2,663 fewer objects per request. --- actionpack/lib/action_dispatch/routing/route_set.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 42512cad91..43e35366a9 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -267,9 +267,13 @@ def handle_positional_args(controller_options, inner_options, args, result, path path_params -= controller_options.keys path_params -= result.keys end - path_params -= inner_options.keys - path_params.take(args.size).each do |param| - result[param] = args.shift + inner_options.each do |key, _| + path_params.delete(key) + end + + args.each_with_index do |arg, index| + param = path_params[index] + result[param] = arg if param end end