Reuse the Array object passed from parent middleware

This patch reduces Array object allocations from some Rack middleware per each
request by reusing the Array object that wraps status, headers, and body
objects. This is a Rails version of the same improvements that has already been
pushed to Rack 3.0. https://github.com/rack/rack/pull/1887
This commit is contained in:
Akira Matsuda 2022-12-17 18:33:36 +09:00
parent 0854202493
commit 41c2c26dc6
No known key found for this signature in database
3 changed files with 12 additions and 6 deletions

View File

@ -47,7 +47,7 @@ module ActionDispatch
req.path_parameters = tmp_params
status, headers, body = route.app.serve(req)
_, headers, _ = response = route.app.serve(req)
if "pass" == headers["X-Cascade"]
req.script_name = script_name
@ -56,7 +56,7 @@ module ActionDispatch
next
end
return [status, headers, body]
return response
end
[404, { "X-Cascade" => "pass" }, ["Not Found"]]

View File

@ -699,7 +699,7 @@ module ActionDispatch
def call(env)
request = ActionDispatch::Request.new env
status, headers, body = @app.call(env)
_, headers, _ = response = @app.call(env)
if request.have_cookie_jar?
cookie_jar = request.cookie_jar
@ -711,7 +711,7 @@ module ActionDispatch
end
end
[status, headers, body]
response
end
end
end

View File

@ -34,9 +34,15 @@ module Rails
handle.start
logger.info { started_request_message(request) }
status, headers, body = @app.call(env)
status, headers, body = response = @app.call(env)
body = ::Rack::BodyProxy.new(body, &handle.method(:finish))
[status, headers, body]
if response.frozen?
[status, headers, body]
else
response[2] = body
response
end
rescue Exception
handle.finish
raise