[Python-Dev] cpython: Optimize tostringlist by taking the stream class outside the function. It's now
Georg Brandl
g.brandl at gmx.net
Sat Jul 21 16:43:02 CEST 2012
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 <eliben at gmail.com>
> 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 at python.org
> http://mail.python.org/mailman/listinfo/python-checkins
>
More information about the Python-Dev
mailing list