33require 'json'
44require 'bigdecimal'
55require 'set'
6+ require 'rdf/util/cache'
67begin
78 # Attempt to load this to avoid unnecessary context fetches
89 require 'json-ld-preloaded'
@@ -24,7 +25,7 @@ class Context
2425 ##
2526 # Defines the maximum number of interned URI references that can be held
2627 # cached in memory at any one time.
27- CACHE_SIZE = - 1 # unlimited by default
28+ CACHE_SIZE = 100 # unlimited by default
2829
2930 class << self
3031 ##
@@ -346,10 +347,18 @@ def self.parse(local_context, override_protected: false, propagate: true, **opti
346347 # @return [RDF::Util::Cache]
347348 # @private
348349 def self . cache
349- require 'rdf/util/cache' unless defined? ( ::RDF ::Util ::Cache )
350350 @cache ||= RDF ::Util ::Cache . new ( CACHE_SIZE )
351351 end
352352
353+ ##
354+ # Class-level cache inverse contexts.
355+ #
356+ # @return [RDF::Util::Cache]
357+ # @private
358+ def self . inverse_cache
359+ @inverse_cache ||= RDF ::Util ::Cache . new ( CACHE_SIZE )
360+ end
361+
353362 ##
354363 # Create new evaluation context
355364 # @param [Hash] options
@@ -1066,9 +1075,6 @@ def create_term_definition(local_context, term, defined,
10661075
10671076 term_definitions [ term ] = definition
10681077 defined [ term ] = true
1069- ensure
1070- # Re-build after term definitions set
1071- @inverse_context = nil
10721078 end
10731079
10741080 ##
@@ -1857,7 +1863,6 @@ def dup
18571863 memo . merge ( term => defn . dup ( ) )
18581864 end
18591865 @iri_to_term = that . iri_to_term . dup
1860- @inverse_context = nil
18611866 end
18621867 ec
18631868 end
@@ -1957,7 +1962,7 @@ def bnode(value = nil)
19571962 # @return [Hash{String => Hash{String => String}}]
19581963 # @todo May want to include @set along with container to allow selecting terms using @set over those without @set. May require adding some notion of value cardinality to compact_iri
19591964 def inverse_context
1960- @inverse_context ||= begin
1965+ Context . inverse_cache [ self . object_id ] ||= begin
19611966 result = { }
19621967 default_language = ( self . default_language || '@none' ) . downcase
19631968 term_definitions . keys . sort do |a , b |
0 commit comments