8 ms·
Faster XML Stream Processing in Go (2019)
- kristianp 3y agoI love this type of optimisation story. Interesting to note that his "in theory" best performance still calls to lxml. A true in theory best performance would surely be in the multiple Gigabyte per second range (if simdjson's performance is taken as a goal), and not 0.56 seconds to process 230 MiB which is only about half a GBps. Half a gigabyte per second is probably more than sufficient for most uses, so I'm splitting hairs somewhat. Here is the issue for slow sax parsing in encoding/xml. No work on it since around 2020: https://github.com/golang/go/issues/21823 https://github.com/golang/go/issues/21823
- masklinn 3y ago> A true in theory best performance would surely be in the multiple Gigabyte per second range (if simdjson's performance is taken as a goal) I don't think it makes any sense for the parser for a completely different and much simpler format to be "taken as a goal".
- kristianp 3y agoPerhaps "goal" is the wrong word, I just mean the "in theory best performance". It doesn't matter that json is a different format, they both have the same worst case complexity for the particular example problem.
- mananaysiempre 3y agoFull XML is hard if you’re going to go nuts with entity declarations and such (I’ve heard SVG output from Adobe tools is fond of those for some reason?), but the part that people commonly use is not really that much more complicated than JSON; perhaps even simpler in some places, given the absence of backslash escapes (that the simdjson paper spends quite a lot of time on). So for the fast path I think it’d be reasonable to expect JSON-like performance.
- notamy 3y ago(2019)
- cxr 3y agoGreat post. An issue I have with it is the way it frames the speed of a language, e.g., Python, C, etc., especially here: > I implemented the same program using pure C with libxml, which has a SAX API. [...] It takes just 0.56 seconds to process our 230 MiB input file, which is very impressive given the other results, but also not very surprising. This is C, after all. What the author is actually comparing here of course is the quality of the GCC (or LLVM or whatever) code generator, not "C" versus e.g. Go (the programming language and/or its standard libraries). There are enough misapprehensions about the source of good/bad performance of Runtimes, Not Languages. Quips like this (that it's "C, after all") don't help that discussion.
- JonChesterfield 3y agoThe C runtime doesn't really show up on a profiler though. It's crawl the init_array then jump to main. Small enough that a lot of people don't notice the C runtime exists at all. Compiling C to efficient machine code is a much easier problem than compiling Go to similar machine code since all the heavy lifting is done by the programmer.
- YZF 3y agoC doesn't really have a "run time". The startup code is optional and I wouldn't really call it a "run time" in the sense that Go has a runtime for managing goroutines and GC. It's not that easy to write Go code with no dynamic memory allocations and no GC pressure. I think it's fair to say "C is faster than Go". It's not purely a function of the language but a combination of factors including the quality of the compiler/optimizer, available libraries, and language features (and their performance cost).
- yxhuvud 3y agoYes it typically does have a runtime. For example, it has persistent structures in memory related to malloc/free, and the manipulation of those most definitely show up when you profile.
- 3y ago
- deleted 3y ago[deleted]
- llimllib 3y agoI reran the results on my m1 mac pro since this post is 6 years old: python etree: 3.25s python lxml: 2.01s go stdlib: 3.35s go gosax: 1.45s C: 0.38s So, python etree gives the same result 6 years on while lxml has gotten faster. Go's standard library does about as well as python's stdlib, and has gotten faster on the cgo binding. Procedure: - clone https://github.com/eliben/xmlgen - run `xmlgen -f 2 > out.xml` to generate a test xml file - clone https://github.com/eliben/code-for-blog/ and switch to the 2019/xml-stream dir - mv out.xml from above to the current directory - `time python etree-count.py out.xml` - `pip install lxml && time python lxml-count.py out.xml` - `go build go-stdlib-count.go && time ./go-stdlib-count out.xml` - `cd c-libxmlsax-count && make && time ./c-libxmlsax-count ../out.xml` - `cd gosax-count && go build gosax-count-simple.go && time ./gosax-count-simple ../out.xml`
- deleted 3y ago[deleted]
- neonsunset 3y agoC# takes 0.55s on M1 Pro with relatively ancient and not most optimized System.Xml.XmlReader: ./xmlgen -f 2 > dotnet/out.xml cd dotnet && dotnet publish -o . time ./XMLParse out.xml https://gist.github.com/neon-sunset/6ba67f23e58afdb80f6be868953a5ee4 https://gist.github.com/neon-sunset/6ba67f23e58afdb80f6be868...
- neonsunset 3y agoDownvoting this won't improve Go's performance :)
- eadmund 3y agoI wrote this quick little program on my laptop: (ql:quickload "cxml") (defclass finder (sax:default-handler) ((in-location :initform nil) (count :initform 0))) (defmethod sax:start-element ((finder finder) namespace-uri local-name qname attributes) (setf (slot-value finder 'in-location) (string= local-name "location"))) (defmethod sax:characters ((finder finder) data) (when (and (slot-value finder 'in-location) (search "Africa" data)) (incf (slot-value finder 'count)))) (let ((finder (make-instance 'finder))) (cxml:parse #P"out.xml" finder) (slot-value finder 'count)) and got: Evaluation took: 5.316 seconds of real time 5.339639 seconds of total run time (5.330888 user, 0.008751 system) [ Real times consist of 0.032 seconds GC time, and 5.284 seconds non-GC time. ] [ Run times consist of 0.049 seconds GC time, and 5.291 seconds non-GC time. ] 100.45% CPU 8,547,510,059 processor cycles 1,082,877,232 bytes consed For comparison: python etree: 3.04s python lxml: 3.65 go stdlib: 5.18 go gosax: 2.37 C: 0.54 Really disappointing that the Common Lisp version performs worse and is less readable than the Python lxml version. This one is more readable, but even slower: (klacks:with-open-source (s (cxml:make-source #P"out.xml")) (loop for key = (klacks:peek s) with count = 0 while key when (and (eq key :start-element) (string= (klacks:current-lname s) "location")) do (progn (setq key (klacks:peek-next s)) (when (and (eq key :characters) (search "Africa" (klacks:current-characters s))) (incf count))) do (klacks:consume s) finally (return count))) It gets: Evaluation took: 9.996 seconds of real time 10.054716 seconds of total run time (9.979712 user, 0.075004 system) [ Real times consist of 0.092 seconds GC time, and 9.904 seconds non-GC time. ] [ Run times consist of 0.101 seconds GC time, and 9.954 seconds non-GC time. ] 100.59% CPU 16,077,773,140 processor cycles 2,338,978,688 bytes consed
- userbinator 3y agoThe task is to find how many times "Africa" appears in the data of the <location> tag throughout the document. I wonder how fast grep/strstr compares, since they can run at "faster than memcpy" speeds, which means dozens of GB/s on any relatively recent machine. I know it's not the "pedantically correct" way to to do it, but in practice it works well. Only mode-escapes like CDATA can break the fact that XML tags otherwise cannot occur as substrings, so a substring match can work otherwise. This is especially true for JSON which doesn't have such escapes. Of course not using XML may gain more than an order of magnitude in speed, so if you have the chance to control both ends and get rid of XML in favour of a simpler binary protocol, I strongly recommend advocating for it.
- jiggawatts 3y agoPlease never do this. I beg you. This is why we can't have nice things, because someone somewhere always wants to cut a corner in a way that isn't as robust as they think it is. I once had an argument with a junior developer that he can't just use a hash code directly instead of an equality check. It can be used as a first step to quickly reject unequal values, but equality must be verified. He disagreed right up until a finance report had "unexpected" dollar figures in it. There is nothing I value more in the code I write than 100.0% pedantic correctness. The opposite of this is inserting Thread.sleep(1000) into multi-threaded code to "fix" it. It's a tiny, slippery step from skipping a correct parsing step to that eldritch madness.
- acimim_ha 3y agoCorrectness and robustness are not always the requirement. Sometimes it's simplicity. Sometimes it's time-to-market. Sometimes you get fed a broken XML and regex actually does a better job. I've also been a junior developer and thought that my code has to be correct, look correct and perform correctly. Then I got a boss. Boy I was wrong.
- jiggawatts 3y ago> Sometimes you get fed a broken XML How do you think that XML was generated? By people who took similar shortcuts, that's how. Layering badness on top of badness results in a tyre fire.
- deleted 3y ago[deleted]