classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 2nd attempt at Re: The right way(tm) of writing toString() (Was: Re:


From: Etienne Gagnon
Subject: Re: 2nd attempt at Re: The right way(tm) of writing toString() (Was: Re: [PATCH] Field position attribute handling)
Date: Sun, 30 Nov 2003 12:49:30 -0500
User-agent: Mozilla/5.0 (X11; U; Linux ppc; en-US; rv:1.5) Gecko/20031110 Debian/1.5-3

Hi Dalibor,

I would drop h) altogether.

Rationale: while a "naive" Java compiler would produce a series of iload
instructions, for reusing a local variable, an optimizing compiler or a jit
would completely remove this.  So, yes, the code that reuse a local could be
a little slower on a interpreter (I am sure that on sablevm this would be
negligible as iload is a very cheap operation), but good software engineering
is the goal here.

Not only do I personally find the following more readable, but it is also
easier to debug, as a debugger would trace through the instructions one by one:

public String toString()
{
  StringBuffer sb = StringBuffer();

  sb.append("Test[field_1=");
  sb.append(field_1);
  sb.append(",field_2=");
  sb.append(field_2);
  sb.append("]");

  return sb.toString();
}

The performance difference (except on "naive" interpreters) is negligible.

Note about performance:  The reason iload is expensive on naive interpreters,
is the cost of dispatching an additional instruction in the interpreter.
SableVM's inline-threaded engine does eliminate dispatch between successive
instructions of a basic block, so the cost of an additional iload is simply
that of the "iload" functionality, not that of (iload + dispatch) as found
in normal switch-based interpreters and direct-threaded interpreters.

So, in summary: it is best to leave such low-level optimizations in the hand
of the compiler, not of the developer.  The developer should instead care about
code readability and debuggability (i.e. software engineering issues).

Etienne

Dalibor Topic wrote:
h) re-use the StringBuffer on the stack

Rationale: In order to write as efficient code using StringBuffer as the good Java compilers, we need to reuse the StringBuffer instance on the stack. So instead of writing multiple append statements, try to use a single statement to perform all the work on the StringBuffer instance, including creation, calling append, and calling toString() on it.

Example code:

public class Test{
  private String field_1;
  private int field_2;

  public String toString() {
    return
    (new StringBuffer("Test[field_1=")).append(field_1)
    .append(",field_2=").append(field_2)
    .append(']')
    .toString();
  }
}

comments are welcome, as usual.

cheers,
dalibor topic





--
Etienne M. Gagnon, Ph.D.             http://www.info.uqam.ca/~egagnon/
SableVM:                                       http://www.sablevm.org/
SableCC:                                       http://www.sablecc.org/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]