Skip to content

Commit 5b81f61

Browse files
committed
GSW-783 fix: reduce iteration time (bitmap index bug)
1 parent 79f419b commit 5b81f61

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

pool/tick_bitmap.gno

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ func (pool *Pool) tickBitmapFlipTick(
1717
) {
1818
require(tick%tickSpacing == 0, ufmt.Sprintf("[POOL] tick_bitmap.gno__tickBitmapFlipTick() || tick(%d) MOD tickSpacing(%d) != 0 (got:%d)", tick, tickSpacing, tick%tickSpacing))
1919
wordPos, bitPos := tickBitmapPosition(tick / tickSpacing)
20-
mask := bigint(1) << uint64(bitPos)
20+
mask := bigint(1) << uint64(bitPos) // 2 ** bitPos
21+
2122
requireUnsigned(mask, ufmt.Sprintf("[POOL] tick_bitmap.gno__tickBitmapFlipTick() || mask(%d) > 0", mask))
2223

2324
pool.tickBitmaps[wordPos] ^= mask
@@ -55,18 +56,10 @@ func (pool *Pool) tickBitmapNextInitializedTickWithInOneWord(
5556

5657
wordPos, bitPos := tickBitmapPosition(compress + 1)
5758

58-
// check zero
59-
var mask bigint
60-
if bitPos == 0 {
61-
mask = MAX_UINT256 - bigint(1)
62-
} else {
63-
mask = MAX_UINT256 - (bigint(1)<<uint64(uint64(bitPos)) - 1)
64-
}
59+
_mask := bigint(1) << uint64(bitPos)
60+
_mask -= bigint(1)
61+
mask := bigintBitwiseNotForUint256BitmapIndexing(_mask)
6562

66-
// mask := ^((bigint(1) << bitPos) - bigint(1)) // (2^256-1) - (2^bitPos-1)
67-
if mask < 0 {
68-
mask = -mask
69-
}
7063
requireUnsigned(mask, ufmt.Sprintf("[POOL] tick_bitmap.gno__tickBitmapNextInitializedTickWithInOneWord__mask(%d) >= 0__#2", mask))
7164

7265
masked := pool.tickBitmaps[wordPos] & mask
@@ -81,3 +74,14 @@ func (pool *Pool) tickBitmapNextInitializedTickWithInOneWord(
8174
return next, initialized
8275
}
8376
}
77+
78+
79+
func bigintBitwiseNotForUint256BitmapIndexing(x bigint) bigint {
80+
// Create a mask with all bits set to 1
81+
mask := MAX_UINT256
82+
mask -= bigint(1)
83+
84+
// XOR with mask to perform bitwise NOT
85+
result := x ^ mask
86+
return result
87+
}

0 commit comments

Comments
 (0)