|
|
|
|
|
by aphyr
928 days ago
|
|
Speaking very loosely, primitives on the JVM are values which are represented directly in memory, instead of as pointers to objects on the heap. Clojure (again, very loosely) generally treats everything as a pointer to a heap object. There is no specialized equivalent for, say, a vector of shorts, or a map where values are floats. The compiler can emit specialized function signatures for... IIRC longs and doubles, but other types (e.g. byte, float) aren't directly accessible--they go through widening conversion. It's also easy for the compiler to quietly fail to recognize it can preserve primitives in some kinds of loops, so you wind up with what Java calls "autoboxing": wrapping a primitive in a corresponding Object type. Here's a recent example of some code in a hot path inside Elle, one of Jepsen's safety checkers. It does a lot in primitives, using packed structures and bitmasks to avoid pointer chasing. https://github.com/jepsen-io/elle/blob/main/src/elle/BFSPath... There was actually a Clojure version of this earlier that got pretty close perf-wise, but I wound up dropping to Java for it instead: https://github.com/jepsen-io/elle/blob/913cbff5ebb19ba850c0a... |
|
``` (defn sum-clojure [size] (reduce + 0 (range 0 size)))
(sum-clojure 100000000) ```
Despite the fact that Clojure primitives are boxed, a manually constructed long array that was summed together in Java was much slower. Why is that?