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

Improve #tag_option performance #26254

Merged
merged 1 commit into from
Aug 23, 2016
Merged

Conversation

badosu
Copy link
Contributor

@badosu badosu commented Aug 23, 2016

After running stackprof on my production application homepage I noticed that TagHelper#tag_option is the second occurrence:

  Mode: wall(50)
  Samples: 43496 (2.11% miss rate)
  GC: 713 (1.64%)
==================================
     TOTAL    (pct)     SAMPLES    (pct)     FRAME
      2459   (5.7%)        2459   (5.7%)     ActiveSupport::SafeBuffer#initialize
      3273   (7.5%)        1810   (4.2%)     ActionView::Helpers::TagHelper#tag_option
      2177   (5.0%)        1754   (4.0%)     #<Module:0x0055667655fe60>.unwrapped_html_escape
      1746   (4.0%)        1746   (4.0%)     ThreadSafe::NonConcurrentCacheBackend#[]
       889   (2.0%)         889   (2.0%)     ActiveSupport::Notifications::Event#initialize

I performed some tests with benchmark-ips and noticed that String#gsub with a String input is almost 2x faster than with a Regex (in this specific case).

I also benchmarked this change and #tag_option speed is improved from 20-45% on the most common cases (when escape is true, with or without quotes present) and up to 50% when escape is false.

I noticed as well that by freezing the string literals I was able to extract a little bit more of performance when an array of values is passed.

Given that this is an internal method and the diff is very trivial I can't see any reason not to change this and have a little performance improvement for free.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @matthewd (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

Freeze string literals and use String instead of
Regex inside gsub call. This should improve performance from 20% up to
50% on most cases.
@badosu
Copy link
Contributor Author

badosu commented Aug 23, 2016

I am not sure why the build failed but seems unrelated with this PR.

@rafaelfranca rafaelfranca merged commit cea338d into rails:master Aug 23, 2016
@badosu
Copy link
Contributor Author

badosu commented Aug 23, 2016

@rafaelfranca Thanks!

@badosu badosu deleted the improve-tag-option branch August 23, 2016 03:49
rafaelfranca added a commit that referenced this pull request Aug 23, 2016
@rafaelfranca
Copy link
Member

Backported in 1f38ef0 and 2509b24

rafaelfranca added a commit that referenced this pull request Aug 23, 2016
MichaelSp pushed a commit to MichaelSp/rails that referenced this pull request Nov 2, 2016
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.

None yet

5 participants