diff --git a/node.go b/node.go index febb373..c016381 100644 --- a/node.go +++ b/node.go @@ -43,11 +43,11 @@ type node[V any] struct { children []*node[V] } -// newNode, BitSets have to be initialized. +// newNode, the zero-value of BitSet is ready to use func newNode[V any]() *node[V] { return &node[V]{ - prefixesBitset: bitset.New(0), // init BitSet - childrenBitset: bitset.New(0), // init BitSet + prefixesBitset: &bitset.BitSet{}, + childrenBitset: &bitset.BitSet{}, } } diff --git a/table.go b/table.go index efa29e6..cc469e6 100644 --- a/table.go +++ b/table.go @@ -48,19 +48,13 @@ func (t *Table[V]) isInit() bool { return t.root4 != nil } -// init the root nodes, no public constructor needed, the zero value is ready to use. +// initOnce the root nodes, no public constructor needed, the zero value is ready to use. // Not using sync.Once here, the table is not safe for concurrent writers anyway -func (t *Table[V]) init() { +func (t *Table[V]) initOnce() { if t.isInit() { return } - // Outlined slow-path to allow inlining of the fast-path. - t.initOnce() -} - -// initOnce, too complex for inlining -func (t *Table[V]) initOnce() { t.root4 = newNode[V]() t.root6 = newNode[V]() } @@ -86,7 +80,7 @@ func (t *Table[V]) Insert(pfx netip.Prefix, val V) { return } - t.init() + t.initOnce() // values derived from pfx ip := pfx.Addr() @@ -159,7 +153,7 @@ func (t *Table[V]) Update(pfx netip.Prefix, cb func(val V, ok bool) V) (newVal V return zero } - t.init() + t.initOnce() // values derived from pfx ip := pfx.Addr() @@ -565,7 +559,7 @@ func (t *Table[V]) Union(o *Table[V]) { return } - t.init() + t.initOnce() dup4 := t.root4.unionRec(o.root4) dup6 := t.root6.unionRec(o.root6) diff --git a/table_test.go b/table_test.go index 775b4ee..7109fa3 100644 --- a/table_test.go +++ b/table_test.go @@ -13,7 +13,6 @@ import ( "fmt" "math/rand" "net/netip" - "reflect" "runtime" "testing" ) @@ -1027,7 +1026,7 @@ func TestDeleteIsReverseOfInsert(t *testing.T) { const N = 10_000 tbl := new(Table[int]) - tbl.init() + tbl.initOnce() want := tbl.dumpString() prefixes := randomPrefixes(N) @@ -1531,7 +1530,7 @@ func TestCloneEdgeCases(t *testing.T) { func TestClone(t *testing.T) { t.Parallel() - pfxs := randomPrefixes4(10_000) + pfxs := randomPrefixes(1_000) golden := new(Table[int]) tbl := new(Table[int]) @@ -1541,8 +1540,8 @@ func TestClone(t *testing.T) { } clone := tbl.Clone() - if !reflect.DeepEqual(golden, clone) { - t.Errorf("cloned table isn't equal") + if tbl.dumpString() != clone.dumpString() { + t.Errorf("Clone: got:\n%swant:\n%s", clone.dumpString(), tbl.dumpString()) } } @@ -1551,7 +1550,7 @@ func TestCloneShallow(t *testing.T) { tbl := new(Table[*int]) clone := tbl.Clone() - if tbl.String() != clone.String() { + if tbl.dumpString() != clone.dumpString() { t.Errorf("empty Clone: got:\n%swant:\n%s", clone.String(), tbl.String()) }