Does neko use String Interning and even better - Int / Float Interning as well?

47 views
Skip to first unread message

der Raab

unread,
Sep 27, 2016, 4:43:20 AM9/27/16
to Haxe
Hi again - while working with huge data in memory I care about memory consumption more than ever. :) I found out that usually identical strings will allocated just once and reused (I guess using pointers under the hood?) - at least that's what I found online. See http://programmers.stackexchange.com/questions/325811/memory-usage-of-javascript-string-type-with-identical-values/325817 .

So I wonder - does neko support that as well? And do I need to build my own Float / Int / Bool pool to reuse a small set of repeating Integer and Float values? And is this even necessary because I would build a little object for every in value (let's say 0-1000) and then just reference to the specific object representing the value I need. I have millions of little objects containing coordinates. Or does using references and therefore pointers need more memory than actually storing every time a Float or Int? Or is there Float Interning built in as well...?

This huge data stuff is new to me. Thanks in advance!

Nicolas Cannasse

unread,
Sep 27, 2016, 1:49:56 PM9/27/16
to haxe...@googlegroups.com
This apply to neko:

Literal strings ("Hello" in code) are memory-shared. Strings built from
code (such as "Hel"+"lo") are not, even if the code contains such String.

Float are the same as strings.

Int are unboxed, meaning they don't take any memory unless you need the
full 32 bits (that is if you have numbers larger than +/-1GB), in which
case they are not shared.

Best,
Nicolas

der Raab

unread,
Sep 27, 2016, 5:37:37 PM9/27/16
to Haxe
Thanks for helping me! Just to make sure I understood correctly - at least for neko there are only two cases where the memory is shared automatically:

- Int values - no matter if generated at runtime through calculation or as static fields
- String values - but only if declared in code
- Float values - but only if declared in code

I did some research about "unboxed" and I think I somehow understand what it means - and therefore I hope I got you right and "don't take any money" does just mean they share memory as well. :)

So now I need to understand how much memory a pointer actually allocates and if it worth to create a map with little objects holding 20.000 "FloatObject"s like {value:0.0} with values from -100.00 to 100.00 - which will be used a million times in fields pointing to a FloatObject rather than always having a field of type Float.

So the question is (and I did some research without really understanding it): How much memory does a pointer use? If I get that right a pointer is just a reference to a object stored somewhere else in memory and therefore must be some kind of address which might be an Integer? And if it's an Integer - will every pointer to the same object share it's memory and therefore exist only once  even if I point to an object a thousand times?
Reply all
Reply to author
Forward
0 new messages