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
I am working on implementing nameduple in C; I am almost there; however, on the path of moving to full compatibility, I ran into a refcount issue somewhere. Hopefully someone can help me work this out.
To describe the issue, When I run the collections tests I most frequently segfault in a non namedtuple test; however, sometimes it runs (and passes) however that probably means I am using an unowned obect somewhere. I am new to the CPython API so I would love to go through this with someone to learn more about how it all works.
I am currently at PyCon and would absolutly love to speak to someone in person. I will be at CPython sprints for at least one day.
Ideally, namedtuple is used to make your code cleaner, using "magic" indecies is less clear than using a named index in a lot of cases. Because namedtuple is mainly to make the code more readable, I don't think that it should have an impact on the runtime performance of the code. This means that namedtuple should be a very thin wrapper around tuple. Currently, namedtuple named item access is much slower than elementwise access. I have this as a standalone package (there are some changes in the diff I posted to acheive full backwards compat) here https://pypi.python.org/pypi/cnamedtuple/0.1.5 that show some profiling runs of this code. The notable one to me is named item access being around twice as fast.
Another issue we ran into at work is that we found ways to get the exec call in nametuple to execute arbitrary code; while this would not be a concern for most people by the nature of the way we got this to happen, we wanted to look at other ways of writing namedtuple. I looked through some older discussion and found that non-exec solutions were rejected in the past for perfomance reasons.
Have you thought of just exposing Object/structseq.c?
Before you put much time into this, I'd get Raymond's acceptance of whatever approach you want to take. It might be best to raise it on python-ideas.
llllllllll commented on Apr 10, 2015
llllllllllmannequin
MannequinAuthor
More actions
would the idea be to deprecate namedtuple in favor of a public structseq that is exposed through collections, or change structseq to fit the namedtuple API?
Can you post before and afters timings of the patch?
llllllllll commented on Apr 12, 2015
llllllllllmannequin
MannequinAuthor
More actions
# Original version / new python implementation
./python -m timeit -s "from collections import namedtuple;a = namedtuple('a', 'a b c')(1, 2, 3)" "a.a"
10000000 loops, best of 3: 0.07 usec per loop
# C implementation
./python -m timeit -s "from collections import namedtuple;a = namedtuple('a', 'a b c')(1, 2, 3)" "a.a"
10000000 loops, best of 3: 0.028 usec per loop
The fallback is the same implementation that is currently used so this should have no affect on pypi.
llllllllll commented on Apr 12, 2015
llllllllllmannequin
MannequinAuthor
More actions
sorry, I meant pypy
llllllllll commented on Apr 14, 2015
llllllllllmannequin
MannequinAuthor
More actions
I am updating the patch to include an entry in Misc/NEWS.
FWIW, the current property(itemgetter(index)) code has a Python creation step, but the actual attribute lookup and dispatch is done entirely in C (no pure python steps around the eval lookup).
Rather than making a user visible C hack directly to namedtuple, any optimization effort should be directly at improving the speed of property() and itemgetter().
I haven't looked too closely at this, but I think you get the general idea. If the speed of property(itemgetter(index)) is the bottleneck in your code, the starting point is to unwind the exact series of C steps performed to see if any of them can be simplified. For the most part, the code in property() and itemgetter() were both implemented using the simplest C parts of the C API rather than the fastest. The chain of calls isn't specialized for the common use case (i.e. property_get() needing exactly 1 argument rather than a variable length arg tuple and itemgetter doing a known integer offset on a list or tuple rather than the less common case of generic types and a possible tuple of indices).
We should start by optimizing what we've already got. That will have a benefit beyond named tuples (faster itemgetters for sorting and faster property gets for the entire language).
It also helps us avoid making the NT code less familiar (using custom private APIs rather than generic, well-known components).
It also reduces the risk of breaking code that relies on the published implementation of named tuple attribute lookups (for example, I've seen deployed code that customizes the attribute docstrings like this):
Point = namedtuple('Point', ['x', 'y'])
Point.x = property(Point.x.fget, doc='abscissa')
Point.y = property(Point.y.fget, doc='ordinate')
coordinate = Point(x=250, y=43)
Hmm, the presense of _PyTuple_DebugMallocStats, repeat_traverse, and visit_decref suggests that the profile may have been run with debugging code enabled and GC enabled.
The property patch looks good. Depending on how far you want to go with this, you could save your tuple of length-1 statically and reuse it on successive calls if its refcnt doesn't grow (see the code for zip() for an example of how to do this). That would save the PyTuple_New and tupledealloc calls.
Going further, potentially you could in-line some of the code it PyObject_Call, caching the callsite and its NULL check, and looking at the other steps to see if they are all necessary in the context of a property_desc_get().
llllllllll commented on Apr 27, 2015
llllllllllmannequin
MannequinAuthor
More actions
I switched to the static tuple.
llllllllll commented on Apr 27, 2015
llllllllllmannequin
MannequinAuthor
More actions
I don't think that I can cache the __call__ of the fget object because it might be an instance of a heaptype, and if someone changed the __class__ of the object in between calls to another heaptype that had a different __call__, you would still get the __call__ from the first type. I also don't know if this is supported behavior or just something that works by accident.
I read through PyObject_Call, and all the code is needed assuming we are not caching the tp_call value.
I am currently on a different machine so these numbers are not relative to the others posted earlier.
default
./python -m timeit -s "from collections import namedtuple as n;a = n('n', 'a b c')(1, 2, 3)" "a.a"
10000000 loops, best of 3: 0.0699 usec per loop
patch
./python -m timeit -s "from collections import namedtuple as n;a = n('n', 'a b c')(1, 2, 3)" "a.a"
10000000 loops, best of 3: 0.0468 usec per loop
For the pre-computed 1-tuple, I think you need to check for a refcnt of 1 and fallback to PyTuple_New if the tuple is in use (i.e. a property that calls another property). See Objects/enumobject.c::105 for an example.
Also, consider providing a way to clean-up that tuple on shutdown. For example, look at what is done with the various freelists. An easier way is to make the premade tuple part of the property object struct so that it gets freed when the property is deallocated.
Adding Serhiy to the nosy list, he can help with cleaning-up the patch so that it is ready to apply.
llllllllll commented on Apr 29, 2015
llllllllllmannequin
MannequinAuthor
More actions
I don't think that we need to worry about reusing the single argument tuple in a recursive situation because we never need the value after we start the call. We also just write our new value and then clean up with a NULL to make sure that we don't blow up when we dealloc the tuple. For example:
This works with recursive properties.
I also think that is is getting cleaned up on shutdown, if I put a pointer to garbage in the tuple, the interpreter blows up on shutdown; this makes me think that tuple_dealloc is being called somewhere. About putting the tuple on the property instance, that would nice for memory management; however, that increases the memory overhead of each property. This also means that we would only get the faster lookup after the property has been accessed once; this is fine but the current implementation makes it so that all properties are faster after any of them are looked up once. I could be wrong about the cleanup though.
I am also updating the title and headers because this issue is no longer about namedtuple.
Activity
llllllllll commentedon Apr 10, 2015
I am working on implementing nameduple in C; I am almost there; however, on the path of moving to full compatibility, I ran into a refcount issue somewhere. Hopefully someone can help me work this out.
To describe the issue, When I run the collections tests I most frequently segfault in a non namedtuple test; however, sometimes it runs (and passes) however that probably means I am using an unowned obect somewhere. I am new to the CPython API so I would love to go through this with someone to learn more about how it all works.
I am currently at PyCon and would absolutly love to speak to someone in person. I will be at CPython sprints for at least one day.
ericvsmith commentedon Apr 10, 2015
What's the motivating use case for this?
llllllllll commentedon Apr 10, 2015
Ideally, namedtuple is used to make your code cleaner, using "magic" indecies is less clear than using a named index in a lot of cases. Because namedtuple is mainly to make the code more readable, I don't think that it should have an impact on the runtime performance of the code. This means that namedtuple should be a very thin wrapper around tuple. Currently, namedtuple named item access is much slower than elementwise access. I have this as a standalone package (there are some changes in the diff I posted to acheive full backwards compat) here https://pypi.python.org/pypi/cnamedtuple/0.1.5 that show some profiling runs of this code. The notable one to me is named item access being around twice as fast.
Another issue we ran into at work is that we found ways to get the exec call in nametuple to execute arbitrary code; while this would not be a concern for most people by the nature of the way we got this to happen, we wanted to look at other ways of writing namedtuple. I looked through some older discussion and found that non-exec solutions were rejected in the past for perfomance reasons.
ericvsmith commentedon Apr 10, 2015
Have you thought of just exposing Object/structseq.c?
Before you put much time into this, I'd get Raymond's acceptance of whatever approach you want to take. It might be best to raise it on python-ideas.
llllllllll commentedon Apr 10, 2015
would the idea be to deprecate namedtuple in favor of a public structseq that is exposed through collections, or change structseq to fit the namedtuple API?
ericvsmith commentedon Apr 10, 2015
I haven't seen thought it through, just that it seems very similar to a C namedtuple.
llllllllll commentedon Apr 11, 2015
I stripped down the patch to only the descriptor like we had discussed.
rhettinger commentedon Apr 12, 2015
Can you post before and afters timings of the patch?
llllllllll commentedon Apr 12, 2015
# Original version / new python implementation
./python -m timeit -s "from collections import namedtuple;a = namedtuple('a', 'a b c')(1, 2, 3)" "a.a"
10000000 loops, best of 3: 0.07 usec per loop
# C implementation
./python -m timeit -s "from collections import namedtuple;a = namedtuple('a', 'a b c')(1, 2, 3)" "a.a"
10000000 loops, best of 3: 0.028 usec per loop
The fallback is the same implementation that is currently used so this should have no affect on pypi.
llllllllll commentedon Apr 12, 2015
sorry, I meant pypy
llllllllll commentedon Apr 14, 2015
I am updating the patch to include an entry in Misc/NEWS.
rhettinger commentedon Apr 26, 2015
FWIW, the current property(itemgetter(index)) code has a Python creation step, but the actual attribute lookup and dispatch is done entirely in C (no pure python steps around the eval lookup).
Rather than making a user visible C hack directly to namedtuple, any optimization effort should be directly at improving the speed of property() and itemgetter().
Here are some quick notes to get you started:
I haven't looked too closely at this, but I think you get the general idea. If the speed of property(itemgetter(index)) is the bottleneck in your code, the starting point is to unwind the exact series of C steps performed to see if any of them can be simplified. For the most part, the code in property() and itemgetter() were both implemented using the simplest C parts of the C API rather than the fastest. The chain of calls isn't specialized for the common use case (i.e. property_get() needing exactly 1 argument rather than a variable length arg tuple and itemgetter doing a known integer offset on a list or tuple rather than the less common case of generic types and a possible tuple of indices).
We should start by optimizing what we've already got. That will have a benefit beyond named tuples (faster itemgetters for sorting and faster property gets for the entire language).
It also helps us avoid making the NT code less familiar (using custom private APIs rather than generic, well-known components).
It also reduces the risk of breaking code that relies on the published implementation of named tuple attribute lookups (for example, I've seen deployed code that customizes the attribute docstrings like this):
Point = namedtuple('Point', ['x', 'y'])
Point.x = property(Point.x.fget, doc='abscissa')
Point.y = property(Point.y.fget, doc='ordinate')
coordinate = Point(x=250, y=43)
4 remaining items
rhettinger commentedon Apr 27, 2015
Hmm, the presense of _PyTuple_DebugMallocStats, repeat_traverse, and visit_decref suggests that the profile may have been run with debugging code enabled and GC enabled.
The property patch looks good. Depending on how far you want to go with this, you could save your tuple of length-1 statically and reuse it on successive calls if its refcnt doesn't grow (see the code for zip() for an example of how to do this). That would save the PyTuple_New and tupledealloc calls.
Going further, potentially you could in-line some of the code it PyObject_Call, caching the callsite and its NULL check, and looking at the other steps to see if they are all necessary in the context of a property_desc_get().
llllllllll commentedon Apr 27, 2015
I switched to the static tuple.
llllllllll commentedon Apr 27, 2015
I don't think that I can cache the __call__ of the fget object because it might be an instance of a heaptype, and if someone changed the __class__ of the object in between calls to another heaptype that had a different __call__, you would still get the __call__ from the first type. I also don't know if this is supported behavior or just something that works by accident.
I read through PyObject_Call, and all the code is needed assuming we are not caching the tp_call value.
rhettinger commentedon Apr 27, 2015
What kind of speed improvement have you gotten?
llllllllll commentedon Apr 27, 2015
I am currently on a different machine so these numbers are not relative to the others posted earlier.
default
./python -m timeit -s "from collections import namedtuple as n;a = n('n', 'a b c')(1, 2, 3)" "a.a"
10000000 loops, best of 3: 0.0699 usec per loop
patch
./python -m timeit -s "from collections import namedtuple as n;a = n('n', 'a b c')(1, 2, 3)" "a.a"
10000000 loops, best of 3: 0.0468 usec per loop
rhettinger commentedon Apr 29, 2015
That's pretty good for a small patch :-)
For the pre-computed 1-tuple, I think you need to check for a refcnt of 1 and fallback to PyTuple_New if the tuple is in use (i.e. a property that calls another property). See Objects/enumobject.c::105 for an example.
Also, consider providing a way to clean-up that tuple on shutdown. For example, look at what is done with the various freelists. An easier way is to make the premade tuple part of the property object struct so that it gets freed when the property is deallocated.
Adding Serhiy to the nosy list, he can help with cleaning-up the patch so that it is ready to apply.
llllllllll commentedon Apr 29, 2015
I don't think that we need to worry about reusing the single argument tuple in a recursive situation because we never need the value after we start the call. We also just write our new value and then clean up with a NULL to make sure that we don't blow up when we dealloc the tuple. For example:
This works with recursive properties.
I also think that is is getting cleaned up on shutdown, if I put a pointer to garbage in the tuple, the interpreter blows up on shutdown; this makes me think that tuple_dealloc is being called somewhere. About putting the tuple on the property instance, that would nice for memory management; however, that increases the memory overhead of each property. This also means that we would only get the faster lookup after the property has been accessed once; this is fine but the current implementation makes it so that all properties are faster after any of them are looked up once. I could be wrong about the cleanup though.
I am also updating the title and headers because this issue is no longer about namedtuple.
[-]C implementation of namedtuple (WIP)[/-][+]property_descr_get reuse argument tuple[/+]python-dev commentedon Apr 30, 2015
New changeset 661cdbd617b8 by Raymond Hettinger in branch 'default':
Issue bpo-23910: Optimize property() getter calls. Patch by Joe Jevnik
https://hg.python.org/cpython/rev/661cdbd617b8
serhiy-storchaka commentedon May 24, 2015
This patch caused crash with C implementation of lru_cache (bpo-14373). Opened bpo-24276 for fixing this.
vstinner commentedon Oct 1, 2018
This optimization caused multiple crashes, so it has been decided to remove it :-( See bpo-30156.