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

Conversation

Projects
None yet
5 participants
@badosu
Contributor

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

This comment has been minimized.

rails-bot commented Aug 23, 2016

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.

@badosu badosu force-pushed the badosu:improve-tag-option branch Aug 23, 2016

Improve TagHelper#tag_option performance
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 badosu force-pushed the badosu:improve-tag-option branch to 735556d Aug 23, 2016

@badosu

This comment has been minimized.

Contributor

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

1 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
codeclimate Code Climate didn't find any new or fixed issues.
Details
@badosu

This comment has been minimized.

Contributor

badosu commented Aug 23, 2016

@rafaelfranca Thanks!

@badosu badosu deleted the badosu:improve-tag-option branch Aug 23, 2016

rafaelfranca added a commit that referenced this pull request Aug 23, 2016

Merge pull request #26254 from badosu/improve-tag-option
Improve #tag_option performance
@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Aug 23, 2016

Backported in 1f38ef0 and 2509b24

rafaelfranca added a commit that referenced this pull request Aug 23, 2016

Merge pull request #26254 from badosu/improve-tag-option
Improve #tag_option performance

MichaelSp added 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