mirror of
https://github.com/jeremyd/ergo.git
synced 2026-07-04 07:43:37 -07:00
fix #1387
Instead of building a new serialized message for each recipient, try to cache them.
This commit is contained in:
@@ -17,6 +17,14 @@ func BitsetGet(set []uint32, position uint) bool {
|
||||
return (block & (1 << bit)) != 0
|
||||
}
|
||||
|
||||
// BitsetGetLocal returns whether a given bit of the bitset is set,
|
||||
// without synchronization.
|
||||
func BitsetGetLocal(set []uint32, position uint) bool {
|
||||
idx := position / 32
|
||||
bit := position % 32
|
||||
return (set[idx] & (1 << bit)) != 0
|
||||
}
|
||||
|
||||
// BitsetSet sets a given bit of the bitset to 0 or 1, returning whether it changed.
|
||||
func BitsetSet(set []uint32, position uint, on bool) (changed bool) {
|
||||
idx := position / 32
|
||||
@@ -79,6 +87,15 @@ func BitsetCopy(set []uint32, other []uint32) {
|
||||
}
|
||||
}
|
||||
|
||||
// BitsetCopyLocal copies the contents of `other` over `set`,
|
||||
// without synchronizing the writes to `set`.
|
||||
func BitsetCopyLocal(set []uint32, other []uint32) {
|
||||
for i := 0; i < len(set); i++ {
|
||||
data := atomic.LoadUint32(&other[i])
|
||||
set[i] = data
|
||||
}
|
||||
}
|
||||
|
||||
// BitsetSubtract modifies `set` to subtract the contents of `other`.
|
||||
// Similar caveats about race conditions as with `BitsetUnion` apply.
|
||||
func BitsetSubtract(set []uint32, other []uint32) {
|
||||
|
||||
@@ -80,4 +80,14 @@ func TestSets(t *testing.T) {
|
||||
if !BitsetGet(t3s, 0) || BitsetGet(t3s, 72) || !BitsetGet(t3s, 74) || BitsetGet(t3s, 71) {
|
||||
t.Error("subtract doesn't work")
|
||||
}
|
||||
|
||||
var tlocal testBitset
|
||||
tlocals := tlocal[:]
|
||||
BitsetCopyLocal(tlocals, t1s)
|
||||
for i = 0; i < 128; i++ {
|
||||
expected := (i != 72)
|
||||
if BitsetGetLocal(tlocals, i) != expected {
|
||||
t.Error("all bits should be set except 72")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user