Always false expressions in ConcurrentHashMap source
Doug Lea
dl at cs.oswego.edu
Fri Mar 16 10:09:09 UTC 2018
On 03/16/2018 02:18 AM, Tagir Valeev wrote:
> Hello!
>
> Experimenting with code static analysis I found several always false
> expressions in ConcurrentHashMap source. See addCount method:
>
> http://hg.openjdk.java.net/jdk/jdk/file/422615764e12/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#l2352
>
> if (sc < 0) {
> if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
> sc == rs + MAX_RESIZERS || ...
>
> RESIZE_STAMP_SHIFT is 16, MAX_RESIZERS is 65535
Right. The code was initially designed to work with any values
for these constants. But upon experimentation, the current values
were chosen in part so that this case would not trigger. Still,
it is cheap enough to keep the check here.
-Doug
>
> While sc is a negative inside if, "(sc >>> RESIZE_STAMP_SHIFT)" is a
> positive number (more precisely, in {32768..65535} range). So if the "(sc
>>>> RESIZE_STAMP_SHIFT) != rs" is false, then
> rs is equal to "(sc >>> RESIZE_STAMP_SHIFT)", thus it's also in the same
> range. This makes the condition "sc == rs + 1" always false when reached
> (rs+1 is {32769..65536}, but sc is negative here). Also "sc == rs +
> MAX_RESIZERS" is always false when reached (rs+MAX_RESIZERS is
> {98303..131070}).
>
> Similar code also appears in helpTransfer method below (line 2378) and also
> contains two always false conditions.
>
> I don't know whether something else was meant here, or these checks are
> redundant. Just wanted to draw your attention.
>
> With best regards,
> Tagir Valeev.
>
More information about the core-libs-dev
mailing list