You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid repeated hash calculation in C implementation of functools.lru_cache() #68671
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/rhettinger'closed_at=<Date2015-10-02.10:16:19.873>created_at=<Date2015-06-21.12:34:02.134>labels= ['library', 'performance']
title='Avoid repeated hash calculation in C implementation of functools.lru_cache()'updated_at=<Date2015-10-02.10:27:08.921>user='https://github.com/serhiy-storchaka'
There is a difference between Python and C implementations of functools.lru_cache(). Python implementation caches the hash of the key, C implementation doesn't. May be this is not too important (at least I have no an example that shows the benefit of caching the hash), but there is a place for possible optimization. Proposed patch uses private functions _PyDict_GetItem_KnownHash() and _PyDict_SetItem_KnownHash() to avoid the second calculating of the hash for new keys.
The hash is still calculated second time when the key is deleted from full cache. To avoid this _PyDict_DelItem_KnownHash() is needed.
I would like the full functionality of the Python version to be implemented. Guaranteeing that the hash is only calculated once prevents a reentrancy hole and provides a speed benefit as well. Please implement exactly what the pure python version does (no more than one call to hash ever).
I can accept this change, but I don't like that code. Is it really considered acceptable to have that much copy-and-paste code in the dict implementation for KnownHash calls?
Could the common code be split off into a Py_LOCAL_INLINE function?
I suggest that 20 lines of identical code copy-and-pasted between two functions is not the cleanest way to do it. Find attached my version of the patch, which splits this common code out into a static function.
Sorry Larry, but I prefer Serhiy's version. The known_hash variants need to have their own function intact version and gum up the rest of the code. See the current known_hash function for comparison (Serhiy modeled on what we had already decided to do much earlier on a separate issue).
I would prefer do not repeat the code, but I afraid this can affect the performance, and the performance of PyDict_GetItem is critical for Python.
Static function calls like that one will most likely be inlined by the compiler... But if you don't want to rely on that, maybe we can use a macro to avoid code duplication?
changed the title [-]Avoid repeated hash calculation in C implementation of functools.lru_cache()[/-][+]Avoid repeated hash calculation in C implementation of functools.lru_cache()[/+]on Jun 24, 2015
Please let this go forward as Serhiy has written it. That is the way I wrote the existing known hash function. You all should be focused on correctness, not on bike-shedding my and Serhiy's coding style.
+1 to what Raymond says here. Concerns regarding code duplication need to be weighed against the likelihood of that code changing in the future, and the impact on the readability of each copy of the code in isolation.
In this particular case, I think the likelihood of future change is low, and the likely negative impact of consolidation on readability is high, so it makes sense to me to defer consolidation until there's a clear benefit to sharing the implementations.
I did some regression testing and reviewed the code; LGTM.
As for the code structure issues, I agree that the duplication is undesirable (the readability argument is not that convincing), but Serhiy's patch is consistent with the existing design. As such, I think the structure issue is a separate one and definitely should not hold this patch up.
New changeset 3f4c319a822f by Serhiy Storchaka in branch '3.5':
Issue bpo-24483: C implementation of functools.lru_cache() now calculates key's https://hg.python.org/cpython/rev/3f4c319a822f
I didn't follow this issue, but I like the change. Good job :)
Activity
serhiy-storchaka commentedon Jun 21, 2015
There is a difference between Python and C implementations of functools.lru_cache(). Python implementation caches the hash of the key, C implementation doesn't. May be this is not too important (at least I have no an example that shows the benefit of caching the hash), but there is a place for possible optimization. Proposed patch uses private functions _PyDict_GetItem_KnownHash() and _PyDict_SetItem_KnownHash() to avoid the second calculating of the hash for new keys.
The hash is still calculated second time when the key is deleted from full cache. To avoid this _PyDict_DelItem_KnownHash() is needed.
rhettinger commentedon Jun 21, 2015
I would like the full functionality of the Python version to be implemented. Guaranteeing that the hash is only calculated once prevents a reentrancy hole and provides a speed benefit as well. Please implement exactly what the pure python version does (no more than one call to hash ever).
serhiy-storchaka commentedon Jun 21, 2015
New patch adds _PyDict_DelItem_KnownHash() and uses it to guarantee that the hash is only calculated once.
serhiy-storchaka commentedon Jun 22, 2015
New patch touches also unbounded cache version.
Larry, do you allow to commit such patch in 3.5?
larryhastings commentedon Jun 22, 2015
I can accept this change, but I don't like that code. Is it really considered acceptable to have that much copy-and-paste code in the dict implementation for KnownHash calls?
Could the common code be split off into a Py_LOCAL_INLINE function?
rhettinger commentedon Jun 22, 2015
Serhiy's code looks like the cleanest way to do it.
larryhastings commentedon Jun 22, 2015
Patch doesn't build for me against current trunk:
serhiy-storchaka commentedon Jun 22, 2015
My bad. I submitted the last patch without checking (rebuilding Python takes too much time on my slow netbook). Here is fixed and tested patch.
larryhastings commentedon Jun 23, 2015
I suggest that 20 lines of identical code copy-and-pasted between two functions is not the cleanest way to do it. Find attached my version of the patch, which splits this common code out into a static function.
rhettinger commentedon Jun 24, 2015
Sorry Larry, but I prefer Serhiy's version. The known_hash variants need to have their own function intact version and gum up the rest of the code. See the current known_hash function for comparison (Serhiy modeled on what we had already decided to do much earlier on a separate issue).
serhiy-storchaka commentedon Jun 24, 2015
I would prefer do not repeat the code, but I afraid this can affect the performance, and the performance of PyDict_GetItem is critical for Python.
1st1 commentedon Jun 24, 2015
Static function calls like that one will most likely be inlined by the compiler... But if you don't want to rely on that, maybe we can use a macro to avoid code duplication?
serhiy-storchaka commentedon Jun 24, 2015
This will not make the code cleaner.
In any case this is other issue.
[-]Avoid repeated hash calculation in C implementation of functools.lru_cache()[/-][+]Avoid repeated hash calculation in C implementation of functools.lru_cache()[/+]rhettinger commentedon Jun 24, 2015
Please let this go forward as Serhiy has written it. That is the way I wrote the existing known hash function. You all should be focused on correctness, not on bike-shedding my and Serhiy's coding style.
ncoghlan commentedon Jun 25, 2015
+1 to what Raymond says here. Concerns regarding code duplication need to be weighed against the likelihood of that code changing in the future, and the impact on the readability of each copy of the code in isolation.
In this particular case, I think the likelihood of future change is low, and the likely negative impact of consolidation on readability is high, so it makes sense to me to defer consolidation until there's a clear benefit to sharing the implementations.
meadori commentedon Jun 25, 2015
I did some regression testing and reviewed the code; LGTM.
As for the code structure issues, I agree that the duplication is undesirable (the readability argument is not that convincing), but Serhiy's patch is consistent with the existing design. As such, I think the structure issue is a separate one and definitely should not hold this patch up.
taleinat commentedon Jun 29, 2015
With clru_cache_known_hash_4.patch on the current default branch, the entire test suite passes here (OSX 10.10).
larryhastings commentedon Jul 3, 2015
This can go in for 3.5 beta 3.
serhiy-storchaka commentedon Jul 14, 2015
Can you allow me to commit the patch or will commit it yourself Raymond?
taleinat commentedon Jul 20, 2015
Ping? Let's not miss the final 3.5 beta.
serhiy-storchaka commentedon Sep 28, 2015
Ping.
vstinner commentedon Sep 29, 2015
clru_cache_known_hash_5.larry.patch looks good to me. Python 3.5 has no more restriction to push patches, go ahead Serhiy, push it to 3.5 & 3.6.
python-dev commentedon Oct 2, 2015
New changeset 3f4c319a822f by Serhiy Storchaka in branch '3.5':
Issue bpo-24483: C implementation of functools.lru_cache() now calculates key's
https://hg.python.org/cpython/rev/3f4c319a822f
New changeset 5758b85627c9 by Serhiy Storchaka in branch 'default':
Issue bpo-24483: C implementation of functools.lru_cache() now calculates key's
https://hg.python.org/cpython/rev/5758b85627c9
vstinner commentedon Oct 2, 2015
I didn't follow this issue, but I like the change. Good job :)