Reduce calls to get path info when finding routes #38914
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
👋 I work for Shopify. Recently, I was doing some profiling of production requests in order to get a better understanding of where time goes for a subset of cache hits to our main application. These typically take 40-50ms. In particular, I was interested in what happens in our middleware stack and all the things we do on every request prior to doing the actual "work".
That led me to this (from a profile viewed in Speedscope):
The calls to
Rack::Request::Helpers#path_info
were somewhat surprising. Taking a look at the code, that same function is called repeatedly for no good reason. So, I changed the code to simply keep it in a local variable and reuse the value.I didn't write a benchmark because this change is pretty simple and straightforward. I also suspect it should have basically no performance effect on applications which have a small number of routes (it will save some allocations, however). I suspect this is noticeable at Shopify scale because of the sheer number of routes that need to be iterated over.