1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

eagerly allocate the renderer object

this means the reader doesn't need to lock, but does have the added cost
of a new object created for every controller
This commit is contained in:
Aaron Patterson 2015-09-14 15:29:19 -07:00
parent 9808cdfa03
commit 8e489db9de
3 changed files with 10 additions and 9 deletions

View file

@ -248,6 +248,7 @@ module ActionController
MODULES.each do |mod|
include mod
end
setup_renderer!
# Define some internal variables that should not be propagated to the view.
PROTECTED_IVARS = AbstractController::Rendering::DEFAULT_PROTECTED_INSTANCE_VARIABLES + [

View file

@ -11,20 +11,16 @@ module ActionController
# Documentation at ActionController::Renderer#render
delegate :render, to: :renderer
RENDERER_LOCK = Mutex.new
attr_writer :renderer
# Returns a renderer instance (inherited from ActionController::Renderer)
# for the controller.
def renderer
@renderer || RENDERER_LOCK.synchronize do
@renderer ||= Renderer.for(self)
end
attr_reader :renderer
def setup_renderer! # :nodoc:
@renderer = Renderer.for(self)
end
def inherited(klass)
klass.renderer = nil
klass.setup_renderer!
super
end
end

View file

@ -1,6 +1,10 @@
require 'abstract_unit'
class RendererTest < ActiveSupport::TestCase
test 'action controller base has a renderer' do
assert ActionController::Base.renderer
end
test 'creating with a controller' do
controller = CommentsController
renderer = ActionController::Renderer.for controller