TimerWheel.java why long mask = SPANS[index] - 1; #541
-
TimerWheel.java line 129 why long mask = SPANS[index] - 1; why not long mask = SPANS[index] - 1; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 14 replies
-
Hi @javakinglzh, You accidentally asked why In case it helps (but you might know this): |
Beta Was this translation helpful? Give feedback.
Hi @javakinglzh,
You accidentally asked why
X
and notX
? Can you please clarify.In case it helps (but you might know this):
The mask is a cheap version of a modulus operand, which is otherwise a division instruction (a multi-cycle arithmetic operation). In the case of a power-of-two this can be replaced with a single cycle AND instruction,
x mod 2^i = x & (2^i – 1)
. This technique is popular in hash tables, including Java's, which improves performance due to how frequently they are used.