1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
eileencodes 2015-02-18 18:37:35 -05:00
parent e334417b78
commit 95ee93892a

View file

@ -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