class Google::Protobuf::LegacyObjectCache
Public Class Methods
new()
click to toggle source
# File lib/google/protobuf/object_cache.rb, line 65 def initialize @secondary_map = {} @map = ObjectSpace::WeakMap.new @mutex = Mutex.new end
Public Instance Methods
get(key)
click to toggle source
# File lib/google/protobuf/object_cache.rb, line 71 def get(key) value = if secondary_key = @secondary_map[key] @map[secondary_key] else @mutex.synchronize do @map[(@secondary_map[key] ||= Object.new)] end end # GC if we could remove at least 2000 entries or 20% of the table size # (whichever is greater). Since the cost of the GC pass is O(N), we # want to make sure that we condition this on overall table size, to # avoid O(N^2) CPU costs. cutoff = (@secondary_map.size * 0.2).ceil cutoff = 2_000 if cutoff < 2_000 if (@secondary_map.size - @map.size) > cutoff purge end value end
try_add(key, value)
click to toggle source
# File lib/google/protobuf/object_cache.rb, line 93 def try_add(key, value) if secondary_key = @secondary_map[key] if old_value = @map[secondary_key] return old_value end end @mutex.synchronize do secondary_key ||= (@secondary_map[key] ||= Object.new) @map[secondary_key] ||= value end end
Private Instance Methods
purge()
click to toggle source
# File lib/google/protobuf/object_cache.rb, line 108 def purge @mutex.synchronize do @secondary_map.each do |key, secondary_key| unless @map.key?(secondary_key) @secondary_map.delete(key) end end end nil end