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 have an application, the use of Python3.2.3 development.
When I upgrade to Python3.4.2, found a problem.
I have an extended "xxx.c (c-python)" module, I call pickle to serialize and deserialize, _pickle.c calls the whichmodule to look for this module, The final will be to find the module from sys.modules.
But probably there are 200 elements in sys.modules, Use the "obj = getattribute (module, global_name, allow_qualname)" to try to get the object:
tmp=PyObject_GetAttr(obj, subpath);
Py_DECREF(obj);
// There will be hundreds of times to return to NULL// There will be hundreds of times calls "PyErr_Format // (PyExc_AttributeError," Can't get attribute%R on%R ", name, obj);"// This process will lead to hundreds of calls to "moduleobject.c-// module_repr(PyModuleObject *m)".
// So I frequently call pickle.dumps CPU consumption sharply.
if (tmp==NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
PyErr_Format(PyExc_AttributeError,
"Can't get attribute %R on %R", name, obj);
}
Py_DECREF(dotted_path);
returnNULL;
}
...
...
}
In Python 3.3 the import machinery changed to use importlib. This means the code to create the representation of a module now calls into Python code (the <frozen importlib._bootstrap>:690(_module_repr) you're seeing).
But my question is why are you not calling PyObject_HasAttr() before calling PyObject_GetAttr()? Exceptions may be relatively cheap but they are not free.
changed the title [-]_pickle.c[/-][+]Creating the string representation of a module is slower[/+]on Oct 20, 2014
kbengine commented on Oct 21, 2014
kbenginemannequin
MannequinAuthor
More actions
This is a misunderstanding, "getattribute (PyObject *obj, PyObject *name, int allow_qualname)" is the Python code, in the Python/Modules/_pickle.c (1538).
There's no doubt that whichmodule() has grown more complex since 3.2. We probably cannot eliminate the O(<number of modules>) component, but could at least make it faster.
Note the problem is easily reproduce. For example we can take numpy where some ufuncs (i.e. function-like objects implemented in C) don't have a __module__.
$ PYTHONHASHSEED=0 python3.4 -m timeit -s "import pickle, numpy; d=numpy.add""pickle.dumps(d)"
1000 loops, best of 3: 280 usec per loop
$ PYTHONHASHSEED=0 python3.4 -m timeit -s "import pickle, numpy; d=numpy.diff""pickle.dumps(d)"
100000 loops, best of 3: 2.74 usec per loop
We see that pickling numpy.add (which doesn't have a __module__) is 100x slower than numpy.diff (which has a __module__)! Note I'm forcing PYTHONHASHSEED for consistent results, since whichmodule() uses dict iteration.
For comparison, 2.7 is fast enough:
$ PYTHONHASHSEED=0 python2.7 -m timeit -s "import cPickle as pickle, numpy; d=numpy.add""pickle.dumps(d)"
100000 loops, best of 3: 6.12 usec per loop
$ PYTHONHASHSEED=0 python2.7 -m timeit -s "import cPickle as pickle, numpy; d=numpy.diff""pickle.dumps(d)"
100000 loops, best of 3: 2.35 usec per loop
(varying PYTHONHASHSEED didn't produce any slower results)
For example we can take numpy where some ufuncs (i.e. function-like objects implemented in C) don't have a __module__.
Oh. I was not aware of that. Is there a way to fix numpy to set the __module__ attribute? Maybe we should warn users that serialiaing objects without __module__ is much slower?
whichmodule_speedup.diff: I proposed once a more generic solution which is much more complex to implement. Lazy formatting of Exception message: in most cases, the message is not used. Replace AttributeError(message) with AttributeError(attr=name), only format when str(exc) is called.
The fix for this now uses module without initializing it, which could lead to a crash if get_dotted_path() tries to raise an exception (it puts module into the error string with %R). See Modules/_pickle.c#l1656 and Modules/_pickle.c#l1538.
I think the fix is to initialize module with Py_None and currently there's no need to bump the refcount (though I'm always wary of doing that in case things change in the future). If that sounds right I'm happy to put the fix in, else Antoine can do it :)
This is still causing a somewhat serious warning on Windows, see [1] for example. The condition warned about affects every platform.
It took me some time to make sense of that function, but I think I finally understand what's going on. I think Steve's suggestion of initializing module to Py_None would be fine, or just pass in a known good object like global instead since the 'obj' parameter is only used for error message formatting. Either way, I think the error check at Modules/_pickle.c:1657 [2] should either use reformat_attribute_error() or do the reformatting itself (to provide a different message) because I don't think the AttributeError message from get_dotted_path would be anywhere close to correct (especially if 'obj' is given as Py_None).
This issue may have introduce a regression, please see:
Issue bpo-22971: test_pickle: "Fatal Python error: Cannot recover from stack overflow." on FreeBSD buildbot
Activity
kbengine commentedon Oct 20, 2014
I have an application, the use of Python3.2.3 development.
When I upgrade to Python3.4.2, found a problem.
I have an extended "xxx.c (c-python)" module, I call pickle to serialize and deserialize, _pickle.c calls the whichmodule to look for this module, The final will be to find the module from sys.modules.
But probably there are 200 elements in sys.modules, Use the "obj = getattribute (module, global_name, allow_qualname)" to try to get the object:
static PyObject *
getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
...
...
...
// So I frequently call pickle.dumps CPU consumption sharply.
ncalls tottime percall cumtime percall filename:lineno(function)
315 0.001 0.000 0.004 0.000 <frozen importlib._bootstrap>:690(_module_repr)
I went wrong?
Look forward to answer, thanks!
brettcannon commentedon Oct 20, 2014
In Python 3.3 the import machinery changed to use importlib. This means the code to create the representation of a module now calls into Python code (the
<frozen importlib._bootstrap>:690(_module_repr)you're seeing).But my question is why are you not calling PyObject_HasAttr() before calling PyObject_GetAttr()? Exceptions may be relatively cheap but they are not free.
[-]_pickle.c[/-][+]Creating the string representation of a module is slower[/+]kbengine commentedon Oct 21, 2014
This is a misunderstanding, "getattribute (PyObject *obj, PyObject *name, int allow_qualname)" is the Python code, in the Python/Modules/_pickle.c (1538).
Do efficiency is decreased a lot.
birkenfeld commentedon Oct 21, 2014
HasAttr would just call GetAttr and discard the exception.
@ OP: what objects are you pickling? You can give them a __module__ attribute to save the lookup in sys.modules.
pitrou commentedon Oct 21, 2014
There's no doubt that whichmodule() has grown more complex since 3.2. We probably cannot eliminate the O(<number of modules>) component, but could at least make it faster.
[-]Creating the string representation of a module is slower[/-][+]_pickle's whichmodule() is slow[/+]pitrou commentedon Oct 21, 2014
Note the problem is easily reproduce. For example we can take numpy where some ufuncs (i.e. function-like objects implemented in C) don't have a __module__.
We see that pickling numpy.add (which doesn't have a __module__) is 100x slower than numpy.diff (which has a __module__)! Note I'm forcing PYTHONHASHSEED for consistent results, since whichmodule() uses dict iteration.
For comparison, 2.7 is fast enough:
(varying PYTHONHASHSEED didn't produce any slower results)
birkenfeld commentedon Oct 21, 2014
Attached patch addresses the repeated __repr__ calling and gives a 4x speedup here.
pitrou commentedon Oct 21, 2014
Actually, numpy.add takes a different path. Sorry.
pitrou commentedon Oct 21, 2014
So, a proper way to reproduce it is through Ellipsis (which does go through whichmodule()):
vstinner commentedon Oct 21, 2014
Oh. I was not aware of that. Is there a way to fix numpy to set the __module__ attribute? Maybe we should warn users that serialiaing objects without __module__ is much slower?
vstinner commentedon Oct 21, 2014
whichmodule_speedup.diff: I proposed once a more generic solution which is much more complex to implement. Lazy formatting of Exception message: in most cases, the message is not used. Replace AttributeError(message) with AttributeError(attr=name), only format when str(exc) is called.
2 remaining items
birkenfeld commentedon Oct 21, 2014
I didn't have numpy anyway, I tested on a function with __module__ set to None manually. But I see the same speedup for Ellipsis.
python-dev commentedon Oct 23, 2014
New changeset e5ad1f27fb54 by Antoine Pitrou in branch 'default':
Issue bpo-22676: Make the pickling of global objects which don't have a __module__ attribute less slow.
https://hg.python.org/cpython/rev/e5ad1f27fb54
pitrou commentedon Oct 23, 2014
Now applied. As Georg said, though, the definitive fix is to add a __module__ attribute to your global objects.
zooba commentedon Nov 3, 2014
The fix for this now uses module without initializing it, which could lead to a crash if get_dotted_path() tries to raise an exception (it puts module into the error string with %R). See Modules/_pickle.c#l1656 and Modules/_pickle.c#l1538.
I think the fix is to initialize module with Py_None and currently there's no need to bump the refcount (though I'm always wary of doing that in case things change in the future). If that sounds right I'm happy to put the fix in, else Antoine can do it :)
zooba commentedon Nov 3, 2014
(Looks like I was outsmarted by the tracker when trying to put line numbers in my links... is there a way to do that?)
zware commentedon Nov 25, 2014
This is still causing a somewhat serious warning on Windows, see [1] for example. The condition warned about affects every platform.
It took me some time to make sense of that function, but I think I finally understand what's going on. I think Steve's suggestion of initializing module to Py_None would be fine, or just pass in a known good object like global instead since the 'obj' parameter is only used for error message formatting. Either way, I think the error check at Modules/_pickle.c:1657 [2] should either use reformat_attribute_error() or do the reformatting itself (to provide a different message) because I don't think the AttributeError message from get_dotted_path would be anywhere close to correct (especially if 'obj' is given as Py_None).
[1] http://buildbot.python.org/all/builders/x86%20Windows7%203.x/builds/8969/steps/compile/logs/stdio
[2] Steve: the 'Comment:' label in the form is a handy link to the devguide section about how magic links work. In this case, it's filepath.ext:number
vstinner commentedon Nov 28, 2014
On Windows with Visual Studio, I got a compiler warning. In whichmodule(), get_dotted_path() is called with module whereas module is not initialiazed:
pitrou commentedon Nov 28, 2014
Here is a patch.
zooba commentedon Nov 30, 2014
That patch looks good to me (better than my fix would have been :) )
vstinner commentedon Dec 1, 2014
This issue may have introduce a regression, please see:
Issue bpo-22971: test_pickle: "Fatal Python error: Cannot recover from stack overflow." on FreeBSD buildbot
python-dev commentedon Dec 1, 2014
New changeset 3e3bec66409c by Antoine Pitrou in branch 'default':
Fix uninitialized variable after bpo-22676.
https://hg.python.org/cpython/rev/3e3bec66409c