Skip to content

Commit c9e4542

Browse files
committed
Optimize Context memory usage
1 parent 072c89d commit c9e4542

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

lib/json/ld/context.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,10 +1544,10 @@ def compact_iri(iri, base: nil, reverse: false, value: nil, vocab: nil)
15441544
candidates = []
15451545

15461546
term_definitions.each do |term, td|
1547-
next if td.nil? || td.id.nil? || td.id == iri || !iri.start_with?(td.id)
1548-
15491547
# Skip term if `@prefix` is not true in term definition
1550-
next unless td.prefix?
1548+
next unless td&.prefix?
1549+
1550+
next if td&.id.nil? || td.id == iri || !td.match_iri?(iri)
15511551

15521552
suffix = iri[td.id.length..]
15531553
ciri = "#{term}:#{suffix}"
@@ -1572,7 +1572,7 @@ def compact_iri(iri, base: nil, reverse: false, value: nil, vocab: nil)
15721572

15731573
# If iri could be confused with a compact IRI using a term in this context, signal an error
15741574
term_definitions.each do |term, td|
1575-
next unless iri.to_s.start_with?("#{term}:") && td.prefix?
1575+
next unless td.prefix? && td.match_compact_iri?(iri)
15761576

15771577
raise JSON::LD::JsonLdError::IRIConfusedWithPrefix, "Absolute IRI '#{iri}' confused with prefix '#{term}'"
15781578
end
@@ -2204,6 +2204,22 @@ def protected?
22042204
!!@protected
22052205
end
22062206

2207+
# Returns true if the term matches a IRI
2208+
#
2209+
# @param iri [String] the IRI
2210+
# @return [Boolean]
2211+
def match_iri?(iri)
2212+
iri.start_with?(id)
2213+
end
2214+
2215+
# Returns true if the term matches a compact IRI
2216+
#
2217+
# @param iri [String] the compact IRI
2218+
# @return [Boolean]
2219+
def match_compact_iri?(iri)
2220+
iri.start_with?(prefix_colon)
2221+
end
2222+
22072223
# Set container mapping, from an array which may include @set
22082224
def container_mapping=(mapping)
22092225
mapping = case mapping
@@ -2328,6 +2344,12 @@ def inspect
23282344
v << "has-context" unless context.nil?
23292345
v.join(" ") + "]"
23302346
end
2347+
2348+
private
2349+
2350+
def prefix_colon
2351+
@prefix_colon ||= "#{term}:".freeze
2352+
end
23312353
end
23322354
end
23332355
end

0 commit comments

Comments
 (0)