Re: Constant expression optimization

2009-01-01 Thread Konrad Hinsen
On 31.12.2008, at 17:56, Rich Hickey wrote: Does the Clojure compiler calculate the constant expression (. Math log 0.5) once, or at every function call? Every call. Clojure does not know that Math/log is a pure function. OK, then I'll use this near-trivial macro: (defmacro const

Re: Constant expression optimization

2009-01-01 Thread cliffc
HotSpot folds FP constants in a few rare cases, and I don't thing Math.log is one of them. For instance you can't fold x+0.0 into x in case x happens to be negative 0. Math.log is a pure function so it would be possible, but I don't think it made the short-list of hot FP functions to optimize.

Re: Constant expression optimization

2009-01-01 Thread Mark H.
Konrad and Cliff -- both useful replies, thank you :-) Happy New Year everyone! mfh --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To

Re: Constant expression optimization

2009-01-01 Thread Christian Vest Hansen
According to this page: http://wikis.sun.com/display/HotSpotInternals/PerformanceTechniques Sun HotSpot is able to recognize constants in local variables, and I recall to have read somewhere that most if not all Math.* functions are intrinsic, so it should theoretically be possible. However, I

Constant expression optimization

2008-12-31 Thread Konrad Hinsen
Suppose I write (defn foo [x] (let [f (. Math log 0.5)] (* f x))) Does the Clojure compiler calculate the constant expression (. Math log 0.5) once, or at every function call? Can I check this somehow, i.e. look at the generated code, with reasonable effort? If it doesn't optimize

Re: Constant expression optimization

2008-12-31 Thread Rich Hickey
On Dec 31, 11:49 am, Konrad Hinsen konrad.hin...@laposte.net wrote: Suppose I write (defn foo [x] (let [f (. Math log 0.5)] (* f x))) Does the Clojure compiler calculate the constant expression (. Math log 0.5) once, or at every function call? Every call. Clojure does not know

Re: Constant expression optimization

2008-12-31 Thread Dave Griffith
Note, however, that your JVM may very well know that java.lang.Math.log (0.5) is a constant, and optimize the calculation out of the JIT compiled code. This wouldn't show up in the bytecode, and is extremely difficult to actually check. Whether or not any JVM actually does this probably

Re: Constant expression optimization

2008-12-31 Thread Randall R Schulz
On Wednesday 31 December 2008 09:03, Dave Griffith wrote: Note, however, that your JVM may very well know that java.lang.Math.log (0.5) is a constant, and optimize the calculation out of the JIT compiled code. This wouldn't show up in the bytecode, and is extremely difficult to actually

Re: Constant expression optimization

2008-12-31 Thread Dave Griffith
No, but the semantics of java.lang classes are fully specified in the Java spec, and JVM implementers are allowed to rely on them. It's entirely possible that there are special case optimizations for java.lang.Math calls. --~--~-~--~~~---~--~~ You received this

Re: Constant expression optimization

2008-12-31 Thread Randall R Schulz
On Wednesday 31 December 2008 09:10, Dave Griffith wrote: No, but the semantics of java.lang classes are fully specified in the Java spec, and JVM implementers are allowed to rely on them. It's entirely possible that there are special case optimizations for java.lang.Math calls. Beyond the

Re: Constant expression optimization

2008-12-31 Thread Randall R Schulz
On Wednesday 31 December 2008 09:10, Dave Griffith wrote: No, but the semantics of java.lang classes are fully specified in the Java spec, Can you quote chapter and verse to this effect? I'm not finding it, at least not in The Java™ Language Specification, Third Edition. There's an index

Re: Constant expression optimization

2008-12-31 Thread Dave Griffith
Can't find the chapter and verse, but a bit of googling shows that GCJ does this optimization http://gcc.gnu.org/ml/gcc-patches/2003-05/msg02312.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group.

Re: Constant expression optimization

2008-12-31 Thread Randall R Schulz
On Wednesday 31 December 2008 09:56, Dave Griffith wrote: Can't find the chapter and verse, but a bit of googling shows that GCJ does this optimization http://gcc.gnu.org/ml/gcc-patches/2003-05/msg02312.html Yes, but you claim that this guarantee is in the language specification and that it

Re: Constant expression optimization

2008-12-31 Thread Mark H.
On Dec 31, 8:49 am, Konrad Hinsen konrad.hin...@laposte.net wrote: Suppose I write (defn foo [x]    (let [f (. Math log 0.5)]      (* f x))) Does the Clojure compiler calculate the constant expression (. Math   log 0.5) once, or at every function call? Folding constants in nontrivial