Field access optimisations inside loops (question).

Ulf Zibis Ulf.Zibis at gmx.de
Fri Jan 8 03:32:07 PST 2010


Am 08.01.2010 11:07, Dawid Weiss schrieb:
>
>> 2. I agree, if:
>> - list is private member in this class,
>> - list is not reachable by other methods of this class from a concurrent
>> thread.
>>     
>
> list is a private member, although not final. The list field is used
> in other methods of this class, but does this
> change anything from the current thread's point of view? Correct me if
> I'm wrong, 

You could have contructors like:
Benchmark(Object list) {
    this.list = list;
}
Benchmark(byte[] bytes) {
    list = new WhatEverClass();
    list.buffer = bytes;
}

In both cases the list's members, and accordingly it's buffer's elements 
could be concurrently modified by another thread from outside.

You have the same situation, if some method is able to change anything 
from outside, the list, it's members, or it's members's elements.

If you don't have something like that, I agree, HotSpot could optimize here.

> Yep, this (http://bugs.sun.com/view_bug.do?bug_id=6914095) seems
> similar. I have a suspicion that my understanding of what hotspot can
> do is not entirely correct though (reachability in your remark 2
> above).
>   

In this bug the situation is little different, because the regarding 
variables are method local, so are not reachable by other threads.
*** Even there, HotSpot could optimize better, IMO.

BTW 1: your example 2 is common practice to increase java code. For 
short, you could use:

       for (int i = 0, size = list.size(), buffer[] = list.buffer; i < 
size; i++)
       {
           value = buffer[i];
       }


BTW 2: Good trick, to prevent from dead code elimination by public 
static volatile target.

-Ulf




More information about the hotspot-compiler-dev mailing list