Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(router) efficient host header reading #2271

Merged
merged 1 commit into from
Mar 28, 2017

Conversation

thibaultcha
Copy link
Member

We now avoid reading all request headers via ngx.req.get_headers() and
use the ngx.var API instead. In the future, we'll distinguish Host
fetching and full headers fetching when arbitrary headers matching will
be added to the router.

We now avoid reading all request headers via `ngx.req.get_headers()` and
use the `ngx.var` API instead. In the future, we'll distinguish Host
fetching and full headers fetching when arbitrary headers matching will
be added to the router.
@@ -446,25 +446,24 @@ function _M.new(apis)
end)


local grab_headers = #wildcard_hosts > 0 or next(indexes.plain_hosts) ~= nil
local grab_host = #wildcard_hosts > 0 or next(indexes.plain_hosts) ~= nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might we consider removing this variable altogether and moving this expression to line 582? reducing the number of live local variables is a win (this assumes the commented out print statement on 579 is removed or adjusted as well)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is such a hot code path that the intent here has always been to cache whether retrieving any header is necessary at all. Moving this down to 582 prevents this code to JIT (next) and evaluates the same statement over and over again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my fault for misreading the scope of the var. For some reason I thought it was being declared in exec, not new. Ignore me!

if type(method) ~= "string" then
return error("arg #1 method must be a string")
end
if type(uri) ~= "string" then
return error("arg #2 uri must be a string")
end
if type(headers) ~= "table" then
return error("arg #3 headers must be a table")
if host and type(host) ~= "string" then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the host and necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the tests, there are occasions where the 3rd argument is ignored for readability.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type(host) would be nil then, no? So the conditional is not necessary then. But no big deal, you know the codebase better than I. Just trying to grasp everything :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, we can probably remove it from here :)

Copy link
Member Author

@thibaultcha thibaultcha Mar 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually on second thought, it is better as it is :) The ~= "string" is key here.

@thibaultcha thibaultcha merged commit 98d379d into next Mar 28, 2017
@thibaultcha thibaultcha deleted the perf/router-host-header-reading branch March 28, 2017 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants