WeakRef Hash


#1

I need a Hash-like structure, using WeakRef, so that the key value
pairs can be garbage collected as needed. I only need to support []
() and []=(), not the whole range of Hash functions. If the pair has
been collected, I just need a simple nil returned.

I’ve tried implementing this a couple of different ways, but keep
running into trouble. Does anyone have any tips?

James Edward G. II


#2

On Jan 23, 2006, at 10:17 AM, James Edward G. II wrote:

I need a Hash-like structure, using WeakRef, so that the key value
pairs can be garbage collected as needed. I only need to support []
() and []=(), not the whole range of Hash functions. If the pair
has been collected, I just need a simple nil returned.

I’ve tried implementing this a couple of different ways, but keep
running into trouble. Does anyone have any tips?

The following code, adapted from an old post by Guy Decoux seems to
do the trick:

class WeakCache
def initialize( cache = Hash.new )
@cache = cache
end

def []( key )
   value_id = @cache[key]
   return ObjectSpace._id2ref(value_id) unless value_id.nil?
   nil
end

def []=( key, value )
   ObjectSpace.define_finalizer(value, lambda { @cache.delete

(key) })
@cache[key] = value.object_id
end
end

END

I’m still interested in seeing a WeakRefHash though, if anyone has
rolled something similar in the past…

James Edward G. II


#3

On Tue, Jan 24, 2006 at 03:02:04AM +0900, James Edward G. II wrote:

The following code, adapted from an old post by Guy Decoux seems to
do the trick:

class WeakCache
def initialize( cache = Hash.new )
@cache = cache
end
[…]
def []=( key, value )
ObjectSpace.define_finalizer(value, lambda { @cache.delete(key) })
==============================
This keeps a reference to value so it will never be reclaimed:

class WeakCache
attr_reader :cache
def initialize( cache = Hash.new )
@cache = cache
end

def
value_id = @cache[key]
return ObjectSpace._id2ref(value_id) unless value_id.nil?
nil
end

def []=( key, value )
ObjectSpace.define_finalizer(value, lambda { @cache.delete(key) })
@cache[key] = value.object_id
end
end

RUBY_VERSION # => “1.8.4”
RUBY_RELEASE_DATE # => “2005-12-24”
c = WeakCache.new
puts c.cache.size
100000.times{|i| c[i] = i.to_s}
GC.start
puts c.cache.size
END

>> 0

>> 100000

Compare to this:

class WeakCache
attr_reader :cache
def initialize( cache = Hash.new )
@cache = cache
end

def
value_id = @cache[key]
return ObjectSpace._id2ref(value_id) unless value_id.nil?
nil
end

def make_lambda(key)
lambda{|value| @cache.delete(key) }
end

def []=( key, value )
ObjectSpace.define_finalizer(value, make_lambda(key))
@cache[key] = value.object_id
end
end

RUBY_VERSION # => “1.8.4”
RUBY_RELEASE_DATE # => “2005-12-24”
c = WeakCache.new
puts c.cache.size
100000.times{|i| c[i] = i.to_s}
GC.start
puts c.cache.size
END

>> 0

>> 3

That is very inefficient though, you want something more like

class WeakCache
attr_reader :cache
def initialize( cache = Hash.new )
@cache = cache
@rev_cache = {}
@reclaim_method = method(:reclaim_value)
end

def
value_id = @cache[key]
return ObjectSpace._id2ref(value_id) unless value_id.nil?
nil
end

def reclaim_value(value_id)
@cache.delete @rev_cache[value_id]
@rev_cache.delete value_id
end

def []=( key, value )
@rev_cache[value.object_id] = key
@cache[key] = value.object_id
ObjectSpace.define_finalizer(value, @reclaim_method)
end
end

RUBY_VERSION # => “1.8.4”
RUBY_RELEASE_DATE # => “2005-12-24”
c = WeakCache.new
puts c.cache.size
100000.times{|i| c[i] = i.to_s}
GC.start
puts c.cache.size
END

>> 0

>> 4


#4

On Jan 23, 2006, at 6:01 PM, Mauricio F. wrote:

  ObjectSpace.define_finalizer(value, lambda { @cache.delete 

(key) })

==============================
This keeps a reference to value so it will never be reclaimed:

[snip sensational examples]

