Skip to content

Commit b9da247

Browse files
authored
Merge pull request #28 from ryanworl/ryanworl/skip-lock-alloc
skip allocating mutex if it isn't required
2 parents 1167866 + 1c271b2 commit b9da247

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
@@ -1090,13 +1092,15 @@ func (tr *BTreeG[T]) Copy() *BTreeG[T] {
10901092
}
10911093

10921094
func (tr *BTreeG[T]) IsoCopy() *BTreeG[T] {
1095+
var mu *sync.RWMutex
10931096
if tr.lock(true) {
1097+
mu = new(sync.RWMutex)
10941098
defer tr.unlock(true)
10951099
}
10961100
tr.isoid = newIsoID()
10971101
tr2 := new(BTreeG[T])
10981102
*tr2 = *tr
1099-
tr2.mu = new(sync.RWMutex)
1103+
tr2.mu = mu
11001104
tr2.isoid = newIsoID()
11011105
return tr2
11021106
}

0 commit comments

Comments
 (0)