mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Store requested detail indexes in hashes
This improves the performance of matching and sorting TemplateDetails based on Hash#[] being faster than Array#include? and Array#index.
This commit is contained in:
parent
a8a54a1821
commit
eac19fea2b
1 changed files with 27 additions and 28 deletions
|
@ -4,13 +4,30 @@ module ActionView
|
||||||
class TemplateDetails # :nodoc:
|
class TemplateDetails # :nodoc:
|
||||||
class Requested
|
class Requested
|
||||||
attr_reader :locale, :handlers, :formats, :variants
|
attr_reader :locale, :handlers, :formats, :variants
|
||||||
|
attr_reader :locale_idx, :handlers_idx, :formats_idx, :variants_idx
|
||||||
|
|
||||||
|
ANY_HASH = Hash.new(1).merge(nil => 0).freeze
|
||||||
|
|
||||||
def initialize(locale:, handlers:, formats:, variants:)
|
def initialize(locale:, handlers:, formats:, variants:)
|
||||||
@locale = locale
|
@locale = locale
|
||||||
@handlers = handlers
|
@handlers = handlers
|
||||||
@formats = formats
|
@formats = formats
|
||||||
@variants = variants
|
@variants = variants
|
||||||
|
|
||||||
|
@locale_idx = build_idx_hash(locale)
|
||||||
|
@handlers_idx = build_idx_hash(handlers)
|
||||||
|
@formats_idx = build_idx_hash(formats)
|
||||||
|
if variants == :any
|
||||||
|
@variants_idx = ANY_HASH
|
||||||
|
else
|
||||||
|
@variants_idx = build_idx_hash(variants)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def build_idx_hash(arr)
|
||||||
|
[*arr, nil].each_with_index.to_h.freeze
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :locale, :handler, :format, :variant
|
attr_reader :locale, :handler, :format, :variant
|
||||||
|
@ -23,28 +40,19 @@ module ActionView
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches?(requested)
|
def matches?(requested)
|
||||||
return if format && !requested.formats.include?(format)
|
requested.formats_idx[@format] &&
|
||||||
return if locale && !requested.locale.include?(locale)
|
requested.locale_idx[@locale] &&
|
||||||
unless requested.variants == :any
|
requested.variants_idx[@variant] &&
|
||||||
return if variant && !requested.variants.include?(variant)
|
requested.handlers_idx[@handler]
|
||||||
end
|
|
||||||
return if handler && !requested.handlers.include?(handler)
|
|
||||||
|
|
||||||
true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def sort_key_for(requested)
|
def sort_key_for(requested)
|
||||||
locale_match = details_match_sort_key(locale, requested.locale)
|
[
|
||||||
format_match = details_match_sort_key(format, requested.formats)
|
requested.formats_idx[@format],
|
||||||
variant_match =
|
requested.locale_idx[@locale],
|
||||||
if requested.variants == :any
|
requested.variants_idx[@variant],
|
||||||
variant ? 1 : 0
|
requested.handlers_idx[@handler]
|
||||||
else
|
]
|
||||||
details_match_sort_key(variant, requested.variants)
|
|
||||||
end
|
|
||||||
handler_match = details_match_sort_key(handler, requested.handlers)
|
|
||||||
|
|
||||||
[locale_match, format_match, variant_match, handler_match]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def handler_class
|
def handler_class
|
||||||
|
@ -54,14 +62,5 @@ module ActionView
|
||||||
def format_or_default
|
def format_or_default
|
||||||
format || handler_class.try(:default_format)
|
format || handler_class.try(:default_format)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
def details_match_sort_key(have, want)
|
|
||||||
if have
|
|
||||||
want.index(have)
|
|
||||||
else
|
|
||||||
want.size
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue