// A Pool is a set of allocated objects to be reused.
// It may be used by multiple goroutines simultaneously.
// The Pool is drained during garbage collection.
type Pool// Put adds x to the pool.func (p *Pool) Put(x interface{})// Get removes and returns a value from the pool.// If the pool is empty, Get returns nil.func (p *Pool) Get() interface{}
Looks pretty reasonable to me. One thing which seems to not be a partof this proposal is: How would pools of different types (pools holding
instances of different types) be declared? Can you please clarify that
detail? TIA.
SGTM
--
---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
To clarify this for myself, I think what you are saying is: you canadd anything to a Pool using Put. Get will return some element placed
in the Pool by Put. The Pool retains the values added via Put. At
garbage collection time, those retained values will be discarded. If
those values contain pointers, and nothing else points to the objects
to which they point, those objects will be collected as usual.
* Russ Cox:
Finalizers require this as well and are closely related.
> Note that this proposal is much simpler than weak pointers. A weak pointer
> is only reset to nil during a collection when no ordinary ("strong")
> pointers point to the same object. That definition effectively requires a
> two pass collector: one pass to find the memory to free and then a second
> pass to zero the corresponding weak pointers.
The most direct way to implement the draining is to maintain a list of non-empty pools. A call to Put on an empty pool adds the pool to this list. At the start of a collection, the garbage collector drains all pools on the list and then clears the list. Since the collection ends with all pools empty, the work is bounded by the number of pool operations since the last collection.
Note that this proposal is much simpler than weak pointers. A weak pointer is only reset to nil during a collection when no ordinary ("strong") pointers point to the same object. That definition effectively requires a two pass collector: one pass to find the memory to free and then a second pass to zero the corresponding weak pointers. Pools are drained during collection regardless of the rest of memory. In general a pointer in a pools should not be in use elsewhere in memory (or else it is a bug to reuse it), so the expensive weak pointer condition would add no benefit to a pool implementation.
Thoughts?
This typically does interact well with partial garbage collection (such as a generational collector). Partial garbage
I would avoid adding an API like this. It is better to implement a cache eviction policy based on the amount of storage being retained by the cache and not involve the collector. You'll need a different API and different signals, but it's easier for users to reason about.