Re: Why are my maps chewing on my RAM? or, Adventures in memory (de)allocation
- Posted by ghaberek (admin) Sep 09, 2019
- 1077 views
Spock said...
Please excuse my ignorance but why can't maps be just normal sequences that encapsulate hash tables? Wouldn't that solve memory allocation issues?
You're not wrong, and that's how previous user-contributed implementations have worked. For example...
sequence table = map_new( 256 ) -- number of buckets table = map_insert( table, "key", "value" ) -- insert data, return updated sequence object value = map_fetch( table, "key" ) -- fetch stored data from the sequence
The goal of the eumem library, and its use in maps and stacks is allow pass-by-reference instead of requiring pass-by-value.
map table = map:new() -- 'table' is a reference to the eumem:ram_space sequence map:put( table, "key", "value" ) -- notice there is no return value here object value = map:get( table, "key" ) -- this is pretty much the same
I think there's an advantage to be had in not having to pass-by-value everything. This method, IMHO, makes for cleaner and easier to understand code.
But, like with any other language that deals with passing references, keeping track of when things can be freed becomes an additional burden.
-Greg