-
Notifications
You must be signed in to change notification settings - Fork 60
Description
At the moment all of the spoke's internal data structures are using reserveId as the mapping key(s). This adds a bit of friction when iterating over the active reserves of an user, given that we store the active reserves in the PositionStatus data structure, which uses buckets of bitmasks. While converting from reserveId to bucketIndex and bucketBit is straightforward, the vice versa is not. Hence, we have the opportunity to replace all reserveId mapping key(s) from our internal data structures with a pair of bucketIndex and bucketMask, such that we can immediately access storage when iterating through active reserves.
Conversion from reserveId to bucketIndex and bucketMask
reserveId = 130 -> bucketIndex = 1, bucketMask = (1 << (130%128)) | (1 << (130%128)+1)
Iterate over active bits of a bitmask: we extract the mask of the least significant bit (by using n - n & (n - 1)) until mask is 0
11011101010000 = n
11011101001111 = n - 1
11011101000000 = n & (n - 1)
We can also combine bucketIndex and bucketMask by hashing them together to avoid adding an additional mapping dimension.