0xJepsen / Solring

ring buffer in solidity
10 stars 0 forks source link

suggestion: add docs/comments for assembly #1

Open aodhgan opened 1 year ago

aodhgan commented 1 year ago

Cool - love the ring buffer approach. We used something similar in PoolTogether V4 (https://github.com/pooltogether/v4-core/blob/master/contracts/libraries/RingBufferLib.sol) with great gas savings (after the initialization period).

Can you ELI5 what you are doing here: https://github.com/0xJepsen/Solring/blob/master/src/RingLib.sol#L11

Also I think there is maybe a load of stale code? Solring.sol and maybe the huff directory? Would be cool if everything could be abstracted enough to add to a standard lib (solmate/oz etc.)

0xJepsen commented 1 year ago

Yeah, @Philogy will have to confirm but i believe the and operation can be used in place of the modulus and saves 3 less gas

Philogy commented 1 year ago

Whoops sorry I didn't get back to this, added a comment explaining @aodhgan @0xJepsen.

But basically the ELI5 is: that the following operations are equivalent: $AND(x, 2^n - 1)$ and $x \mod 2^n$. However in the EVM bitwise-ANDs (and) are 2 gas cheaper than modulo operations (mod). However to take advantage of this the ring buffer's size must be a power of 2