Thank you for the excellent lesson!

James Edward G. II


#5

James Edward G. II wrote:

I need a Hash-like structure, using WeakRef, so that the key value
pairs can be garbage collected as needed. I only need to support []
() and []=(), not the whole range of Hash functions. If the pair has
been collected, I just need a simple nil returned.

I’ve tried implementing this a couple of different ways, but keep
running into trouble. Does anyone have any tips?

Can’t you just do something like this?

require ‘delegate’
require ‘weakref’
WeakHash = DelegateClass(Hash)
class WeakHash
def []=(key,val)
getobj[WeakRef.new(key)] = WeakRef.new(val)
end

def
getobj[WeakRef.new(key)]
end

def each(&b)
getobj.each do |k,v|
b[k.getobj, v.getobj] unless k.getobj.nil?
end
self
end

def cleanup
delete_if {|k,v| k.getobj.nil?}
end
end

Kind regards

robert

#6

On Jan 24, 2006, at 9:51 AM, Robert K. wrote:

def
def cleanup
delete_if {|k,v| k.getobj.nil?}
end
end

I didn’t see this message while the gateway was down. Sorry about that.

I can’t use this code as is. I assume you didn’t set up delegation
quite right:

class WeakHash < DelegateClass(Hash)
def initialize
super(Hash.new)
end

end

Some questions the above raises for me:

  1. What will Ruby do if a key is GCed, but not a value?
  2. How does this code deal with a value being GCed when the key
    remains?
  3. Don’t we need to shut off GC in places, to keep references from
    disappearing before we can use them?

Thanks for the help.

James Edward G. II


#7

Mauricio F. wrote:

end

  @reclaim_method = method(:reclaim_value)
 @rev_cache.delete value_id

RUBY_RELEASE_DATE # => “2005-12-24”
c = WeakCache.new
puts c.cache.size
100000.times{|i| c[i] = i.to_s}
GC.start
puts c.cache.size
END

>> 0

>> 4

I like your technique, but suppose the same value is stored for more
than one key in a WeakCache? When the value is finalized, only the last
key will be removed from the cache. I think it might be better to let
the reverse cache hold more than one key per value.


#8

On Wed, Jan 25, 2006 at 12:58:04AM +0900, Jeffrey S. wrote:

end
[…]
I like your technique, but suppose the same value is stored for more
than one key in a WeakCache? When the value is finalized, only the last
key will be removed from the cache. I think it might be better to let
the reverse cache hold more than one key per value.

You’re very right, I should have written something like

class WeakCache
attr_reader :cache
def initialize( cache = Hash.new )
@cache = cache
@rev_cache = Hash.new{|h,k| h[k] = {}}
@reclaim_lambda = lambda do |value_id|
if @rev_cache.has_key? value_id
@rev_cache[value_id].each_key{|key| @cache.delete key}
@rev_cache.delete value_id
end
end
end

def
value_id = @cache[key]
return ObjectSpace._id2ref(value_id) unless value_id.nil?
nil
end

def []=( key, value )
@rev_cache[value.object_id][key] = true
@cache[key] = value.object_id
ObjectSpace.define_finalizer(value, @reclaim_lambda)
end
end

puts “=” * 20
c = WeakCache.new
puts c.cache.size
100000.times{|i| c[i] = (i % 1000).to_s}
GC.start
puts c.cache.size
puts “=” * 20

>> ====================

>> 0

>> 3

>> ====================

(not thread-safe yet though)


#9

2006/1/24, James Edward G. II removed_email_address@domain.invalid:

end
end

def cleanup
delete_if {|k,v| k.getobj.nil?}
end
end

I didn’t see this message while the gateway was down. Sorry about that.

I can’t use this code as is. I assume you didn’t set up delegation
quite right:

What problem exactly do you have with it?

class WeakHash < DelegateClass(Hash)
def initialize
super(Hash.new)
end

end

No need to inherit to fix this. You can simply do

require ‘delegate’
Wh=DelegateClass(Hash)
class Wh
def initialize(h={})
setobj h
end
end

Some questions the above raises for me:

  1. What will Ruby do if a key is GCed, but not a value?

This was just rough outline code. For a production version I’d
probably change it to consider a pair as gone if either of them is
GC’ed.

  1. How does this code deal with a value being GCed when the key
    remains?

