I really don’t understand what you mean. How can you save another 10k dicts?
here’s the idea:
start with the easy operation, empty immutabledict merged with other empty mappings:
EMPTY_DICT = util.immutabledict({})
So the first operation, we are merging some other immutabledicts. suppose they are all empty as well (which in our code, means we started with EMPTY_DICT - we never call util.immutabledict({}) a second time):
a = EMPTY_DICT
b = EMPTY_DICT
c = EMPTY_DICT
new_dict = EMPTY_DICT.merge_with(a, b, c)
immutabledict iterates through the empty dictionaries, sees that they are all empty, and says, hey, we have nothing to merge. Let’s return ourselves:
> new_dict is EMPTY_DICT
True
so if the above EMPTY_DICT merge thing happened 10000 times, we created zero new dictionaries. literally nothing happened.
The next scenario, hey, let’s say we’re empty, and one of those dictionaries has something in it:
a = EMPTY_DICT
b = util.immutabledict({"hey": "some data"})
c = EMPTY_DICT
new_dict = EMPTY_DICT.merge_with(a, b, c)
merge_with() right now sees that dictionary “b” is non-empty. so it creates a new immutabledict, fills up the internal dictionary representation with the data from the left side (EMPTY_DICT in this case), then fills it up with the data from “b”. Here’s an older version of the code that uses plain python:
def merge_with(
self, *dicts: Optional[Mapping[_KT, _VT]]
) -> immutabledict[_KT, _VT]:
new = None
for d in dicts:
if d:
if new is None:
new = ImmutableDictBase.__new__(self.__class__)
dict.__init__(new, self)
dict.update(new, d)
if new is None:
return self
return new
So the above code called 10000 times creates 20000 dictionaries; b = util.immutabledict(...) plus the merge_with operation. We can see that the returned dictionary is new:
>>> new_dict is EMPTY_DICT
False # obviously, because it's not empty
>>> new_dict is b
False # also false
>>> new_dict == b
True # but it has the identical contents
The idea for optimizing the above further is that across a, b, c, EMPTY_DICT, we still have exactly one dictionary that is non-empty. We should just return that:
>>> new_dict is EMPTY_DICT
False # obviously, because it's not empty
>>> new_dict is b
True # because it's identical
>>> new_dict == b
True # but it has the identical contents
So with the above optimization, the code example creates only 10K dictionaries for 10K runs, rather than 20K. that is, it detects that the merge operation produces a dictionary that is identical to one of the dictionaries it already has, so it can return that without making a new one.