> On 14 Dec 2023, at 08:12, Amul Sul <sulamul@gmail.com> wrote: > > > + int bankno = pageno & ctl->bank_mask; > > I am a bit uncomfortable seeing it as a mask, why can't it be simply a number > of banks (num_banks) and get the bank number through modulus op (pageno % > num_banks) instead of bitwise & operation (pageno & ctl->bank_mask) which is a > bit difficult to read compared to modulus op which is quite simple, > straightforward and much common practice in hashing. > > Are there any advantages of using & over % ?
use Compiler Explorer[1] tool, '%' has more Assembly instructions than '&' .
The instruction AND is ~20 times faster than IDIV [0]. This is relatively hot function, worth sacrificing some readability to save ~ten nanoseconds on each check of a status of a transaction.
Now that AND is more faster, Can we replace the '% SLRU_MAX_BANKLOCKS' operation in SimpleLruGetBankLock() with '& 127' :
SimpleLruGetBankLock()
{ int banklockno = (pageno & ctl->bank_mask) % SLRU_MAX_BANKLOCKS; use '&' return &(ctl->shared->bank_locks[banklockno].lock); }