It will yield nil - but see 1.

  1. Don’t we need to shut off GC in places, to keep references from
    disappearing before we can use them?

No. Because while the yield takes place instances are hard referenced
and cannot be gc’ed.

Thanks for the help.

Ywc

Kind regards

robert


#10

2006/1/24, James Edward G. II removed_email_address@domain.invalid:

manually though.
Yep.

require ‘delegate’
Wh=DelegateClass(Hash)
class Wh
def initialize(h={})
setobj h
end
end

Here’s another question for you: What are we gaining by delegating
to an object here, as opposed to subclassing?

Note, the difference between yours and mine was that you first
delegated and then subclassed the delegating class. I’d say do either
of both in this case but not both. Generally I’d prefer delegation in
this use case because we really have a case of wrapping and unwrapping
during insertion and retrieval ops. IMHO inheritance is often used in
many cases where it’s not appropriate (although in this case you could
argue that a WeakHash is really a Hash - but then again, it doesn’t
share some of Hash’s basic properties, namely that the contents do not
change suddenly). There’s no hard written rule to be found here.

It will yield nil - but see 1.

  1. Don’t we need to shut off GC in places, to keep references from
    disappearing before we can use them?

No. Because while the yield takes place instances are hard referenced
and cannot be gc’ed.

Good answers all around. Thanks.

In fact to be really sure, one should probably do something like this:

def each
getobj.each do |wk,wv|
k,v=wk.getobj, wv.getobj
yield unless k.nil? || v.nil?
end
end

Cheers

robert


#11

On Jan 24, 2006, at 1:06 PM, Robert K. wrote:

I can’t use this code as is. I assume you didn’t set up delegation
quite right:

What problem exactly do you have with it?

When I placed the code you posted in a file and tried to create
WeahHash, the program crashed. (Wrong number of arguments to
initialize().) I assume it would have worked if I passed the Hash
manually though.

require ‘delegate’
Wh=DelegateClass(Hash)
class Wh
def initialize(h={})
setobj h
end
end

Here’s another question for you: What are we gaining by delegating
to an object here, as opposed to subclassing?

It will yield nil - but see 1.

  1. Don’t we need to shut off GC in places, to keep references from
    disappearing before we can use them?

No. Because while the yield takes place instances are hard referenced
and cannot be gc’ed.

Good answers all around. Thanks.

James Edward G. II


#12

2006/1/24, Mauricio F. removed_email_address@domain.invalid:

I summarized the solutions seen in this thread so far, and presented a couple
new ones at
http://eigenclass.org/hiki.rb?weakhash+and+weakref

There are other ways to implement WeakHash, I’ll probably add some to the
above page (later).

Nice work. Btw, it’s expected that my lookup code is slow because it
has to create a weakref for every insertion. We could try to optimize
this by providing a single weakref for the hash that is used for
lookups.

class WR < WeakRef
attr_accessor :getobj
end

class RKWeakHash
def initialize(…)
@lookup = WR.new nil
end

def
@lookup.getobj = x
getobj[@lookup]
end
end

Unfortunately this is not thread safe, unless one invests more and
stores lookup thread local…

robert


#13

On Tue, Jan 24, 2006 at 01:17:07AM +0900, James Edward G. II wrote:

I need a Hash-like structure, using WeakRef, so that the key value
pairs can be garbage collected as needed. I only need to support []
() and []=(), not the whole range of Hash functions. If the pair has
been collected, I just need a simple nil returned.

I’ve tried implementing this a couple of different ways, but keep
running into trouble. Does anyone have any tips?

I summarized the solutions seen in this thread so far, and presented a
couple
new ones at
http://eigenclass.org/hiki.rb?weakhash+and+weakref

There are other ways to implement WeakHash, I’ll probably add some to
the
above page (later).

HTH


#14

On Jan 24, 2006, at 2:47 PM, Robert K. wrote:

Note, the difference between yours and mine was that you first
delegated and then subclassed the delegating class.

My usage was straight out of the delegate docs:

http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html

Technically, I wrote those docs, but I certainly didn’t invent that
idiom. I mainly copied it from Matz’s usage in TempFile. (I believe
he wrote that, my apologies if I mis-credited there.)

James Edward G. II