Skip to content

Commit 1a33a60

Browse files
committed
Delay initialization of keyword arg default value to reduce object allocations.
1 parent 0381746 commit 1a33a60

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lib/json/ld/context.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ def create_term_definition(local_context, term, defined)
655655
(simple_term || ((processingMode || 'json-ld-1.0') == 'json-ld-1.0'))
656656
elsif term.include?(':')
657657
# 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.
658-
prefix, suffix = term.split(':')
658+
prefix, suffix = term.split(':', 2)
659659
create_term_definition(local_context, prefix, defined) if local_context.has_key?(prefix)
660660

661661
definition.id = if td = term_definitions[prefix]
@@ -980,12 +980,14 @@ def reverse_term(term)
980980
# IRI or String, if it's a keyword
981981
# @raise [JSON::LD::JsonLdError::InvalidIRIMapping] if the value cannot be expanded
982982
# @see http://json-ld.org/spec/latest/json-ld-api/#iri-expansion
983-
def expand_iri(value, documentRelative: false, vocab: false, local_context: nil, defined: {}, quiet: false, **options)
983+
def expand_iri(value, documentRelative: false, vocab: false, local_context: nil, defined: nil, quiet: false, **options)
984984
return value unless value.is_a?(String)
985985

986986
return value if KEYWORDS.include?(value)
987987
#log_debug("expand_iri") {"value: #{value.inspect}"} unless quiet
988988

989+
defined = defined || {} # if we initialized in the keyword arg we would allocate {} at each invokation, even in the 2 (common) early returns above.
990+
989991
# If local context is not null, it contains a key that equals value, and the value associated with the key that equals value in defined is not true, then invoke the Create Term Definition subalgorithm, passing active context, local context, value as term, and defined. This will ensure that a term definition is created for value in active context during Context Processing.
990992
if local_context && local_context.has_key?(value) && !defined[value]
991993
create_term_definition(local_context, value, defined)

0 commit comments

Comments
 (0)