diff options
author | eileencodes <eileencodes@gmail.com> | 2015-02-18 18:37:35 -0500 |
---|---|---|
committer | eileencodes <eileencodes@gmail.com> | 2015-02-18 18:54:37 -0500 |
commit | 95ee93892a07f90fdfd503c7ea07abf0fb397bec (patch) | |
tree | a0c181c35bcd0cd78bdd66c4e1b7cf0f8785f1b6 | |
parent | e334417b7804b721306e70dbd53d2d542c8f7847 (diff) | |
download | rails-95ee93892a07f90fdfd503c7ea07abf0fb397bec.tar.gz rails-95ee93892a07f90fdfd503c7ea07abf0fb397bec.tar.bz2 rails-95ee93892a07f90fdfd503c7ea07abf0fb397bec.zip |
Freeze strings to reduce allocations in integration tests
Moves `X-Request-ID`, `action_dispatch.request_id` and
`HTTP_X_REQUEST_ID` strings to constants and freezes them.
We are freezing these strings to reduce the number of allocations in
Rails integration tests. The tests are spending a lot of time in GC and
this reduces the amount of time spent from 12% to 9% (in combination
with Rack PR that also freezes some strings).
Number of allocations before this change: 1030722
Number of allocations after this change: 967722
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/request_id.rb | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/request_id.rb b/actionpack/lib/action_dispatch/middleware/request_id.rb index 25658bac3d..cf31815c23 100644 --- a/actionpack/lib/action_dispatch/middleware/request_id.rb +++ b/actionpack/lib/action_dispatch/middleware/request_id.rb @@ -12,19 +12,23 @@ module ActionDispatch # The unique request id can be used to trace a request end-to-end and would typically end up being part of log files # from multiple pieces of the stack. class RequestId + X_REQUEST_ID = "X-Request-Id".freeze # :nodoc: + ACTION_DISPATCH_REQUEST_ID = "action_dispatch.request_id".freeze # :nodoc: + HTTP_X_REQUEST_ID = "HTTP_X_REQUEST_ID".freeze # :nodoc: + def initialize(app) @app = app end def call(env) - env["action_dispatch.request_id"] = external_request_id(env) || internal_request_id - @app.call(env).tap { |_status, headers, _body| headers["X-Request-Id"] = env["action_dispatch.request_id"] } + env[ACTION_DISPATCH_REQUEST_ID] = external_request_id(env) || internal_request_id + @app.call(env).tap { |_status, headers, _body| headers[X_REQUEST_ID] = env[ACTION_DISPATCH_REQUEST_ID] } end private def external_request_id(env) - if request_id = env["HTTP_X_REQUEST_ID"].presence - request_id.gsub(/[^\w\-]/, "").first(255) + if request_id = env[HTTP_X_REQUEST_ID].presence + request_id.gsub(/[^\w\-]/, "".freeze).first(255) end end |