Re: [Python-Dev] cpython: Optimize tostringlist by taking the stream class outside the function. It's now
Uh, optimizations are not exactly what I want to see during feature freeze. Georg On 07/17/2012 02:10 PM, eli.bendersky wrote:
http://hg.python.org/cpython/rev/51978f89e5ed changeset: 78156:51978f89e5ed user: Eli Bendersky
date: Tue Jul 17 15:09:12 2012 +0300 summary: Optimize tostringlist by taking the stream class outside the function. It's now 2x faster on short calls. Related to #1767933 files: Lib/xml/etree/ElementTree.py | 38 +++++++++++++---------- 1 files changed, 22 insertions(+), 16 deletions(-)
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -1184,23 +1184,29 @@ # @defreturn sequence # @since 1.3
+class _ListDataStream(io.BufferedIOBase): + """ An auxiliary stream accumulating into a list reference + """ + def __init__(self, lst): + self.lst = lst + + def writable(self): + return True + + def seekable(self): + return True + + def write(self, b): + self.lst.append(b) + + def tell(self): + return len(self.lst) + def tostringlist(element, encoding=None, method=None): - data = [] - class DataStream(io.BufferedIOBase): - def writable(self): - return True - - def seekable(self): - return True - - def write(self, b): - data.append(b) - - def tell(self): - return len(data) - - ElementTree(element).write(DataStream(), encoding, method=method) - return data + lst = [] + stream = _ListDataStream(lst) + ElementTree(element).write(stream, encoding, method=method) + return lst
## # Writes an element tree or element structure to sys.stdout. This
_______________________________________________ Python-checkins mailing list Python-checkins@python.org http://mail.python.org/mailman/listinfo/python-checkins
On Sat, Jul 21, 2012 at 5:43 PM, Georg Brandl
Uh, optimizations are not exactly what I want to see during feature freeze.
This is not a new optimization. It fixes a performance regression introduced by an earlier bugfix changeset (for http://bugs.python.org/issue1767933). Eli
participants (2)
-
Eli Bendersky
-
Georg Brandl