|
| 1 | +package bart |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/netip" |
| 5 | + "sync" |
| 6 | + "sync/atomic" |
| 7 | +) |
| 8 | + |
| 9 | +// multiPool groups sub-pools for internal node, leaf, and fringe types. |
| 10 | +// Each sub-multiPool handles allocation, reuse, and statistics tracking |
| 11 | +// for its corresponding node type. |
| 12 | +type multiPool[V any] struct { |
| 13 | + node *nodePool[V] |
| 14 | + leaf *leafPool[V] |
| 15 | + fringe *fringePool[V] |
| 16 | +} |
| 17 | + |
| 18 | +// newMultiPool initializes and returns a new pool structure with sub-pools |
| 19 | +// for internal, leaf, and fringe nodes. |
| 20 | +func newMultiPool[V any]() *multiPool[V] { |
| 21 | + return &multiPool[V]{ |
| 22 | + node: newNodePool[V](), |
| 23 | + leaf: newLeafPool[V](), |
| 24 | + fringe: newFringePool[V](), |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// getNode obtains a *node[V] from the pool. |
| 29 | +// If the parent pool is nil, a new instance is returned without tracking. |
| 30 | +func (mp *multiPool[V]) getNode() *node[V] { |
| 31 | + if mp == nil { |
| 32 | + return new(node[V]) |
| 33 | + } |
| 34 | + mp.node.currentLive.Add(1) |
| 35 | + return mp.node.Get().(*node[V]) |
| 36 | +} |
| 37 | + |
| 38 | +// getLeaf obtains a *leafNode[V] from the pool, initialized with |
| 39 | +// a prefix and value. If the pool is nil, a fresh instance is created. |
| 40 | +func (mp *multiPool[V]) getLeaf(pfx netip.Prefix, val V) *leafNode[V] { |
| 41 | + if mp == nil { |
| 42 | + return &leafNode[V]{prefix: pfx, value: val} |
| 43 | + } |
| 44 | + mp.leaf.currentLive.Add(1) |
| 45 | + l := mp.leaf.Get().(*leafNode[V]) |
| 46 | + l.prefix = pfx |
| 47 | + l.value = val |
| 48 | + return l |
| 49 | +} |
| 50 | + |
| 51 | +// getFringe obtains a *fringeNode[V] from the pool, initialized with a value. |
| 52 | +// If the pool is nil, a new instance is returned without tracking. |
| 53 | +func (mp *multiPool[V]) getFringe(val V) *fringeNode[V] { |
| 54 | + if mp == nil { |
| 55 | + return &fringeNode[V]{value: val} |
| 56 | + } |
| 57 | + mp.fringe.currentLive.Add(1) |
| 58 | + f := mp.fringe.Get().(*fringeNode[V]) |
| 59 | + f.value = val |
| 60 | + return f |
| 61 | +} |
| 62 | + |
| 63 | +// putNode returns an internal node back to its pool for reuse. |
| 64 | +// If the pool is nil, the node is discarded. |
| 65 | +func (mp *multiPool[V]) putNode(n *node[V]) { |
| 66 | + if mp != nil { |
| 67 | + n.reset() // clear internal state but keep allocated memory |
| 68 | + mp.node.currentLive.Add(-1) |
| 69 | + mp.node.Put(n) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// putLeaf returns a leaf node back to its pool for reuse. |
| 74 | +// If the pool is nil, the node is discarded. |
| 75 | +func (mp *multiPool[V]) putLeaf(l *leafNode[V]) { |
| 76 | + if mp != nil { |
| 77 | + mp.leaf.currentLive.Add(-1) |
| 78 | + mp.leaf.Put(l) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// putFringe returns a fringe node back to its pool for reuse. |
| 83 | +// If the pool is nil, the node is discarded. |
| 84 | +func (mp *multiPool[V]) putFringe(f *fringeNode[V]) { |
| 85 | + if mp != nil { |
| 86 | + mp.fringe.currentLive.Add(-1) |
| 87 | + mp.fringe.Put(f) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// nodeStats returns the number of currently live (checked-out) nodes |
| 92 | +// and the total number of *node[V] objects ever allocated by this pool. |
| 93 | +func (mp *multiPool[V]) nodeStats() (live int64, total int64) { |
| 94 | + if mp == nil { |
| 95 | + return 0, 0 |
| 96 | + } |
| 97 | + return mp.node.currentLive.Load(), mp.node.totalAllocated.Load() |
| 98 | +} |
| 99 | + |
| 100 | +// leafStats returns the current number of in-use leaf nodes and |
| 101 | +// the total number created across the pool's lifetime. |
| 102 | +func (mp *multiPool[V]) leafStats() (live int64, total int64) { |
| 103 | + if mp == nil { |
| 104 | + return 0, 0 |
| 105 | + } |
| 106 | + return mp.leaf.currentLive.Load(), mp.leaf.totalAllocated.Load() |
| 107 | +} |
| 108 | + |
| 109 | +// leafStats returns the current number of in-use fringe nodes and |
| 110 | +// the total number created across the pool's lifetime. |
| 111 | +func (mp *multiPool[V]) fringeStats() (live int64, total int64) { |
| 112 | + if mp == nil { |
| 113 | + return 0, 0 |
| 114 | + } |
| 115 | + return mp.fringe.currentLive.Load(), mp.fringe.totalAllocated.Load() |
| 116 | +} |
| 117 | + |
| 118 | +// ################################################################## |
| 119 | + |
| 120 | +// nodePool is a type-safe wrapper around sync.Pool, |
| 121 | +// specialized for managing *node[V] instances. |
| 122 | +// |
| 123 | +// It supports efficient memory reuse and tracks allocation |
| 124 | +// and usage statistics to aid debugging and profiling. |
| 125 | +type nodePool[V any] struct { |
| 126 | + sync.Pool |
| 127 | + totalAllocated atomic.Int64 // total number of *node[V] instances ever created |
| 128 | + currentLive atomic.Int64 // number of currently checked-out (in-use) nodes |
| 129 | +} |
| 130 | + |
| 131 | +// newNodePool constructs and returns a nodePool with tracking enabled. |
| 132 | +func newNodePool[V any]() *nodePool[V] { |
| 133 | + np := &nodePool[V]{} |
| 134 | + np.New = func() any { |
| 135 | + np.totalAllocated.Add(1) |
| 136 | + return new(node[V]) |
| 137 | + } |
| 138 | + return np |
| 139 | +} |
| 140 | + |
| 141 | +// ################################################################## |
| 142 | + |
| 143 | +// leafPool is a sync.Pool wrapper for *leafNode[V] objects. |
| 144 | +// It tracks allocation and reuse statistics for monitoring purposes. |
| 145 | +type leafPool[V any] struct { |
| 146 | + sync.Pool |
| 147 | + totalAllocated atomic.Int64 |
| 148 | + currentLive atomic.Int64 |
| 149 | +} |
| 150 | + |
| 151 | +// newLeafPool initializes a leafPool instance with a node constructor. |
| 152 | +func newLeafPool[V any]() *leafPool[V] { |
| 153 | + lp := &leafPool[V]{} |
| 154 | + lp.New = func() any { |
| 155 | + lp.totalAllocated.Add(1) |
| 156 | + return new(leafNode[V]) |
| 157 | + } |
| 158 | + return lp |
| 159 | +} |
| 160 | + |
| 161 | +// ################################################################## |
| 162 | + |
| 163 | +// fringePool is a type-safe wrapper around sync.Pool, |
| 164 | +// specialized for managing *node[V] instances. |
| 165 | +// |
| 166 | +// It efficiently reuses node memory and tracks statistics |
| 167 | +// on allocations and active use for debugging and performance tuning. |
| 168 | +type fringePool[V any] struct { |
| 169 | + sync.Pool // embedded Sync Pool for *node[V] |
| 170 | + |
| 171 | + totalAllocated atomic.Int64 // total number of *node[V] ever allocated |
| 172 | + currentLive atomic.Int64 // number of nodes currently in use (not returned to pool) |
| 173 | +} |
| 174 | + |
| 175 | +// newFringePool creates and returns a new pool for *fringeNode[V] instances. |
| 176 | +// |
| 177 | +// The pool uses sync.Pool internally, and defines a New function |
| 178 | +// that creates new nodes with statistical tracking. |
| 179 | +func newFringePool[V any]() *fringePool[V] { |
| 180 | + fp := &fringePool[V]{} |
| 181 | + fp.New = func() any { |
| 182 | + fp.totalAllocated.Add(1) |
| 183 | + |
| 184 | + return new(fringeNode[V]) |
| 185 | + } |
| 186 | + return fp |
| 187 | +} |
0 commit comments