Recently I’ve been working on optimizing custom sampling logic for one of the performance sensitive services. I’ll save you the details, but the part that is the hero of this article is dealt with replacing modulo arithmetic - instead of count % n == 0,
we can decrement count on each request until we get to the bottom, at which point, count is set to sample rate again. The interesting part is what we use as a bottom value. We can use 1 or 0 like in the code below
Does it matter? Well, let’s take a look at the assembly
Note that when we use 0, we don’t need an extra cmp eax, 1
, since we can just reuse the zero flag set after subtracting 1 (add ecx, -1
) . Obviously, it’s just a single cheap instruction, so we shouldn’t expect huge wins, but the benchmark confirmed small improvement
and since there are no obvious downsides of this approach, apart from having to handle 0 sample rate with extra care, keep this optimization in mind when dealing with performance sensitive sections.