Merge pull request #48163 from Shopify/view-lookup-alloc

This commit is contained in:
Jean Boussier 2023-05-08 04:25:35 +02:00 committed by GitHub
commit 55da913c41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 70 additions and 43 deletions

@ -73,6 +73,6 @@ def self.eager_load!
ActiveSupport.on_load(:action_view) do
ActionView::Base.default_formats ||= Mime::SET.symbols
ActionView::Template::Types.delegate_to Mime
ActionView::Template.mime_types_implementation = Mime
ActionView::LookupContext::DetailsKey.clear
end

@ -147,6 +147,6 @@ def eager_load!
ActiveSupport.on_load(:action_view) do
ActionView::Base.default_formats ||= Mime::SET.symbols
ActionView::Template::Types.delegate_to Mime
ActionView::Template.mime_types_implementation = Mime
ActionView::LookupContext::DetailsKey.clear
end

@ -11,6 +11,7 @@ class Mimes
def initialize
@mimes = []
@symbols = []
@symbols_set = Set.new
end
def each(&block)
@ -19,17 +20,25 @@ def each(&block)
def <<(type)
@mimes << type
@symbols << type.to_sym
sym_type = type.to_sym
@symbols << sym_type
@symbols_set << sym_type
end
def delete_if
@mimes.delete_if do |x|
if yield x
@symbols.delete(x.to_sym)
sym_type = x.to_sym
@symbols.delete(sym_type)
@symbols_set.delete(sym_type)
true
end
end
end
def valid_symbols?(symbols) # :nodoc
symbols.all? { |s| @symbols_set.include?(s) }
end
end
SET = Mimes.new
@ -42,6 +51,14 @@ def [](type)
Type.lookup_by_extension(type)
end
def symbols
SET.symbols
end
def valid_symbols?(symbols) # :nodoc:
SET.valid_symbols?(symbols)
end
def fetch(type, &block)
return type if type.is_a?(Type)
EXTENSION_LOOKUP.fetch(type.to_s, &block)

@ -63,11 +63,15 @@ def self.digest_cache(details)
end
def self.details_cache_key(details)
if details[:formats]
details = details.dup
details[:formats] &= Template::Types.symbols
@details_keys.fetch(details) do
if formats = details[:formats]
unless Template::Types.valid_symbols?(formats)
details = details.dup
details[:formats] &= Template::Types.symbols
end
end
@details_keys[details] ||= TemplateDetails::Requested.new(**details)
end
@details_keys[details] ||= TemplateDetails::Requested.new(**details)
end
def self.clear
@ -262,7 +266,7 @@ def formats=(values)
values.concat(default_formats) if values.delete "*/*"
values.uniq!
unless values.all? { |v| Template::Types.symbols.include?(v) }
unless Template::Types.valid_symbols?(values)
invalid_values = values - Template::Types.symbols
raise ArgumentError, "Invalid formats: #{invalid_values.map(&:inspect).join(", ")}"
end

@ -109,6 +109,7 @@ class Template
autoload :Handlers
autoload :HTML
autoload :Inline
autoload :Types
autoload :Sources
autoload :Text
autoload :Types
@ -119,6 +120,17 @@ class Template
singleton_class.attr_accessor :frozen_string_literal
@frozen_string_literal = false
class << self # :nodoc:
def mime_types_implementation=(implementation)
# This method isn't thread-safe, but it's not supposed
# to be called after initialization
if self::Types != implementation
remove_const(:Types)
const_set(:Types, implementation)
end
end
end
attr_reader :identifier, :handler
attr_reader :variable, :format, :variant, :virtual_path

@ -4,11 +4,14 @@
module ActionView
class Template # :nodoc:
module Types
class Type
SET = Struct.new(:symbols).new([ :html, :text, :js, :css, :xml, :json ])
# SimpleType is mostly just a stub implementation for when Action View
# is used without Action Dispatch.
class SimpleType # :nodoc:
@symbols = [ :html, :text, :js, :css, :xml, :json ]
class << self
attr_reader :symbols
def self.[](type)
def [](type)
if type.is_a?(self)
type
else
@ -16,41 +19,32 @@ def self.[](type)
end
end
attr_reader :symbol
def initialize(symbol)
@symbol = symbol.to_sym
end
def to_s
@symbol.to_s
end
alias to_str to_s
def ref
@symbol
end
alias to_sym ref
def ==(type)
@symbol == type.to_sym unless type.blank?
def valid_symbols?(symbols) # :nodoc
symbols.all? { |s| @symbols.include?(s) }
end
end
class << self
attr_reader :symbols
attr_reader :symbol
def delegate_to(klass)
@symbols = klass::SET.symbols
@type_klass = klass
end
def [](type)
@type_klass[type]
end
def initialize(symbol)
@symbol = symbol.to_sym
end
delegate_to Type
def to_s
@symbol.to_s
end
alias to_str to_s
def ref
@symbol
end
alias to_sym ref
def ==(type)
@symbol == type.to_sym unless type.blank?
end
end
Types = SimpleType # :nodoc:
end
end

@ -4,7 +4,7 @@
require "active_support/ordered_options"
require "action_dispatch"
ActionView::Template::Types.delegate_to Mime
ActionView::Template.mime_types_implementation = Mime
module AssetTagHelperTestHelpers
def with_preload_links_header(new_preload_links_header = true)