Skip to content

Commit b87793d

Browse files
lornelideckarep
authored andcommitted
threadUnsafeSet: reduce allocation in Add func
In Add func, there's no need to alloc a new empty struct if corresponding key-value pair exists.
1 parent 1d4478f commit b87793d

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

threadunsafe.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ func (pair *OrderedPair) Equal(other OrderedPair) bool {
5757

5858
func (set *threadUnsafeSet) Add(i interface{}) bool {
5959
_, found := (*set)[i]
60+
if found {
61+
return false //False if it existed already
62+
}
63+
6064
(*set)[i] = struct{}{}
61-
return !found //False if it existed already
65+
return true
6266
}
6367

6468
func (set *threadUnsafeSet) Contains(i ...interface{}) bool {

0 commit comments

Comments
 (0)