I rarely have more questions than answers here, but this is one of
those times, and it has to do with efficiency.
Is the bytecode generated from

(let [^StringBuilder sb (StringBuilder.)]
  (.append sb "Hello, ")
  (.append sb "world")
  (.toString sb))

equivalent to Java

final StringBuilder sb = new StringBuilder();
sb.append("Hello, ");
sb.append("world");
return sb.toString();

or instead to

final Object sb = new StringBuilder();
((StringBuilder)sb).append("Hello, ");
((StringBuilder)sb).append("world");
return ((StringBuilder)sb).toString();

?

I ask because though the latter is much more efficient than
reflection, it is also potentially less efficient than the former
(though not by nearly as large an amount).

In theory, casts on dereference-and-use can be avoided if the type
being assigned to a hinted local has been "vetted" already -- hinted
function parameters require a cast, hinted let/loop locals assigned
from unknown objects require a cast, but hinted let/loop locals
assigned something that's already been cast or that resulted from a
new special form could avoid that requirement.

At the same time: how good is the HotSpot JIT at eliminating the casts
in the latter Java code, after discovering that sb's referent is never
not of a type assignable to StringBuilder?

Usually I'd just run my own tests, but in this case to answer question
1 I'd need to find myself a decompiler and decompile me some bytecode
from some sample Clojure code, and for question 2 do speed comparisons
between two chunks of Java code, which would mean dealing with Java,
javac, creating a whole new directory tree just for some throwaway
code since you can't just ad-hoc write and compile some Java without a
project structure as javac hates that, and yadda yadda yadda, instead
of just evaluating a few forms at a REPL. :)

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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