I've had similar questions, though I haven't yet read Charles' article.
> Nope. While Clojure does use a class per function, and thus
> potentially more classes than a Java app might, if it had the same
> number of methods, the difference is not that great, as there is far
> more function reuse in Clojure, and thus fewer functions than you
> would have methods.
What is the magnitude of the space overhead of a class' internal
representation, beyond the bytecodes comprising its method(s)?
> More to the permgen point, Clojure does not do any dynamic ephemeral
> class creation. It creates classes for the functions you define.
> Period.
>
> ...
>
> Note also that by fn definition I mean the occurrence in code, not
> the execution:
>
> (defn foo [x] (fn [] x))
>
> creates two classes, no matter how many times foo is called. Each
> call to foo creates an instance of the same class.
Why is an instance creation required for an invocation? I thought you
used the JVM stack for calling and parameter passing both for Clojure
functions and (naturally) for calls to "ordinary" Java methods. If each
invocation of a Clojure function requires instantiation of the
function's class, aren't you effectively using those instances as call
stack frames? And does this not limit function call rate to
significantly less than the object allocation rate? And doesn't it
rapidly tear through the eden space?
If it isn't obvious, I know very little about the JVM or about how you
compile Clojure to JVM bytecodes, so perhaps I'm way off. Please excuse
my ignorance.
> One nice thing about Clojure is it was written to run well on today's
> (actually yesterday's) JVM. JVM enhancements will be nice, but we're
> not waiting for nor dependent upon them.
I've also been wondering whether there's anything coming in the JVM that
will relax the current TCO limitations in Clojure?
> Rich
Randall Schulz
OK. I misunderstood. So a closure is created only when the function is
treated like a value?
> > I thought you used the JVM stack for calling and parameter passing
> > both for Clojure functions and (naturally) for calls to "ordinary"
> > Java methods.
>
> I do.
>
> > If each invocation of a Clojure function requires instantiation of
> > the function's class
>
> It doesn't.
I looked at the source for clojure.lang.IFn and the invoke(...) methods
are not static. What instance of the function's implementing Java class
is used in cases where no closure is required?
> ...
>
> Rich
Randall Schulz