On Sat, Nov 14, 2009 at 6:19 AM, ajuc <aju...@gmail.com> wrote:

> > > I would like to somehow hide the global hilbert-map into my function,
> > > but I can't see how to do that.
> >
> > Just put the literal directly into the function.
> >
> > > Is this possible? I know that I can just inert literal into my let,
> > > but that degrades performance, when function is called many times.
> >
> > Have you tested it, using (time ...), preferably on the server VM? I
> > wouldn't be surprised if a calculation that produces a constant gets
> > optimized away to just the constant.
>
> (time (dotimes [_ (int 10000)] (point-to-hilbert (rand-int (int
> -1000)) (int 1) (int 16))))
> (time (dotimes [_ (int 10000)] (point-to-hilbert-2 (rand-int (int
> -1000)) (int 1) (int 16))))
>
> "Elapsed time: 892.041865 msecs" - version with global
> "Elapsed time: 1152.569949 msecs" - version with literal in let
> statement inside the function
>

Eeeuw.

Was this with a Clojure literal in the let, or with a non-trivial
calculation using constants?

If it's with a literal, it's an optimization Rich can make pretty easily: if
a function being compiled contains a let bind whose value, or a loop bind
whose initial value, is (after macro expansion) pure data (no function or
special form invocations -- just vectors, sets, maps, etc. with leaves that
are integers, strings, doubles, etc.), then change that value to a private
static final field in the function class, and in the "let" case the places
where the function requests that variable can compile to bytecode to access
that field. The "loop" case just requires the loop initialization grab the
field value as the initial value of the loop binding, then behave as usual
for the loop body and recur.

Some function and special form calls can even be allowed, with a little
finesse: clojure.core/list for one, quote, and nested let:

(let [x (let [y [1 2]] [y y])] ...)

can be reduced to bytecode equivalent to this pseudo-Java

private static final IPersistentVector X = [[1 2] [1 2]];

public Object invoke blah blah blah {
    // ... uses X
}

-- 
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