From 95ee93892a07f90fdfd503c7ea07abf0fb397bec Mon Sep 17 00:00:00 2001 From: eileencodes Date: Wed, 18 Feb 2015 18:37:35 -0500 Subject: 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 --- actionpack/lib/action_dispatch/middleware/request_id.rb | 12 ++++++++---- 1 file 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 -- cgit v1.2.3