Skip to content

Commit

Permalink
Move the cache to a nested hash which performs better than a hash wit…
Browse files Browse the repository at this point in the history
…h array as keys.
  • Loading branch information
josevalim committed Jul 28, 2011
1 parent dee8115 commit d5eeacc
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions actionpack/lib/action_view/renderer/partial_renderer.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ module ActionView
# <%- end -%> # <%- end -%>
# <% end %> # <% end %>
class PartialRenderer < AbstractRenderer #:nodoc: class PartialRenderer < AbstractRenderer #:nodoc:
PARTIAL_NAMES = Hash.new { |h,k| h[k] = {} }

def initialize(*)
super
@context_prefix = @lookup_context.prefixes.first
@partial_names = PARTIAL_NAMES[@context_prefix]
end

def render(context, options, block) def render(context, options, block)
setup(context, options, block) setup(context, options, block)


Expand Down Expand Up @@ -284,6 +292,7 @@ def setup(context, options, block)
else else
paths.map! { |path| retrieve_variable(path).unshift(path) } paths.map! { |path| retrieve_variable(path).unshift(path) }
end end

if String === partial && @variable.to_s !~ /^[a-z_][a-zA-Z_0-9]*$/ if String === partial && @variable.to_s !~ /^[a-z_][a-zA-Z_0-9]*$/
raise ArgumentError.new("The partial name (#{partial}) is not a valid Ruby identifier; " + raise ArgumentError.new("The partial name (#{partial}) is not a valid Ruby identifier; " +
"make sure your partial name starts with a letter or underscore, " + "make sure your partial name starts with a letter or underscore, " +
Expand Down Expand Up @@ -352,21 +361,18 @@ def collection_without_template
segments segments
end end


PARTIAL_PATHS = {}

def partial_path(object = @object) def partial_path(object = @object)
object = object.to_model if object.respond_to?(:to_model) object = object.to_model if object.respond_to?(:to_model)


path = if object.respond_to?(:to_path) path = if object.respond_to?(:to_path)
object.to_path object.to_path
else else
ActiveSupport::Deprecation.warn "ActiveModel-compatible objects whose classes return a #model_name that responds to #partial_path are deprecated. Please respond to #to_path directly instead." ActiveSupport::Deprecation.warn "ActiveModel-compatible objects whose classes return a #model_name that responds to #partial_path are deprecated. Please respond to #to_path directly instead."
object.class.model_name.partial_path object.class.model_name.partial_path
end end


prefix = @lookup_context.prefixes.first @partial_names[path] ||= path.dup.tap do |object_path|
PARTIAL_PATHS[ [path, prefix] ] ||= path.dup.tap do |object_path| merge_prefix_into_object_path(@context_prefix, object_path)
merge_prefix_into_object_path(prefix, object_path)
end end
end end


Expand Down

0 comments on commit d5eeacc

Please sign in to comment.