Skip to content

Commit 25bfef2

Browse files
committed
Minor performance improvements
1 parent 54edc4c commit 25bfef2

File tree

4 files changed

+46
-35
lines changed

4 files changed

+46
-35
lines changed

lib/view_component/base.rb

+10-11
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ def render_in(view_context, &block)
7272

7373
# TODO flag with initializer
7474
if !self.__vc_original_view_context
75-
view_context.original_output_buffer = ActionView::OutputBuffer.new if !view_context.output_buffer
75+
view_context.output_buffer = ActionView::OutputBuffer.new if !view_context.output_buffer
7676
self.__vc_original_view_context ||= view_context
7777
if self.__vc_original_view_context.global_buffer_coordinator.nil?
7878
coordinator = self.__vc_original_view_context.global_buffer_coordinator = GlobalBuffer::Coordinator.new
79-
coordinator.subscribers.push(self.__vc_original_view_context)
79+
coordinator.subscribe(self.__vc_original_view_context)
8080
end
8181
end
8282

8383
@output_buffer = self.__vc_original_view_context.output_buffer
8484
self.global_buffer_coordinator = self.__vc_original_view_context.global_buffer_coordinator
85-
self.__vc_original_view_context.global_buffer_coordinator.subscribers.push(self)
85+
self.__vc_original_view_context.global_buffer_coordinator.subscribe(self)
8686

8787
@lookup_context ||= view_context.lookup_context
8888

@@ -114,18 +114,17 @@ def render_in(view_context, &block)
114114
@__vc_content_evaluated = false
115115
@__vc_render_in_block = block
116116

117-
self.__vc_original_view_context.capture do
118-
before_render
117+
before_render
119118

120-
if render?
121-
capture do
122-
render_template_for(@__vc_variant).to_s + _output_postamble
123-
end
124-
else
125-
""
119+
if render?
120+
capture do
121+
render_template_for(@__vc_variant).to_s + _output_postamble
126122
end
123+
else
124+
""
127125
end
128126
ensure
127+
global_buffer_coordinator&.unsubscribe(self)
129128
@current_template = old_current_template
130129
end
131130

lib/view_component/global_buffer.rb

+18-10
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@ module ViewComponent
44
# TODO if things break, log each time an unsafe string method is called
55
module GlobalBuffer
66
class Coordinator
7+
attr_reader :subscribers
8+
79
def initialize
8-
@subscribers = []
10+
@subscribers = Set.new
11+
end
12+
13+
def subscribe(subscriber)
14+
@subscribers << subscriber
15+
end
16+
17+
def unsubscribe(subscriber)
18+
@subscribers.delete(subscriber)
919
end
1020

11-
def subscribers
12-
@subscribers
21+
def clear
22+
@subscribers = []
1323
end
1424
end
1525

@@ -24,33 +34,31 @@ def with_output_buffer(buf = nil) # :nodoc:
2434
end
2535
end
2636
self.output_buffer, old_buffer = buf, output_buffer
27-
global_buffer_coordinator&.subscribers&.each { |s| s.output_buffer = buf}
37+
global_buffer_coordinator.subscribers.each { |s| s.output_buffer = buf} if global_buffer_coordinator
2838
yield
2939
output_buffer
3040
ensure
3141
self.output_buffer = old_buffer
32-
global_buffer_coordinator&.subscribers&.each { |s| s.output_buffer = old_buffer }
42+
global_buffer_coordinator.subscribers.each { |s| s.output_buffer = old_buffer } if global_buffer_coordinator
3343
end
3444

3545
def _run(method, template, locals, buffer, add_to_stack: true, &block)
3646
_old_output_buffer, _old_virtual_path, _old_template = @output_buffer, @virtual_path, @current_template
3747
@current_template = template if add_to_stack
3848
@output_buffer = buffer
39-
global_buffer_coordinator&.subscribers&.each { |s| s.output_buffer = buffer }
49+
global_buffer_coordinator.subscribers.each { |s| s.output_buffer = buffer } if global_buffer_coordinator
4050
public_send(method, locals, buffer, &block)
4151
ensure
4252
@output_buffer, @virtual_path, @current_template = _old_output_buffer, _old_virtual_path, _old_template
43-
global_buffer_coordinator&.subscribers&.each { |s| s.output_buffer = _old_output_buffer }
53+
global_buffer_coordinator.subscribers.each { |s| s.output_buffer = _old_output_buffer } if global_buffer_coordinator
4454
end
4555
end
4656

4757
included do
4858
attr_accessor :global_buffer_coordinator
59+
alias_method(:original_with_output_buffer, :with_output_buffer)
4960

50-
alias_method(:original_output_buffer=, :output_buffer=)
5161
prepend Compatibility
52-
53-
alias_method(:original_with_output_buffer, :with_output_buffer)
5462
end
5563
end
5664
end

lib/view_component/slot_v2.rb

+4-10
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,12 @@ def to_s
4545
if defined?(@__vc_content_set_by_with_content)
4646
@__vc_component_instance.with_content(@__vc_content_set_by_with_content)
4747

48-
view_context.capture do
49-
@__vc_component_instance.render_in(view_context)
50-
end
48+
@__vc_component_instance.render_in(view_context)
5149
elsif defined?(@__vc_content_block)
52-
view_context.capture do
53-
# render_in is faster than `parent.render`
54-
@__vc_component_instance.render_in(view_context, &@__vc_content_block)
55-
end
50+
# render_in is faster than `parent.render`
51+
@__vc_component_instance.render_in(view_context, &@__vc_content_block)
5652
else
57-
view_context.capture do
58-
@__vc_component_instance.render_in(view_context)
59-
end
53+
@__vc_component_instance.render_in(view_context)
6054
end
6155
elsif defined?(@__vc_content)
6256
@__vc_content

performance/benchmark.rb

+14-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,25 @@ class BenchmarksController < ActionController::Base
1818
end
1919

2020
BenchmarksController.view_paths = [File.expand_path("./views", __dir__)]
21-
controller_view = BenchmarksController.new.view_context
2221

2322
Benchmark.ips do |x|
2423
x.time = 10
2524
x.warmup = 2
2625

27-
x.report("component:") { controller_view.render(Performance::NameComponent.new(name: "Fox Mulder")) }
28-
x.report("inline:") { controller_view.render(Performance::InlineComponent.new(name: "Fox Mulder")) }
29-
x.report("partial:") { controller_view.render("partial", name: "Fox Mulder") }
26+
x.report("component:") do
27+
controller_view = BenchmarksController.new.view_context
28+
controller_view.render(Performance::NameComponent.new(name: "Fox Mulder"))
29+
end
30+
31+
x.report("inline:") do
32+
controller_view = BenchmarksController.new.view_context
33+
controller_view.render(Performance::InlineComponent.new(name: "Fox Mulder"))
34+
end
35+
36+
x.report("partial:") do
37+
controller_view = BenchmarksController.new.view_context
38+
controller_view.render("partial", name: "Fox Mulder")
39+
end
3040

3141
x.compare!
3242
end

0 commit comments

Comments
 (0)