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

Reduce calls to get path info when finding routes #38914

Merged
merged 1 commit into from
Apr 13, 2020
Merged

Reduce calls to get path info when finding routes #38914

merged 1 commit into from
Apr 13, 2020

Conversation

jasonhl
Copy link
Contributor

@jasonhl jasonhl commented Apr 10, 2020

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):

Screen Shot 2020-04-10 at 12 28 02 AM

Screen Shot 2020-04-10 at 12 28 18 AM

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.

@rails-bot rails-bot bot added the actionpack label Apr 10, 2020
@kaspth
Copy link
Contributor

kaspth commented Apr 10, 2020

I’d still like to see a benchmark to know the impact, but I’ll leave that call to @rafaelfranca

@rafaelfranca
Copy link
Member

Speedscope shows it will shave almost 2ms. That is 5% of the request time for those actions and this code not only is faster but it make more sense to me.

@rafaelfranca rafaelfranca merged commit c7620bb into rails:master Apr 13, 2020
@jasonhl jasonhl deleted the less-path-info branch April 13, 2020 18:16
@kaspth
Copy link
Contributor

kaspth commented Apr 13, 2020

Perfect! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants