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

Do not allocate the first path character in asset helper #47714

Merged

Conversation

nirebu
Copy link
Contributor

@nirebu nirebu commented Mar 20, 2023

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

And its output, showing a consistent speedup:

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

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:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot bot added the actionview label Mar 20, 2023
@nirebu nirebu force-pushed the nirebu/stop-root-string-allocation-in-helper branch from 1f57a5b to 5f09b70 Compare March 20, 2023 16:31
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 nirebu force-pushed the nirebu/stop-root-string-allocation-in-helper branch from 5f09b70 to a63ae91 Compare March 20, 2023 16:32
@jonathanhefner jonathanhefner merged commit d53dc86 into rails:main Mar 20, 2023
@jonathanhefner
Copy link
Member

Thank you, @nirebu! 🎉

@nirebu nirebu deleted the nirebu/stop-root-string-allocation-in-helper branch March 21, 2023 07:48
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants