A while back I wrote a Base64 encoder, and have been using it to play
with the primitive enhancements in 1.3.  After a bit of fiddling I was
able to get it from around an order of magnitude slower than apache
commons-codec down to about 5x slower.

Taking Rich's exclamation that "clojure was meant to replace java, not
ruby" to heart, I fired up the profiler.  It turns out that calls to
aset-byte were killing me.  The aset-byte macro expanded into
clojure.core$aset_byte.invoke(Object, Object, Object), an autoboxing
nightmare.

By replacing those calls with plain aset (which goes to one of the
primitive RT.aset methods), it's now *faster* than commons-codec:

user=> (time (dotimes [_ 10000] (Base64/encodeBase64 rand-
bytes-4096)))
"Elapsed time: 413.225 msecs"
nil
user=> (time (dotimes [_ 10000] (encode rand-bytes-4096)))
"Elapsed time: 182.8 msecs"
nil


A few other things I ran into:
- bit-shifting requires int or long if you want to avoid reflection,
so bytes from the array had to be explicitly converted.
- But you can't convert a byte to a long (missing overload on
longCast), you have to use int
- the let bindings are smart, but if the value is the result of some
expression (e.g., case) then it needs to be wrapped in a cast if you
want a primitive instead of an Object binding.


http://github.com/ataggart/codec/blob/master/src/codec/base64.clj

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