Speed up `normalize_keys` by removing dup step.

Previously env was duplicated and then had it's keys mutated. This iterates through
the hash twice.
Using `transform_keys`, duplication and key mutation is a single iteration.

`convert_symbols` was renamed to `http_header_format`.
This commit is contained in:
Kasper Timm Hansen 2015-01-22 17:35:19 +01:00
parent 3d4eaf6dad
commit d3211d76ce
1 changed files with 6 additions and 9 deletions

View File

@ -1,3 +1,5 @@
require 'active_support/core_ext/hash/keys'
module ActionController
# ActionController::Renderer allows to render arbitrary templates
# without requirement of being in controller actions.
@ -71,20 +73,15 @@ module ActionController
private
def normalize_keys(env)
env.dup.tap do |new_env|
convert_symbols! new_env
http_header_format(env).tap do |new_env|
handle_method_key! new_env
handle_https_key! new_env
end
end
def convert_symbols!(env)
env.keys.each do |key|
if key.is_a? Symbol
value = env.delete key
key = key.to_s.upcase
env[key] = value
end
def http_header_format(env)
env.transform_keys do |key|
key.is_a?(Symbol) ? key.to_s.upcase : key
end
end