Skip to content

Commit

Permalink
Merge pull request #312 from rails/schneems/hash-metadata-lookup
Browse files Browse the repository at this point in the history
Prefer Hash#[] over Set#.include? for speed
  • Loading branch information
schneems authored Jun 17, 2016
2 parents 0860f26 + f032821 commit 88fca20
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/sprockets/processor_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ def processors_cache_keys(processors)
Set
]).freeze

# Internal: Hash of all "simple" value types allowed to be returned in
# processor metadata.
VALID_METADATA_VALUE_TYPES_HASH = VALID_METADATA_VALUE_TYPES.each_with_object({}) do |type, hash|
hash[type] = true
end.freeze

# Internal: Hash of all nested compound metadata types that can nest values.
VALID_METADATA_COMPOUND_TYPES_HASH = VALID_METADATA_COMPOUND_TYPES.each_with_object({}) do |type, hash|
hash[type] = true
end.freeze

# Internal: Set of all allowed metadata types.
VALID_METADATA_TYPES = (VALID_METADATA_VALUE_TYPES + VALID_METADATA_COMPOUND_TYPES).freeze

Expand Down Expand Up @@ -168,9 +179,9 @@ def validate_processor_result!(result)
#
# Returns true if class is in whitelist otherwise false.
def valid_processor_metadata_value?(value)
if VALID_METADATA_VALUE_TYPES.include?(value.class)
if VALID_METADATA_VALUE_TYPES_HASH[value.class]
true
elsif VALID_METADATA_COMPOUND_TYPES.include?(value.class)
elsif VALID_METADATA_COMPOUND_TYPES_HASH[value.class]
value.all? { |v| valid_processor_metadata_value?(v) }
else
false
Expand Down

0 comments on commit 88fca20

Please sign in to comment.