|
Yeah, it was a bit blithe of me to write that the sequence of
tests was faster. In the table-lookup version, if you get rid of
the initial test for RUNNABLE, and if you use
Integer.numberOfLeadingZeros, and if the JIT compiler intrinsifies
that to a native processor instruction, and if the lookup table is
in L1 cache, then the table-lookup version will run in constant
time and be better than the worst case of the sequence-of-tests
version, and probably better than the average case too. But, as
you say, that last if (the cache hit) will usually not be
true, and in that case I would not be surprised if the sequence of
tests were faster even in its worst case. Anyway the sequence-of-tests version is unquestionably simpler,
and I would venture that the best solution is probably to go with
that, plus a new method in the API that explicitly tests whether a
thread is runnable. That's trivial to implement now that Mandy has
pulled the knowledge of state bits into the Java code rather than
being hidden in the bowels of the VM; and its implementation will
be faster than (Thread.getState() == RUNNABLE) regardless of the
implementation of the latter. Éamonn On 5/12/10 8:27 AM, Brian Goetz wrote: As Eamonn writes it, it will never cache miss but may frequently branch mispredict (possibly multiple times). If you do a shift + mask + index into a small table, it will cache miss most the time but never branch mispredict. (In a real program it will cache miss frequently since thread state calls are infrequent and the lookup table will fall out of cache; in a microbenchmark it will almost never cache miss as the lookup table will be hot.) |
- Review request for 6977034 Thread.getState() very slow Mandy Chung
- Re: Review request for 6977034 Thread.getState() very ... Rémi Forax
- Re: Review request for 6977034 Thread.getState() very ... Eamonn McManus
- Re: Review request for 6977034 Thread.getState() v... Brian Goetz
- Re: Review request for 6977034 Thread.getState... Eamonn McManus
- Re: Review request for 6977034 Thread.getS... Brian Goetz
- Re: Review request for 6977034 Thread... David Holmes
- Re: Review request for 6977034 Th... Doug Lea
- Re: Review request for 6977034 Thread.getState() v... Mandy Chung
- Re: Review request for 6977034 Thread.getState... Eamonn McManus
- Re: Review request for 6977034 Thread.getS... Mandy Chung
- Re: Review request for 6977034 Thread.getState... David Holmes
- Re: Review request for 6977034 Thread.getState... Robert Lougher
- Re: Review request for 6977034 Thread.getS... Mandy Chung
