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
Armin's method cache optimization updated for Python 2.6 #44842
I've forward ported and slightly cleaned up Armin's method cache patch (see bpo-1685986). I've attempted to clarify and tighten up several loose ends in the code, so hopefully I haven't mucked anything up.
My performance results are not quite as good as Armin's, though still very encouraging. I see a typical speed up of 10%, with heavily object oriented application code seeing speedups of 15-20%. Given these rather significant results, the major task is to verify correctness.
I tried this test on parrotbench (b0.py in particular) and I'm not sure I could distinguish an improvement over the noise (best case was about 5%). The variability is pretty high on my box (dual amd64 opertons, ubuntu gcc 4.1.2-ish). At first it seemed a bit slower with the original patch which uses unsigned ints. I tried changing to unsigned longs. It seemed a little better, though not sure if it was really due to the change. I think unsigned longs should be used for 64-bit boxes.
Did you use a benchmark/program that is open source? I don't know that I have anything decent to test this with. Raymond probably does though. Also what platform did you test on?
bioinformed commented on Apr 14, 2007
bioinformedmannequin
MannequinAuthor
More actions
I benchmarked using pystone, pybench, and a bunch of my local scientific applications that have tight computational kernels still in pure Python. I tested on a 64bit Linux box, defining the version_tag as either int or unsigned long with no appreciable difference in performance. I'm trying to get parrotbench to run, but it doesn't seem to have been updated for modern Python versions.
arigo commented on May 14, 2007
arigomannequin
Mannequin
More actions
A further minor improvement could be achieved by
re-doing the inlining of _PyType_Lookup() in
object.c. Ideally, the fast path only could be
inlined, and the rest put in a separate function
or just using _PyType_Lookup().
bioinformed commented on May 14, 2007
bioinformedmannequin
MannequinAuthor
More actions
I tried re-inlining the fast path from _PyType_Lookup() in object.c and found no measurable improvement on the simple benchmarks I tried. I've also stress-tested the patch by disabling the fast-path return, always performing the slow-path lookup, and asserting that the cached result matches the slow-path result. I then ran that modified interpreter on the Python test-suite, various benchmarks, and a range of my own applications. While not a formal proof of correctness, it was encouraging that the cache remained consistent.
peaker commented on Jun 10, 2007
peakermannequin
Mannequin
More actions
Why is CPython copying functions to inline them?
This is not only code duplication that makes the code easy to break, it also bloats the caller function beyond readability.
I believe that using a private .c #include file with an inline keyword, or at worst, #include the function code directly is a better solution.
Is there any reason to use the method currently used by CPython?
bioinformed commented on Jun 10, 2007
bioinformedmannequin
MannequinAuthor
More actions
First off, all versions of this patch do away with the rather aggressively repeated inline code. My previous comment about refactoring and testing an inlined form were purely an experiment with null results.
That aside, you do raise a good question. However, given the current patch, it is unfortunately off-topic and irrelevant to the consideration of this patch. Please feel free to pursue it elsewhere, since I worry that it will only serve as a negative distraction from the much more interesting aims of the mro optimization. Since you are clearly worried about the performance of attribute lookup, please try it out and report your findings. I'll be happy review the results from your benchmarks and any suggestions you have.
I may be missing something trivial here, but why the cache global - and not per-typeobject?
As I saw it - the mro cache was supposed to be a mapping of (type,name)->(descriptor/classattr) that's based on the __mro__ of type, and all of the dicts in type and its bases.
I think the right way to do it is to use another Pythonic dict object in each type, that holds this cache (name->(descriptor/classattr)).
This cache dict should essentially be equivalent to a merge of all the dicts in the mro. and gets updated whenever any of the dicts of the types in the mro gets updated.
Not sure why to re-implement a fast-hashtable when one already exists.
Also - once it is associated with the type - it does not hold any references to any names that are not already referenced by someone in the type hierarchy, so there are no mm/gc issues either.
Am I missing something here?
bioinformed commented on Jun 10, 2007
bioinformedmannequin
MannequinAuthor
More actions
The cache is global to make it fast, have a deterministic and low memory footprint, and be easy to invalidate. It is distinct from the standard Python dict implementation since it need not be as general nor fully associative, as cache collisions are not subject to re-probing. Neither does it even go through the motions of memory management, which would be rather hard to get the standard dict implementation to emulate. It is kept drastically minimal in the interests of performance and goes to great pains to hide and minimize the impact to the rest of the interpreter. In this regard it seems quite successful, as it even removes the need for the manually inlined code you commented on before. It also survives harsh stress-testing and presents no detectable performance regressions in any of my benchmarks (to the contrary, many operations are significantly faster).
While a per-type-object cache sounds feasible, it is hard for me to see how it could be as efficient in the general case -- for one, the dict look up fast path is just so much heavier. Please feel free to take that as a challenge to produce a patch that contradicts my intuition.
arigo commented on Jun 10, 2007
arigomannequin
Mannequin
More actions
peaker: we tried several approaches (including ones similar to what you describe) in PyPy, and the one in this patch is the one that worked best there. There is of course no guarantee that the same experiments would have given exactly the same results in CPython, so feel free to try them all again - I won't :-)
Backed out again in r59940 -- test_ctypes fails in test_incomplete.py.
Armin reports that with his original patch on 2.4, this test passes.
bioinformed commented on Jan 13, 2008
bioinformedmannequin
MannequinAuthor
More actions
All tests passed when I first ported Armin's patch to 2.6. I'll have a
chance to look into this later in the week. If anyone gets to it first,
please summarize your findings here to avoid duplication of effort.
bioinformed commented on Jan 13, 2008
bioinformedmannequin
MannequinAuthor
More actions
Couldn't resist looking into this more and I must admit that I am a bit
stumped. If instead of returning a value from the cache fast path in
_PyType_Lookup, I modified the code to store the value and assert that
it matches with the non-cached result from traversing the mro. That
assertion never fails. However, test_incomplete works with this change
that ideally should not alter the semantics of _PyType_Lookup.
More so, if I selectively don't cache the name "next" (with the caching
fast path reenabled), test_incomplete works. For test_incomplete,
_PyType_Lookup on "next" always return NULL from both the cached and
non-cached paths.
I'm beginning to suspect there is some black magic going on. None of
the relevant reference counts drop to zero, the same return value (NULL)
is generated, but the slow patch seems to be doing something with a side
effect that makes it work.
Wish I had more time to debug this, but I'm already overdrawn until
Wednesday...
I think that these changes are sensible: The first one deal with the
type's attribute instead of updating its __dict__, and the second
properly delegates "tp_setattro" to the base class ('type' in this case).
For the record:
It is very wrong to call self->ob_type->tp_base's slot: slots are often
copied from their base class, and you get infinite recursion.
Calling StructType.tp_base->tp_setattro is much better.
Activity
bioinformed commentedon Apr 13, 2007
I've forward ported and slightly cleaned up Armin's method cache patch (see bpo-1685986). I've attempted to clarify and tighten up several loose ends in the code, so hopefully I haven't mucked anything up.
My performance results are not quite as good as Armin's, though still very encouraging. I see a typical speed up of 10%, with heavily object oriented application code seeing speedups of 15-20%. Given these rather significant results, the major task is to verify correctness.
nnorwitz commentedon Apr 14, 2007
I tried this test on parrotbench (b0.py in particular) and I'm not sure I could distinguish an improvement over the noise (best case was about 5%). The variability is pretty high on my box (dual amd64 opertons, ubuntu gcc 4.1.2-ish). At first it seemed a bit slower with the original patch which uses unsigned ints. I tried changing to unsigned longs. It seemed a little better, though not sure if it was really due to the change. I think unsigned longs should be used for 64-bit boxes.
Did you use a benchmark/program that is open source? I don't know that I have anything decent to test this with. Raymond probably does though. Also what platform did you test on?
bioinformed commentedon Apr 14, 2007
I benchmarked using pystone, pybench, and a bunch of my local scientific applications that have tight computational kernels still in pure Python. I tested on a 64bit Linux box, defining the version_tag as either int or unsigned long with no appreciable difference in performance. I'm trying to get parrotbench to run, but it doesn't seem to have been updated for modern Python versions.
arigo commentedon May 14, 2007
A further minor improvement could be achieved by
re-doing the inlining of _PyType_Lookup() in
object.c. Ideally, the fast path only could be
inlined, and the rest put in a separate function
or just using _PyType_Lookup().
bioinformed commentedon May 14, 2007
I tried re-inlining the fast path from _PyType_Lookup() in object.c and found no measurable improvement on the simple benchmarks I tried. I've also stress-tested the patch by disabling the fast-path return, always performing the slow-path lookup, and asserting that the cached result matches the slow-path result. I then ran that modified interpreter on the Python test-suite, various benchmarks, and a range of my own applications. While not a formal proof of correctness, it was encouraging that the cache remained consistent.
peaker commentedon Jun 10, 2007
Why is CPython copying functions to inline them?
This is not only code duplication that makes the code easy to break, it also bloats the caller function beyond readability.
I believe that using a private .c #include file with an inline keyword, or at worst, #include the function code directly is a better solution.
Is there any reason to use the method currently used by CPython?
bioinformed commentedon Jun 10, 2007
First off, all versions of this patch do away with the rather aggressively repeated inline code. My previous comment about refactoring and testing an inlined form were purely an experiment with null results.
That aside, you do raise a good question. However, given the current patch, it is unfortunately off-topic and irrelevant to the consideration of this patch. Please feel free to pursue it elsewhere, since I worry that it will only serve as a negative distraction from the much more interesting aims of the mro optimization. Since you are clearly worried about the performance of attribute lookup, please try it out and report your findings. I'll be happy review the results from your benchmarks and any suggestions you have.
rhettinger commentedon Jun 10, 2007
FWIW, I'm still reviewing this code.
peaker commentedon Jun 10, 2007
I may be missing something trivial here, but why the cache global - and not per-typeobject?
As I saw it - the mro cache was supposed to be a mapping of (type,name)->(descriptor/classattr) that's based on the __mro__ of type, and all of the dicts in type and its bases.
I think the right way to do it is to use another Pythonic dict object in each type, that holds this cache (name->(descriptor/classattr)).
This cache dict should essentially be equivalent to a merge of all the dicts in the mro. and gets updated whenever any of the dicts of the types in the mro gets updated.
Not sure why to re-implement a fast-hashtable when one already exists.
Also - once it is associated with the type - it does not hold any references to any names that are not already referenced by someone in the type hierarchy, so there are no mm/gc issues either.
Am I missing something here?
bioinformed commentedon Jun 10, 2007
The cache is global to make it fast, have a deterministic and low memory footprint, and be easy to invalidate. It is distinct from the standard Python dict implementation since it need not be as general nor fully associative, as cache collisions are not subject to re-probing. Neither does it even go through the motions of memory management, which would be rather hard to get the standard dict implementation to emulate. It is kept drastically minimal in the interests of performance and goes to great pains to hide and minimize the impact to the rest of the interpreter. In this regard it seems quite successful, as it even removes the need for the manually inlined code you commented on before. It also survives harsh stress-testing and presents no detectable performance regressions in any of my benchmarks (to the contrary, many operations are significantly faster).
While a per-type-object cache sounds feasible, it is hard for me to see how it could be as efficient in the general case -- for one, the dict look up fast path is just so much heavier. Please feel free to take that as a challenge to produce a patch that contradicts my intuition.
arigo commentedon Jun 10, 2007
peaker: we tried several approaches (including ones similar to what you describe) in PyPy, and the one in this patch is the one that worked best there. There is of course no guarantee that the same experiments would have given exactly the same results in CPython, so feel free to try them all again - I won't :-)
2 remaining items
rhettinger commentedon Dec 21, 2007
Okay, this patch is accepted. We'll likely do additional tweaks after
it goes in.
Please close the other tracker items for this same issue.
If no one applies this, I'll do it when I return from the holidays.
birkenfeld commentedon Jan 12, 2008
Committed to trunk as r59931. Leaving open if you want to make more
adjustments, Raymond.
birkenfeld commentedon Jan 13, 2008
Backed out again in r59940 -- test_ctypes fails in test_incomplete.py.
Armin reports that with his original patch on 2.4, this test passes.
bioinformed commentedon Jan 13, 2008
All tests passed when I first ported Armin's patch to 2.6. I'll have a
chance to look into this later in the week. If anyone gets to it first,
please summarize your findings here to avoid duplication of effort.
bioinformed commentedon Jan 13, 2008
Couldn't resist looking into this more and I must admit that I am a bit
stumped. If instead of returning a value from the cache fast path in
_PyType_Lookup, I modified the code to store the value and assert that
it matches with the non-cached result from traversing the mro. That
assertion never fails. However, test_incomplete works with this change
that ideally should not alter the semantics of _PyType_Lookup.
More so, if I selectively don't cache the name "next" (with the caching
fast path reenabled), test_incomplete works. For test_incomplete,
_PyType_Lookup on "next" always return NULL from both the cached and
non-cached paths.
I'm beginning to suspect there is some black magic going on. None of
the relevant reference counts drop to zero, the same return value (NULL)
is generated, but the slow patch seems to be doing something with a side
effect that makes it work.
Wish I had more time to debug this, but I'm already overdrawn until
Wednesday...
amauryfa commentedon Jan 14, 2008
After some debug work, I found an explanation:
during the type construction.
__dict_ is replaced by a "stgdict".
call to _PyType_Lookup()
I have come with a patch which corrects the problem, and all ctypes
tests pass:
Index: Modules/_ctypes/stgdict.c
===================================================================
I think that these changes are sensible: The first one deal with the
type's attribute instead of updating its __dict__, and the second
properly delegates "tp_setattro" to the base class ('type' in this case).
rhettinger commentedon Jan 14, 2008
Nice analysis. Please apply.
amauryfa commentedon Jan 14, 2008
OK, I apply first my 2 lines, then the original patch.
amauryfa commentedon Jan 14, 2008
For the record:
It is very wrong to call self->ob_type->tp_base's slot: slots are often
copied from their base class, and you get infinite recursion.
Calling StructType.tp_base->tp_setattro is much better.
collinwinter commentedon Jan 15, 2009
Looks like this was re-applied in r59943 and r59944. Thanks for taking
care of this, Amaury.