Skip to content

Commit e53c051

Browse files
committed
SL-18330: Use xml.etree.ElementTree.fromstring() when we can.
Apparently, wrapping an incoming bytes object in io.BytesIO and passing it to xml.etree.ElementTree.parse() is slower than passing the bytes object directly to fromstring(). Detect the BytesIO case and call fromstring() with contents.
1 parent b83295a commit e53c051

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

llsd/serde_xml.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import io
23
import re
34
import types
45

@@ -264,8 +265,15 @@ def parse_xml_nohdr(baseparser):
264265
# before XML declaration. Since we explicitly test support for that case,
265266
# skip initial whitespace.
266267
baseparser.matchseq(b'')
268+
stream = baseparser.remainder()
267269
try:
268-
element = _parse(baseparser.remainder()).getroot()
270+
if isinstance(stream, io.BytesIO):
271+
# Empirically, fromstring() seems faster than _parse(). If passed
272+
# a BytesIO, extract its contents and skip to BytesIO read pos.
273+
element = fromstring(stream.getvalue()[stream.tell():])
274+
else:
275+
# Not a BytesIO, parse the stream
276+
element = _parse(stream).getroot()
269277
except ElementTreeError as err:
270278
raise LLSDParseError(*err.args)
271279

0 commit comments

Comments
 (0)