-
Notifications
You must be signed in to change notification settings - Fork 61
Open
Labels
Description
Since the order of elements in the dictionary is guaranteed in python 3.7, it would be good to use a standard dictionary instead of OrderedDict - this will save memory and slightly improve performance. Though, probably, at the size of 128 elements it does not matter much.
$ ipython
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from collections import OrderedDict
In [2]: import sys
In [3]: std_dict = {i: object() for i in range(128)}
In [4]: ord_dict = OrderedDict((i, object()) for i in range(128))
In [5]: sys.getsizeof(std_dict)
Out[5]: 4704
In [6]: sys.getsizeof(ord_dict)
Out[6]: 10912
In [7]: %timeit for i in range(128): i in std_dict
3.79 µs ± 6.01 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [8]: %timeit for i in range(128): i in ord_dict
4.17 µs ± 44.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In addition, it seems that there is an error in the _cache_hit method: probably, in this case, the key should be moved to the beginning, not the end of the dictionary. Although I do not really understand what in general can seriously affect the movement of the key on small amounts of data.