3 ms·
I can't have a list of a million integer pairs take 160MB of RAM. This doesn't have much to do with sharing. Rather, it is the overhead of boxing the integers
by jrockway 17y ago
I can't have a list of a million integer pairs take 160MB of RAM.
This doesn't have much to do with sharing. Rather, it is the overhead of boxing the integers.
If you want a byte array that you'll interpret as an array of machine integers (as an int * would be in C), then you need to use that type instead of a "list of integers" that your language provides. That "list of integers" is optimized for something other than space (integer math not overflowing, O(1) insertions, etc.). Conversely, arrays of machine ints have many annoying quirks, but the underlying hardware can process them really quickly, and they are very frugal with respect to memory use. (No overhead, basically.)
As it stands now, most language implementations are not smart enough to see that you just used lists because you like the syntax, but meant to use byte arrays. (Or, you used Integers instead of ints.)
Tracemonkey, GHC, and probably other languages have code to help optimize this case. dons wrote a nice article about how GHC's codegen produces code that uses unboxed types even though the programmer used boxed types.
As far as I know, Clojure is not really smart enough to optimize much right now (beyond the JIT that the JVM does), so this is probably a problem. The solution is to fix Clojure or to use the native types. I don't know how to do this, as I don't use Java or Clojure, but it sounds like a Simple Matter Of Programming to expose a shared array of unboxed integers. (There are a lot of papers on this.)
Functional programming is a "new idea" in terms of people using it for "Real Work", so there is a long way to go in terms of writing excellent optimizing compilers. It is getting better very quickly, though.
- fauigerzigerk 17y agoWell, I have played around with this for some time and I found that boxing is just one reason. Another one is that clojure structs seem to always act as generic maps even if only predefined elements are actually stored. This seems like an easy one to optimize. But if I put lots of structs in a Java ArrayList, memory use goes down significantly, so clojure vectors are much less efficient regardless of boxing.
- pmjordan 17y agoHave you poked around the (Java) source for clojure's data structures? It's very readable, and if you have any ideas for improving their memory footprint, I think a lot of people (including myself) would appreciate hearing about them.