12.03.20 17:02, Eric Wieser пише:
     >>> [id(v) for v in itertools.combinations([1, 2, 3], 1)]
     [2500926200992, 2500926199072, 2500926200992]

Note that the first id is repeated for third item. And if you use larger example

 >>> [id(v) for v in itertools.combinations(range(10), 1)]
[139701363452336, 139701363451616, 139701363452336, 139701363451616, 139701363452336, 139701363451616, 139701363452336, 139701363451616, 139701363452336, 139701363451616]

you will see that two ids are interleaving.

There are many causes of reusing the id. First, the combinations() iterator keeps a reference to a tuple and reuse it if it is free. Second, the tuple allocator keeps a pool of preallocated objects for small tuples. Third, the Python memory allocator keeps a pool of small blocks. Fourth, the libc memory allocator also more likely return the same memory address if repeatedly allocate and free the same amount of memory.

All this is optimizations and implementation artifacts. If you are worring that the second tuple produced by combinations() uses more slower cache, you can make a first-level cache in combinations() keeping references to two tuples. Or make the second-level cache more efficient. Of cause if you have evidences that there is a problem and that this solution fixes it. Did you do any benchmarks?
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MUPMOL3W246QUUXM254SIV2P7FBVZG3K/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to