30

In Java's String class, the trim method contains this:

int off = offset;      /* avoid getfield opcode */
char[] val = value;    /* avoid getfield opcode */

I'm a bit puzzled by the comment "avoid getfield opcode"...

What does this mean? (I take it this avoids the use of getfield in the bytecode but why is this a Good Thing [TM]?)

Is it to prevent object creation in case trim doesn't do anything (and hence this is returned) or?

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120

2 Answers2

23

My guess is that the point is to copy the values into local variables once, to avoid having to fetch the field value repeatedly from the heap for each iteration of the loop in the next few lines.

Of course, that begs the question as to why the same comment hasn't been applied on the "len" local variable. (I'd also expect the JIT to avoid refetching anyway, especially as the variables are final.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 8
    The comment does not apply to 'len' because len is actively modified in the method, so it had to be a local variable anyway. 'off' and 'val' on the other hand aren't modified, but exist solely for optimization. – Lars Jan 21 '11 at 17:27
  • @Lars: Well spotted; I hadn't seen the change to len. – Jon Skeet Jan 21 '11 at 17:30
  • @JonSkeet If you personally were writing this library, do you expect you would have done this? Or simply relied on the JIT to micro-optimze? – corsiKa Feb 28 '12 at 18:20
  • 2
    @corsiKa: I'd have written the simplest code first, then profiled it. If that had suggested the micro-optimization, I *might* have applied it. – Jon Skeet Feb 28 '12 at 18:22
14

getfield is used to get the member variable of a class.

As you can see from the remaining code:

while ((st < len) && (val[off + st] <= ' ')) {
    st++;
}
while ((st < len) && (val[off + len - 1] <= ' ')) {
    len--;
}

So when you're in a loop, it has to execute getfield every time you reference value or offset. You can incur a large performance-hit if the loop runs for a long time (because every time the loop-condition is tested, a getfield is exeuted for both offset and value). So by using the local variables off and val, you reduce the performance hit.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • Agreed, except for this being valid only for non-optimized code. When JIT gets involved, it doesn't matter anymore. JIT does the optimization probably by putting value in a register; it's trivially allowed since it's fully equivalent. With value being final, this is obvious. But it'd possible even without it. – maaartinus Jan 21 '11 at 17:58
  • 6
    JIT *may* optimize this. Eg, don't expect this optimization on Dalvik. Pulling a field into a local can be useful for other reasons, such as a null check for a field that could be accessed concurrently. – NateS Nov 22 '12 at 03:07