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 don't think it happens. Clojure stores the 0.5 constant as
an Object that is the result of RT.readString("0.5"). This Object then
has to go through Number.doubleValue prior to the call to Math.log on
every invocation of foo.

I would *guess* that this is too much clutter for HotSpot to figure
out that the result of the Math.log call is constant-foldable, but
you'd probably need to look at the generated assembly to be sure -
HotSpot is very aggressive with inlining, so it's possible that it can
see through the type-boxing and the call to doubleValue.

So much talk for not answering the question :)

On Wed, Dec 31, 2008 at 5:49 PM, 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? Can I check this somehow,
> i.e. look at the generated code, with reasonable effort? If it
> doesn't optimize it, I'd have to write
>
> (let [f (. Math log 0.5)]
>   (defn foo [x]
>     (* f x)))
>
> which is fine as well for this minimal example, but less clear in
> real-life code. I'd then rather write a small macro to evaluate the
> constant expression, but before doing so I'd like to know if I really
> have to.
>
> Konrad.
>
>
> >
>



-- 
Venlig hilsen / Kind regards,
Christian Vest Hansen.

--~--~---------~--~----~------------~-------~--~----~
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 unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to