Skip to content

Commit 7ad3a67

Browse files
jpervillegkellogg
authored andcommitted
Reduce object allocations in JSON::LD::Expand#expand
1 parent ed40a6a commit 7ad3a67

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

lib/json/ld/expand.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
347347
term_context = context.term_definitions[key].context if context.term_definitions[key]
348348
active_context = term_context ? context.parse(term_context) : context
349349
container = active_context.container(key)
350-
expanded_value = if container == %w(@language) && value.is_a?(Hash)
350+
expanded_value = if container.length == 1 && container.first == '@language' && value.is_a?(Hash)
351351
# Otherwise, if key's container mapping in active context is @language and value is a JSON object then value is expanded from a language map as follows:
352352

353353
# Set multilingual array to an empty array.
@@ -370,7 +370,7 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
370370
end
371371

372372
ary
373-
elsif !(CONTAINER_MAPPING_INDEX_ID_TYPE & container).empty? && value.is_a?(Hash)
373+
elsif container.any? { |key| CONTAINER_MAPPING_INDEX_ID_TYPE.include?(key) } && value.is_a?(Hash)
374374
# Otherwise, if key's container mapping in active context contains @index, @id, @type and value is a JSON object then value is expanded from an index map as follows:
375375

376376
# Set ary to an empty array.
@@ -426,14 +426,14 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
426426
#log_debug {" => #{expanded_value.inspect}"}
427427

428428
# If the container mapping associated to key in active context is @list and expanded value is not already a list object, convert expanded value to a list object by first setting it to an array containing only expanded value if it is not already an array, and then by setting it to a JSON object containing the key-value pair @list-expanded value.
429-
if active_context.container(key) == %w(@list) && !list?(expanded_value)
429+
if container.first == '@list' && container.length == 1 && !list?(expanded_value)
430430
#log_debug(" => ") { "convert #{expanded_value.inspect} to list"}
431431
expanded_value = {'@list' => as_array(expanded_value)}
432432
end
433433
#log_debug {" => #{expanded_value.inspect}"}
434434

435435
# convert expanded value to @graph if container specifies it
436-
if active_context.container(key) == %w(@graph)
436+
if container.first == '@graph' && container.length == 1
437437
#log_debug(" => ") { "convert #{expanded_value.inspect} to list"}
438438
expanded_value = as_array(expanded_value).map do |v|
439439
graph?(v) ? v : {'@graph' => as_array(v)}
@@ -457,7 +457,14 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
457457
else
458458
# Otherwise, if key is not a reverse property:
459459
# If result does not have an expanded property member, create one and initialize its value to an empty array.
460-
(output_object[expanded_property] ||= []).concat([expanded_value].flatten)
460+
(output_object[expanded_property] ||= []).tap do |memo|
461+
# expanded_value is either Array[Hash] or Hash; in both case append to memo without flatten
462+
if expanded_value.is_a?(Array)
463+
memo.concat(expanded_value)
464+
else # Hash
465+
memo << expanded_value
466+
end
467+
end
461468
end
462469
end
463470

0 commit comments

Comments
 (0)