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/
_______________________________________________ Classpath mailing list [EMAIL PROTECTED] http://mail.gnu.org/mailman/listinfo/classpath