Sha256: 46ca65ca140b0a9768fd336fc212cefdebc303cb7c99d7bbafe2bbd7c6e77c40

Contents?: true

Size: 687 Bytes

Versions: 1

Compression:

Stored size: 687 Bytes

Contents

class TinyStruct
  # Maintains an in-memory cache of the defined `TinyStruct` classes, such that
  # classes with the same attribute list do not end up getting created twice.
  #
  # This cache makes performance quite a bit better since looping through
  # `ObjectSpace` takes a while, but takes a hit on memory because everything
  # is now stored. Also prevents the classes from being freed if they were
  # created anonymously and could have otherwise have been freed.
  class MemoryCache
    attr_reader :cache

    def initialize
      @cache = {}
    end

    def []=(members, clazz)
      cache[members] = clazz
    end

    def [](members)
      cache[members]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tiny_struct-0.0.1 lib/tiny_struct/memory_cache.rb