Skip to content

Commit

Permalink
use zero-value of bitset.BitSet, not bitset.New()
Browse files Browse the repository at this point in the history
- initOnce() is now inlineable
  • Loading branch information
gaissmai committed Sep 1, 2024
1 parent 7139e25 commit 65275aa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
6 changes: 3 additions & 3 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
}

Expand Down
16 changes: 5 additions & 11 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()
}
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"fmt"
"math/rand"
"net/netip"
"reflect"
"runtime"
"testing"
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand All @@ -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())
}
}

Expand All @@ -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())
}

Expand Down

0 comments on commit 65275aa

Please sign in to comment.