Skip to content

Commit ed40a6a

Browse files
jpervillegkellogg
authored andcommitted
Reduce object allocations in JSON::LD::Context#create_term_definition
1 parent 99c3002 commit ed40a6a

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

lib/json/ld/context.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ def merge!(context)
527527
self
528528
end
529529

530+
# The following constants are used to reduce object allocations in #create_term_definition below
531+
ID_NULL_OBJECT = { '@id' => nil }.freeze
532+
JSON_LD_10_EXPECTED_KEYS = Set.new(%w(@container @id @language @reverse @type)).freeze
533+
JSON_LD_EXPECTED_KEYS = Set.new(%w(@container @context @id @language @nest @prefix @reverse @type)).freeze
534+
530535
##
531536
# Create Term Definition
532537
#
@@ -570,7 +575,7 @@ def create_term_definition(local_context, term, defined)
570575
value = {'@id' => value} if simple_term
571576

572577
case value
573-
when nil, {'@id' => nil}
578+
when nil, ID_NULL_OBJECT
574579
# If value equals null or value is a JSON object containing the key-value pair (@id-null), then set the term definition in active context to null, set the value associated with defined's key term to true, and return.
575580
#log_debug("") {"=> nil"}
576581
term_definitions[term] = TermDefinition.new(term)
@@ -581,14 +586,16 @@ def create_term_definition(local_context, term, defined)
581586
definition = TermDefinition.new(term)
582587
definition.simple = simple_term
583588

584-
expected_keys = case processingMode
585-
when "json-ld-1.0", nil then %w(@container @id @language @reverse @type)
586-
else %w(@container @context @id @language @nest @prefix @reverse @type)
587-
end
589+
if options[:validate]
590+
expected_keys = case processingMode
591+
when "json-ld-1.0", nil then JSON_LD_10_EXPECTED_KEYS
592+
else JSON_LD_EXPECTED_KEYS
593+
end
588594

589-
extra_keys = value.keys - expected_keys
590-
if !extra_keys.empty? && @options[:validate]
591-
raise JsonLdError::InvalidTermDefinition, "Term definition for #{term.inspect} has unexpected keys: #{extra_keys.join(', ')}"
595+
if value.any? { |key, _| !expected_keys.include?(key) }
596+
extra_keys = value.keys - expected_keys.to_a
597+
raise JsonLdError::InvalidTermDefinition, "Term definition for #{term.inspect} has unexpected keys: #{extra_keys.join(', ')}"
598+
end
592599
end
593600

594601
if value.has_key?('@type')
@@ -650,7 +657,7 @@ def create_term_definition(local_context, term, defined)
650657

651658
# If id ends with a gen-delim, it may be used as a prefix for simple terms
652659
definition.prefix = true if !term.include?(':') &&
653-
definition.id.to_s.end_with?(*%w(: / ? # [ ] @)) &&
660+
definition.id.to_s.end_with?(':', '/', '?', '#', '[', ']', '@') &&
654661
simple_term
655662
elsif term.include?(':')
656663
# If term is a compact IRI with a prefix that is a key in local context then a dependency has been found. Use this algorithm recursively passing active context, local context, the prefix as term, and defined.

0 commit comments

Comments
 (0)