-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Do not allocate the first path character in asset helper #47714
Merged
jonathanhefner
merged 1 commit into
rails:main
from
nirebu:nirebu/stop-root-string-allocation-in-helper
Mar 20, 2023
Merged
Do not allocate the first path character in asset helper #47714
jonathanhefner
merged 1 commit into
rails:main
from
nirebu:nirebu/stop-root-string-allocation-in-helper
Mar 20, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nirebu
force-pushed
the
nirebu/stop-root-string-allocation-in-helper
branch
from
March 20, 2023 16:31
1f57a5b
to
5f09b70
Compare
In asset heavy views, the asset_path helper might be called a lot of times. When checking the first character of the source element with [] we allocate a string each time just to check it against ?/. By using String#start_with? we can avoid this step and go a bit faster. This is the code I used to measure the change: ``` require "bundler/inline" ROOT_STRING = '/' TEST_PATH = "/some/path" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report("source[0]") do TEST_PATH[0] != ROOT_STRING end x.report("source.start_with?") do TEST_PATH.start_with?(ROOT_STRING) end x.compare! end ``` Warming up -------------------------------------- source[0] 905.322k i/100ms source.start_with? 1.541M i/100ms Calculating ------------------------------------- source[0] 9.012M (± 0.7%) i/s - 45.266M in 5.022969s source.start_with? 15.395M (± 0.4%) i/s - 77.030M in 5.003691s Comparison: source.start_with?: 15394807.0 i/s source[0]: 9012304.9 i/s - 1.71x slower
nirebu
force-pushed
the
nirebu/stop-root-string-allocation-in-helper
branch
from
March 20, 2023 16:32
5f09b70
to
a63ae91
Compare
Thank you, @nirebu! 🎉 |
4 tasks
nirebu
added a commit
to nirebu/rails
that referenced
this pull request
Mar 22, 2023
This is the same optimization applied in rails@a63ae91 which I proposed in rails#47714 Here's the benchmark: require "bundler/inline" ROOT_STRING = '/' TEST_PATH = "/some/path" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report("path[0]") do TEST_PATH[0] != ROOT_STRING end x.report("path.start_with?") do TEST_PATH.start_with?(ROOT_STRING) end x.compare! end Warming up -------------------------------------- path[0] 942.044k i/100ms path.start_with? 1.556M i/100ms Calculating ------------------------------------- path[0] 9.463M (± 0.9%) i/s - 48.044M in 5.077358s path.start_with? 15.611M (± 0.2%) i/s - 79.352M in 5.083056s Comparison: path.start_with?: 15611192.8 i/s path[0]: 9463245.0 i/s - 1.65x slower
danielvdao
pushed a commit
to danielvdao/rails
that referenced
this pull request
May 1, 2023
This is the same optimization applied in rails@a63ae91 which I proposed in rails#47714 Here's the benchmark: require "bundler/inline" ROOT_STRING = '/' TEST_PATH = "/some/path" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report("path[0]") do TEST_PATH[0] != ROOT_STRING end x.report("path.start_with?") do TEST_PATH.start_with?(ROOT_STRING) end x.compare! end Warming up -------------------------------------- path[0] 942.044k i/100ms path.start_with? 1.556M i/100ms Calculating ------------------------------------- path[0] 9.463M (± 0.9%) i/s - 48.044M in 5.077358s path.start_with? 15.611M (± 0.2%) i/s - 79.352M in 5.083056s Comparison: path.start_with?: 15611192.8 i/s path[0]: 9463245.0 i/s - 1.65x slower
danielvdao
pushed a commit
to danielvdao/rails
that referenced
this pull request
May 1, 2023
This is the same optimization applied in rails@a63ae91 which I proposed in rails#47714 Here's the benchmark: require "bundler/inline" ROOT_STRING = '/' TEST_PATH = "/some/path" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report("path[0]") do TEST_PATH[0] != ROOT_STRING end x.report("path.start_with?") do TEST_PATH.start_with?(ROOT_STRING) end x.compare! end Warming up -------------------------------------- path[0] 942.044k i/100ms path.start_with? 1.556M i/100ms Calculating ------------------------------------- path[0] 9.463M (± 0.9%) i/s - 48.044M in 5.077358s path.start_with? 15.611M (± 0.2%) i/s - 79.352M in 5.083056s Comparison: path.start_with?: 15611192.8 i/s path[0]: 9463245.0 i/s - 1.65x slower
danielvdao
pushed a commit
to danielvdao/rails
that referenced
this pull request
May 1, 2023
This is the same optimization applied in rails@a63ae91 which I proposed in rails#47714 Here's the benchmark: require "bundler/inline" ROOT_STRING = '/' TEST_PATH = "/some/path" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report("path[0]") do TEST_PATH[0] != ROOT_STRING end x.report("path.start_with?") do TEST_PATH.start_with?(ROOT_STRING) end x.compare! end Warming up -------------------------------------- path[0] 942.044k i/100ms path.start_with? 1.556M i/100ms Calculating ------------------------------------- path[0] 9.463M (± 0.9%) i/s - 48.044M in 5.077358s path.start_with? 15.611M (± 0.2%) i/s - 79.352M in 5.083056s Comparison: path.start_with?: 15611192.8 i/s path[0]: 9463245.0 i/s - 1.65x slower
briu
pushed a commit
to briu/rails
that referenced
this pull request
May 4, 2023
This is the same optimization applied in rails@a63ae91 which I proposed in rails#47714 Here's the benchmark: require "bundler/inline" ROOT_STRING = '/' TEST_PATH = "/some/path" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report("path[0]") do TEST_PATH[0] != ROOT_STRING end x.report("path.start_with?") do TEST_PATH.start_with?(ROOT_STRING) end x.compare! end Warming up -------------------------------------- path[0] 942.044k i/100ms path.start_with? 1.556M i/100ms Calculating ------------------------------------- path[0] 9.463M (± 0.9%) i/s - 48.044M in 5.077358s path.start_with? 15.611M (± 0.2%) i/s - 79.352M in 5.083056s Comparison: path.start_with?: 15611192.8 i/s path[0]: 9463245.0 i/s - 1.65x slower
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
In asset heavy views, the asset_path helper might be called a lot of times. When checking the first character of the source element with
[]
we allocate a string each time just to check it against?/
.By using
String#start_with?
we can avoid this step and go a bit faster.This is the code I used to measure the change:
And its output, showing a consistent speedup:
Motivation / Background
This Pull Request has been created because I noticed the
/
string getting allocated many times while benchmarking a client application.Detail
This Pull Request changes the way we check if the first character of an asset URL is
/
, without allocating this string each time.Additional information
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]