From: Andrew Green (ag_at_[hidden])
Date: 2001-01-09 11:05:19
David Abrahams wrote:
> Actually, what I've mostly wanted was a way to keep garbage-collectable data
> in a kind of cache without preventing it from being collected. This is only
> loosely related to weak_ptr.
One technique I've had success with is modifying the behavior of a
counted_ptr so that it invokes an appropriate 'finalize' function, in my
case usually a virtual method on the object in question, but that's not
crucial to the idea. The finalize method can just delete the now
un-referenced object, or it can move it into a cache.
This does require that the there's some kind of non pointer-based abstract
name for an object, say a record or node ID, which is turned into a
counted_ptr to a real object by a factory, which keeps referenced objects in
one list, and recently used but unreferenced objects in a separate list.
It's great for walking disk-resident B-Trees, where you simply let the (C++
call) stack maintain counted_ptrs to parent nodes as you recurse, and the
cache is somewhat larger than the maximum depth of the tree. For my purposes
a scheme as simple as this has a hit rate of over 80% for the fairly random
accesses I see, and multiple threads all walking the tree at once benefit
from one another's I/O.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk