Show HN: Gosax – A high-performance SAX XML parser for Go (github.com)
I've just released gosax, a new Go library for high-performance SAX (Simple API for XML) parsing. It's designed for efficient, memory-conscious XML processing, drawing inspiration from quick-xml and pkg/json.
https://github.com/orisano/gosax
Key features:
- Read-only SAX parsing - Highly efficient parsing using techniques inspired by quick-xml and pkg/json - SWAR (SIMD Within A Register) optimizations for fast text processing
gosax is particularly useful for processing large XML files or streams without loading the entire document into memory. It's well-suited for data feeds, large configuration files, or any scenario where XML parsing speed is crucial. I'd appreciate any feedback, especially from those working with large-scale XML processing in Go. What are your current pain points with XML parsing? How could gosax potentially help your projects?
34 comments
[ 2.3 ms ] story [ 82.1 ms ] threadhttps://github.com/artpar/gosax/
its a High performance golang implementation of Symbolic Aggregate approXimation
And the world runs on Excel files.
It's like choosing to use `.then()` instead of `await`. I seriously don't understand why it is so popular in the XML world when pull based parsing is much easier to use and surely just as efficient? Just brain damaged Java design patterns maybe?
Yeah if you're unfamiliar, SAX is like this (pseudocode):
Whereas pull parsers are like this: They are much easier to use because you can trivially write a recursive descent parser: Whereas with SAX you're going to end up with some monstrous hand-coded state machine like So painful. Honestly it's so obviously the right way to do tokenisation and parsing that I have yet to see another language that even has names for them. They all just use pull parsers. Nobody else does callback-based parsing like SAX because it's obviously ridiculous.Unhelpfully my only pain point with XML parsing is colleagues refusing to use XML in favour of json or, in really grim moments, yaml.
So I'm delighted to see a sensible modern web language implementation of the one true data exchange format. Thank you for sharing it.
I'm in the same boat, but I found XML has some nice properties that I sometimes miss in JSON, given that XML is used well ("correctly"), such as the differentiation of metadata (attributes) and data (nodes), namespaces, standard query languages, XSLT etc. (You can use XSLT on the web even.)
Think of all the custom, ad-hoc code that turns JSON into HTML vs having a declarative standardized way of doing so.
https://developer.mozilla.org/en-US/docs/Web/XSLT
For example, if I wanted an AI to help me highlight to the user where in a body of text I mentioned AI, I might have it return something like:
<text>Great writeup. To add an example, I personally use JSON for most of my work, but have found myself using XML for certain <ai-mention>AI</ai-mention> use cases that require annotating an original text with segments.</text>
I like being able to read and edit the data files easily in a text editor (bias against the binary formats) and for there to be a decent chance tools written by other people will interact predictably with the format (so it can't be bespoke).
I'd say the main feature is that an XML document with a schema tells you a lot about the various shapes of the file that you might need to worry about. It's essentially an extensible type system for tree shaped data.
XML has an annoying collection of spurious limitations but that's what you get with lowest common denominator / popular-cos-old systems.
XML is just terrible though. Unless you have a proper schema everything is entirely untyped (tbf the schema support is pretty good). But more to the point it just doesn't map to normal programming language objects cleanly. It's a document markup language, not an object encoding.
That means there's an annoying mismatch when parsing for 99% of use cases.
Couple that with the crazy verbosity and the weird confusing features like namespaces... I think I would rather use YAML to be honest, even though it is really bad.
Since YAML is a superset of JSON I sometimes actually use JSON with `#` comments, and read it as YAML. Only downside is nothing checks if you are using that format correctly.
I have seen real life examples of some developers putting money as a decimal in JSON, then other developers using some default parsing into float and finally truncate that float on conversion to Decimal money to loose 1 cent.
I have also seen examples of developers putting int64 random IDs in JSON interacting with Azure Cosmos, then discover that when fetching data back the numbers had been silently rounded to 53 bits! (i.e., float64 precision)
JSON does not document at all how its numbers should behave, making it useless for a lot of things.
Honestly prefer XML, being honest about not having typing.
Don't get me started on YAML and types..
But excellent. Thanks!
Great to see this sort of thing!
However, looking at the output data structures, yes, it would have the same problem if the obvious modification to re-emit XML was made.
It's actually very common, to the point I'm surprised when I encounter an XML parser that handles the problem you are referring to correctly, in any language. I've had to hack it in to every XML parser I've ever used when I care about preserving namespace abbreviations.
The XML required a good 4GB of RAM to load the model. So… I just read the stream to get to the token I needed and read until the end token.
Obviously, it was faster and required much less memory. The take-away is if you don’t need to parse the model, don’t.
I assume that nowadays, they’re using more sensible format.