Tuesday, November 3, 2015

Proper use of hashes in Hellcore

Problem: the lambamoo C server keeps allocated memory in process when you allocate it. So if you have a large hash assigned to a variable this big chunk of memory will be stuck in the MOO process until you reboot the MOO.  The server will properly free the memory when the variable is destroyed or out of use, but the process itself will hang onto this memory forever.

 Solution: Do not assign hashes to variables. Do not assign the results of keys() or values() to variables.

 Wrong code:
rkeys = keys($ods.bigkeyhash);
for x in (rkeys)
"something something";
endfor

 Correct code:
for x in (keys($ods.bigkeyhash))
"something something";
endfor

Hash Warnings:

The following are things to avoid when using hashes in hellcore.  Using these codes can crash the entire MOO and even induce data corruption (this is really bad!).

Do not iterate directly through a hash:

Wrong:
for x in (somehash)

Correct:

for x in (keys(somehash))

Do not modify values while iterating through a hash (guaranteed crash):

Wrong:
for x in (somehash)
somehash[x] = newvalue;
endfor

Correct:
for x in (keys(somehash))
somehash[x] = newvalue;
endfor

These are covered in the HELP HASHES programmer help, but are included here for reference.