Repository navigation
Open
Labels
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performance
Milestone
Description
What version of Go are you using (go version)?
1.9
Does this issue reproduce with the latest release?
True
What operating system and processor architecture are you using (go env)?
Windows
What did you do?
I trying to parse large files with SAX with go, and get decadent performance.
I rewrite code in C#, and get maximum performance.
file, err := os.Open(filename)
handle(err)
defer file.Close()
buffer := bufio.NewReaderSize(file, 1024*1024*256) // 33554432
decoder := xml.NewDecoder(buffer)
for {
t, _ := decoder.Token()
if t == nil {
break
}
switch se := t.(type) {
case xml.StartElement:
if se.Name.Local == "House" {
house := House{}
err := decoder.DecodeElement(&house, &se)
handle(err)
}
}
}using (XmlReader reader = XmlReader.Create(filename)
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name == "House")
{
//Code
}
break;
}
}
}What did you expect to see?
Mature and fast xml parser in golang.
What did you see instead?
The bottleneck in SAX xml parsing with go is CPU, instead of low HDD io performance.
Reacted by Mura Li, ASPecherkin, Will Smith, Jade Montagu-Navarrete, SebScoFr, ajbeach2, A.J. Ruckman, Tamer Gür, Marcel Hernandez, Quim Muntal and 29 more
Reacted by Matthew James Briggs, Rickie Du, Iguchi Tomokatsu, Kordian Bruck and BenderVano
Metadata
Metadata
Assignees
Labels
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performance
Activity
ianlancetaylor commentedon Sep 9, 2017
Can you show us a complete, standalone example program? And provide your input file, or at least tell us about it?
What are the actual performance numbers?
Thanks.
[-]Very low performance in xml parser[/-][+]encoding/xml: very low performance in xml parser[/+]243083df commentedon Sep 9, 2017
File is very big, around 10-50 millions records.
The xml file looks like this:
a-h commentedon Sep 10, 2017
I was curious, since I use .NET and Go regularly, so I turned this into a reproduction at https://github.com/a-h/sax
On my MacOS machine, I found that the Go version was much slower (around 20 seconds) than the .NET Core 2.0 one (around 3 seconds) for a file with 10 million elements in it laid out as per the example above.
I put the timing output in the README.md. I noted that the CPU was at 100% during Go execution.
I added an SVG of the Go CPU profile output to the repo.
243083df commentedon Sep 11, 2017
I think, @a-h's benchmark enough to investigate. I used same code.
gopherbot commentedon Sep 12, 2017
Change https://golang.org/cl/63390 mentions this issue:
unicode: speed-up is16/is32unicode: speed-up is16/is32
243083df commentedon Sep 13, 2017
By the way. Is call len(p) in https://github.com/golang/go/blob/master/src/bufio/bufio.go#L196 necessarily?
mattn commentedon Sep 13, 2017
xml.NewDecoder already create bufio.Reader.
243083df commentedon Sep 14, 2017
@mattn Yes, but its create bufio.Reader without minimum read value.
bufio.Reader with minimum read value around 64Mb, speed up parsing 2Gb xml from 2m40s to 2m5s(around 20%, but its not stable) on HDD.
robfordww commentedon Nov 4, 2017
A lot of speed could be gained if the parser was referring to the xml byte slice instead of copying everything. I am working on such an parser for go, based on the rapidxml library in c++
saleem-mirza commentedon Dec 2, 2017
I am experiencing same issue. Parsing 1 GB XML takes minutes which C# version completes in few seconds ( < 10)
13 remaining items
saleem-mirza commentedon Apr 27, 2019
Actually, programmer who coded this library is long gone so we are clueless.
Disclaimer: this is not official excuse, I made it up since community is mysteriously silent on this issue
webern commentedon Apr 27, 2019
@saleem-mirza this begs the question, what are the plans for Golang to continue supporting XML natively as part of the standard library?
Currently the XML implementation suffers from several issues that make it substandard (when compared with Xerces, Java or libxml2, for example)
I would love to work on something like this, but it would definitely be a full-time job, i.e. someone would have to be paid to build this.
tamerh commentedon Jun 11, 2019
For people who are interested in streaming parsing i have refactored the library
to improve code quality, error handling and now it is more than 30% faster compare to previous version.Let me know if you have suggestions/feedbacks.
gopherbot commentedon Feb 8, 2020
Change https://golang.org/cl/218658 mentions this issue:
encoding/xml: decoder to use buffered start attr