In Julia, if I delete! a key and value from a dictionary, will it re-allocate the dictionary?

Viewed 2221

Specifically, I'm interested in speed. If I remove an entry from a dictionary, will it immediately re-allocate the space used by the dictionary? Would it be faster to just set the value to nothing?

1 Answers

If you delete! an entry from the dictionary then it is not re-allocated. Actually dictionary currently does not support shrinking (even empty! does not shrink it) - it can only grow if needed.

This means that you have added a lot of entries to the dict and then removed them then sometimes it would save memory to copy these remaining elements to a new dictionary element by element (but not using copy as it will create copy with the memory footprint that is the same as the source).

Note, however, that if keys or values are not bits type then removing an entry from a dictionary will remove a reference to it (which means that Julia will be able to garbage collect them if there are no other references to them).

Related