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

Speed up partial rendering by caching "variable" calculation #35171

Merged
merged 1 commit into from Feb 6, 2019

Conversation

tenderlove
Copy link
Member

This commit speeds up rendering partials by caching the variable name
calculation on the template. The variable name is based on the "virtual
path" used for looking up the template. The same virtual path
information lives on the template, so we can just ask the cached
template object for the variable.

This benchmark takes a couple files, so I'll cat them below:

[aaron@TC ~/g/r/actionview (speed-up-partials)]$ cat render_benchmark.rb
require "benchmark/ips"
require "action_view"
require "action_pack"
require "action_controller"

class TestController < ActionController::Base
end

TestController.view_paths = [File.expand_path("test/benchmarks")]
controller_view = TestController.new.view_context

result = Benchmark.ips do |x|
  x.report("render") do
    controller_view.render("many_partials")
  end
end
[aaron@TC ~/g/r/actionview (speed-up-partials)]$ cat test/benchmarks/test/_many_partials.html.erb
Looping:
<ul>
<% 100.times do |i| %>
  <%= render partial: "list_item", locals: { i: i } %>
<% end %>
</ul>
[aaron@TC ~/g/r/actionview (speed-up-partials)]$ cat test/benchmarks/test/_list_item.html.erb
<li>Number: <%= i %></li>

Benchmark results (master):

[aaron@TC ~/g/r/actionview (master)]$ be ruby render_benchmark.rb
Warming up --------------------------------------
              render    41.000  i/100ms
Calculating -------------------------------------
              render    424.269  (± 3.5%) i/s -      2.132k in   5.031455s

Benchmark results (this branch):

[aaron@TC ~/g/r/actionview (speed-up-partials)]$ be ruby render_benchmark.rb
Warming up --------------------------------------
              render    50.000  i/100ms
Calculating -------------------------------------
              render    521.862  (± 3.8%) i/s -      2.650k in   5.085885s

Caching is enabled, so this improvement should impact production apps. Also, the logic in the partial renderer is pretty complex, we may not even need that value to be calculated.

self
end

def as_variable(options)
if as = options[:as]
raise_invalid_option_as(as) unless /\A[a-z_]\w*\z/.match?(as.to_s)
as = as.to_sym
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this as = is not necessary here anymore.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch, thanks!

This commit speeds up rendering partials by caching the variable name
calculation on the template.  The variable name is based on the "virtual
path" used for looking up the template.  The same virtual path
information lives on the template, so we can just ask the cached
template object for the variable.

This benchmark takes a couple files, so I'll cat them below:

```
[aaron@TC ~/g/r/actionview (speed-up-partials)]$ cat render_benchmark.rb
require "benchmark/ips"
require "action_view"
require "action_pack"
require "action_controller"

class TestController < ActionController::Base
end

TestController.view_paths = [File.expand_path("test/benchmarks")]
controller_view = TestController.new.view_context

result = Benchmark.ips do |x|
  x.report("render") do
    controller_view.render("many_partials")
  end
end
[aaron@TC ~/g/r/actionview (speed-up-partials)]$ cat test/benchmarks/test/_many_partials.html.erb
Looping:
<ul>
<% 100.times do |i| %>
  <%= render partial: "list_item", locals: { i: i } %>
<% end %>
</ul>
[aaron@TC ~/g/r/actionview (speed-up-partials)]$ cat test/benchmarks/test/_list_item.html.erb
<li>Number: <%= i %></li>
```

Benchmark results (master):

```
[aaron@TC ~/g/r/actionview (master)]$ be ruby render_benchmark.rb
Warming up --------------------------------------
              render    41.000  i/100ms
Calculating -------------------------------------
              render    424.269  (± 3.5%) i/s -      2.132k in   5.031455s
```

Benchmark results (this branch):

```
[aaron@TC ~/g/r/actionview (speed-up-partials)]$ be ruby render_benchmark.rb
Warming up --------------------------------------
              render    50.000  i/100ms
Calculating -------------------------------------
              render    521.862  (± 3.8%) i/s -      2.650k in   5.085885s
```
@tenderlove tenderlove merged commit 9483cde into master Feb 6, 2019
@tenderlove tenderlove deleted the speed-up-partials branch February 6, 2019 00:41
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