New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up ActionController::Renderer `normalize_keys` by ~28%. #18643

Merged
merged 1 commit into from Jan 22, 2015

Conversation

Projects
None yet
2 participants
@kaspth
Member

kaspth commented Jan 22, 2015

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.

I didn't find convert_symbols to be very telling, so I renamed it to http_header_format (though I'd love a better name still).

Here's the benchmark that showcases the speed increase:

require 'benchmark/ips'
require 'active_support/core_ext/hash/keys'

env = {
  http_host: 'example.org',
  https: false,
  method: 'get',
  script_name: '',
  'rack.input' => ''
}

Benchmark.ips do |x|
  x.report('dup + bang transform') {
    env.dup.tap do |new_env|
      new_env.keys.each do |key|
        if key.is_a?(Symbol)
          new_env[key.to_s.upcase] = new_env.delete(key)
        end
      end
    end
  }

  x.report('transform_keys') {
    env.transform_keys do |key|
      key.is_a?(Symbol) ? key.to_s.upcase : key
    end
  }

  x.compare!
end

which results in an around 28% speed increase:

Calculating -------------------------------------
dup + bang transform     7.239k i/100ms
      transform_keys     8.800k i/100ms
-------------------------------------------------
dup + bang transform     88.735k (± 3.4%) i/s -    448.818k
      transform_keys    113.369k (± 3.6%) i/s -    572.000k

Comparison:
      transform_keys:   113369.5 i/s
dup + bang transform:    88735.4 i/s - 1.28x slower
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`.
@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca Jan 22, 2015

Member

It broke tests for ruby-head

Member

rafaelfranca commented Jan 22, 2015

It broke tests for ruby-head

rafaelfranca added a commit that referenced this pull request Jan 22, 2015

Merge pull request #18643 from kaspth/renderer-normalize_keys-speedup
Speed up ActionController::Renderer `normalize_keys` by ~28%.

@rafaelfranca rafaelfranca merged commit 139c232 into rails:master Jan 22, 2015

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details

@rafaelfranca rafaelfranca deleted the kaspth:renderer-normalize_keys-speedup branch Jan 22, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment