Skip to content

Commit 1c271b2

Browse files
committed
skip allocating mutex if it isn't required
1 parent c538c0c commit 1c271b2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

btreeg.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ func NewBTreeG[T any](less func(a, b T) bool) *BTreeG[T] {
5454
func NewBTreeGOptions[T any](less func(a, b T) bool, opts Options) *BTreeG[T] {
5555
tr := new(BTreeG[T])
5656
tr.isoid = newIsoID()
57-
tr.mu = new(sync.RWMutex)
5857
tr.locks = !opts.NoLocks
58+
if tr.locks {
59+
tr.mu = new(sync.RWMutex)
60+
}
5961
tr.less = less
6062
tr.init(opts.Degree)
6163
return tr
@@ -1066,13 +1068,15 @@ func (tr *BTreeG[T]) Copy() *BTreeG[T] {
10661068
}
10671069

10681070
func (tr *BTreeG[T]) IsoCopy() *BTreeG[T] {
1071+
var mu *sync.RWMutex
10691072
if tr.lock(true) {
1073+
mu = new(sync.RWMutex)
10701074
defer tr.unlock(true)
10711075
}
10721076
tr.isoid = newIsoID()
10731077
tr2 := new(BTreeG[T])
10741078
*tr2 = *tr
1075-
tr2.mu = new(sync.RWMutex)
1079+
tr2.mu = mu
10761080
tr2.isoid = newIsoID()
10771081
return tr2
10781082
}

0 commit comments

Comments
 (0)