CompiledExpression.evaluate implementation

Mark Taylor <[email protected]> Tue, 17 Nov 2020 10:48:08 +0000 (GMT)
Newsgroups gmane.comp.java.jel.general
Message-ID <alpine.DEB.2.21.2011171044550.2990@IT076926>
Konstantin,

while I'm talking to you, I have another suggestion.

In gnu.jel.CompiledExpression.evaluate you have this code:

    switch (type) {
    case 0: res=new Boolean(evaluate_boolean(dl)); break;
    case 1: res=new Byte(evaluate_byte(dl)); break;
    case 2: res=new Character(evaluate_char(dl)); break;
    case 3: res=new Short(evaluate_short(dl)); break;
    case 4: res=new Integer(evaluate_int(dl)); break;
    case 5: res=new Long(evaluate_long(dl)); break;
    case 6: res=new Float(evaluate_float(dl)); break;
    case 7: res=new Double(evaluate_double(dl)); break;
    case 9: evaluate_void(dl); break;
    ...

What do you think about writing instead:
    
    case 0: res=Boolean.valueOf(evaluate_boolean(dl)); break;
    case 1: res=Byte.valueOf(evaluate_byte(dl)); break;

since those methods avoid object creation by using pre-constructed
Boolean/Byte object instances (see the advice in the javadocs
for those static methods).  The case is less clear for the other
types; although the javadocs suggest that valueOf is "likely to
yield significantly better space and time performance by caching
frequently requested values" for all those wrapper class valueOf
methods, I have sometimes found in tests that they are slower
than using the constructors, presumably because of lookup overheads.

Thanks for considering,

Mark

--
Mark Taylor   Astronomical Programmer   Physics, Bristol University, UK
[email protected] +44-117-9288776  http://www.star.bris.ac.uk/~mbt/