[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-jel] Stateless method evaluation
From: |
Mark Taylor |
Subject: |
Re: [Help-jel] Stateless method evaluation |
Date: |
Wed, 13 Feb 2019 15:32:04 +0000 |
Konstantin,
thank you, that clarifies it. I suppose the documentation should be
modified (if or when you can get round to it) to read something like:
"Stateless methods *with primitive return values* will be
evaluated by JEL at compile time if their arguments are
constants (known at compile time)."
The example I showed using an Integer wrapper class was just the simplest
illustration I could find for the question (I do use primitives
wherever possible with JEL). The reason I really wanted to know
about this was in relation to arguments with constant array-valued
arguments (like the array* static methods in my example code): if I
could expect to receive the same array instance every time one of these
methods was called, I could perform efficient caching of results
for array-valued functions by use of an IdentityHashMap.
But it looks like that won't be possible.
Mark
On Wed, 13 Feb 2019, Konstantin L. Metlov wrote:
> Dear Mark,
>
> Yes, the JEL only evaluates the constant subexpressions if it can
> represent the result as a constant in the class file. The Java class file
> format, unfortunately, does not allow the storage of the objects, only
> primitive types and some special constants.
>
> On the other hand, recasting the computation in terms of primitive types
> offers also other speed advantages, besides precomputing more constant
> subexpressions. From the point of view of JEL interface, the differences
> (between the program, using JEL on primitives and the program using it on
> wrapped objects) are negligible, but the computation will be much faster.
>
> There is also an alternative of substantially modifying JEL, so that for
> wrapper objects it uses primitives internally and converts to objects on
> demand. To me it seems much harder, compared to just switching to the
> primitive types in the library class. Also, the identities of the returned
> objects will still be different.
>
> With the best regards,
> Konstantin.
>
> > Dear Konstantin,
> >
> > I have a question about evaluation of stateless methods in JEL.
> > The documentation says this:
> >
> > "Methods and variables of the first class are assumed (by default) to
> > be dependent only on their arguments i.e. not to save any information
> > from call to call (they are "stateless")... Examples are mathematical
> > functions like sin, cos, log, constants E, PI in java.lang.Math. For
> > such methods (fields) it does not matter how many times (when)
> > they will be called (their value will be taken) the result will
> > always be the same provided arguments (if they are present) are the
> > same. Stateless methods will be evaluated by JEL at compile time if
> > their arguments are constants (known at compile time)."
> >
> > If I run the following code:
> >
> > import gnu.jel.CompiledExpression;
> > import gnu.jel.Evaluator;
> > import gnu.jel.Library;
> >
> > public class JelEval {
> > public static void main( String[] args ) throws Throwable {
> > String sexpr = "identity(intObj(3))";
> > Library lib = new Library( new Class[] { JelEval.ALib.class },
> > null, null, null, null );
> > CompiledExpression compex = Evaluator.compile( sexpr, lib );
> > System.out.println( sexpr );
> > for ( int i = 0; i < 4; i++ ) {
> > System.out.println( "\t" + compex.evaluate( null ) );
> > }
> > }
> >
> > public static class ALib {
> > public static String identity( Object o ) {
> > return Integer.toHexString( System.identityHashCode( o )
> > );
> > }
> > public static Integer intObj( int i ) {
> > return new Integer( i );
> > }
> > public static double[] array3( double x1, double x2, double x3
> > ) {
> > return new double[] { x1, x2, x3 };
> > }
> > public static double[] array( double... values ) {
> > return values;
> > }
> > }
> > }
> >
> > I get this output:
> >
> > identity(intObj(3))
> > 67386000
> > 2626d4f1
> > 3f662dc8
> > 4d47c5fc
> >
> > Given the last sentence of the manual excerpt I quote above, I would
> > expect the program to print the same identityHashCode four times,
> > rather than four different ones. Does the evaluation at compile time
> > only apply to primitive return values, or is there something else I
> > don't understand here? If I haven't stated the problem clearly enough,
> > please let me know and I'll try to be more explicit.
> >
> > Many thanks in advance,
> >
> > Mark
> >
> > --
> > Mark Taylor Astronomical Programmer Physics, Bristol University, UK
> > address@hidden +44-117-9288776 http://www.star.bris.ac.uk/~mbt/
> >
> > _______________________________________________
> > Help-jel mailing list
> > address@hidden
> > https://lists.gnu.org/mailman/listinfo/help-jel
> >
>
>
>
--
Mark Taylor Astronomical Programmer Physics, Bristol University, UK
address@hidden +44-117-9288776 http://www.star.bris.ac.uk/~mbt/