Skip to content

Commit 41c2c26

Browse files
committed
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. rack/rack#1887
1 parent 0854202 commit 41c2c26

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

actionpack/lib/action_dispatch/journey/router.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def serve(req)
4747

4848
req.path_parameters = tmp_params
4949

50-
status, headers, body = route.app.serve(req)
50+
_, headers, _ = response = route.app.serve(req)
5151

5252
if "pass" == headers["X-Cascade"]
5353
req.script_name = script_name
@@ -56,7 +56,7 @@ def serve(req)
5656
next
5757
end
5858

59-
return [status, headers, body]
59+
return response
6060
end
6161

6262
[404, { "X-Cascade" => "pass" }, ["Not Found"]]

actionpack/lib/action_dispatch/middleware/cookies.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def initialize(app)
699699
def call(env)
700700
request = ActionDispatch::Request.new env
701701

702-
status, headers, body = @app.call(env)
702+
_, headers, _ = response = @app.call(env)
703703

704704
if request.have_cookie_jar?
705705
cookie_jar = request.cookie_jar
@@ -711,7 +711,7 @@ def call(env)
711711
end
712712
end
713713

714-
[status, headers, body]
714+
response
715715
end
716716
end
717717
end

railties/lib/rails/rack/logger.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@ def call_app(request, env) # :doc:
3434
handle.start
3535

3636
logger.info { started_request_message(request) }
37-
status, headers, body = @app.call(env)
37+
status, headers, body = response = @app.call(env)
3838
body = ::Rack::BodyProxy.new(body, &handle.method(:finish))
39-
[status, headers, body]
39+
40+
if response.frozen?
41+
[status, headers, body]
42+
else
43+
response[2] = body
44+
response
45+
end
4046
rescue Exception
4147
handle.finish
4248
raise

0 commit comments

Comments
 (0)