Skip to content

TimerWheel.java why long mask = SPANS[index] - 1; #541

Answered by ben-manes
javakinglzh asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @javakinglzh,

You accidentally asked why X and not X? 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.

Replies: 1 comment 14 replies

Comment options

You must be logged in to vote
14 replies
@ben-manes
Comment options

@javakinglzh
Comment options

@ben-manes
Comment options

@ben-manes
Comment options

@ben-manes
Comment options

Answer selected by ben-manes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants