Skip to content

encoding/xml: very low performance in xml parser #21823

Open

Description

@243083df

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.

Activity

  1. ianlancetaylor commented on Sep 9, 2017

    @ianlancetaylor
    Contributor

    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.

  2. changed the title [-]Very low performance in xml parser[/-] [+]encoding/xml: very low performance in xml parser[/+] on Sep 9, 2017
  3. added this to the Unplanned milestone on Sep 9, 2017
  4. added
    NeedsInvestigation
    on Sep 9, 2017
  5. 243083df commented on Sep 9, 2017

    @243083df
    Author

    File is very big, around 10-50 millions records.
    The xml file looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LIST>
    <ELEMENT ATTRIBUTE1="" ATTRIBUTE2="" /><ELEMENT ATTRIBUTE1="" ATTRIBUTE2="" />
    </LIST>
  6. a-h commented on Sep 10, 2017

    @a-h
    Contributor

    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.

    screen shot 2017-09-10 at 22 55 33

  7. 243083df commented on Sep 11, 2017

    @243083df
    Author

    I think, @a-h's benchmark enough to investigate. I used same code.

  8. gopherbot commented on Sep 12, 2017

    @gopherbot
    Contributor

    Change https://golang.org/cl/63390 mentions this issue: unicode: speed-up is16/is32

  9. added a commit that references this issue on Sep 12, 2017
  10. 243083df commented on Sep 13, 2017

    @243083df
    Author

    By the way. Is call len(p) in https://github.com/golang/go/blob/master/src/bufio/bufio.go#L196 necessarily?

  11. mattn commented on Sep 13, 2017

    @mattn
    Member

    xml.NewDecoder already create bufio.Reader.

  12. 243083df commented on Sep 14, 2017

    @243083df
    Author

    @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.

  13. robfordww commented on Nov 4, 2017

    @robfordww

    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++

  14. saleem-mirza commented on Dec 2, 2017

    @saleem-mirza

    I am experiencing same issue. Parsing 1 GB XML takes minutes which C# version completes in few seconds ( < 10)

  15. 13 remaining items

  16. saleem-mirza commented on Apr 27, 2019

    @saleem-mirza

    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

  17. webern commented on Apr 27, 2019

    @webern

    @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)

    1. It is very slow.
    2. It is non-validating (we need xsd validation)
    3. There is no support for loading XML when the schema is not known. i.e. we can load any json and we get a map[string]interface. But we cannot do this with xml.

    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.

  18. tamerh commented on Jun 11, 2019

    @tamerh

    Hi all and @243083df, @l-we, @webern, @ajruckman, @qwantix, @evt

    Regarding this issue you can try my library

    https://github.com/tamerh/xml-stream-parser

    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.

  19. gopherbot commented on Feb 8, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/218658 mentions this issue: encoding/xml: decoder to use buffered start attr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationPerformance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions