# XML parser benchmarks

## Method

Both parsers read the same generated document from a shared `String`, built before timing starts. Each benchmark materializes the whole document and keeps a result derived from it:

- `justjavac/xml` calls `parse`, which builds the compact tree, then reads the root name (or the concatenated text, for the read workloads).
- `Milky2018/xml` 0.6.1 calls `Reader::from_string(input).read_events_until_eof()`, which builds the complete event array, then reads its length (or sums the text events).

Neither side is asked to do less work than the other: both end up with the whole document in memory. `it.keep` consumes every result.

Measurements use `moon bench --release`; each reported time is the runner's mean for one complete operation. Fixtures are generated by `fixture.mbt` and `bench_upstream.mbt` so every backend receives identical input without file I/O inside the timed region.

Environment: GitHub Actions `ubuntu-latest`, MoonBit nightly 0.1.20261204, Node.js 22 for the wasm backends. Every table below comes from one job of run [47508373815](https://github.com/justjavac/moonbit-xml/actions/runs/47508373815) on `main`, which ran all three benchmark files for all three backends, so each ratio is taken inside a single run. Each platform publishes its own `results.md` as a CI artifact.

Shared runners are noisy in a specific way: the machine changes between jobs, not within one. The same commit was measured three times — the first `ubuntu-latest` attempt of the previous run, a rerun of that attempt, and this run. The first and the third agreed to four significant digits on every fixture: 520.80 µs and 520.77 µs for the catalogue parse, 27.27 µs and 27.48 µs for `entities`, 166.51 µs and 166.36 µs for the 2 MiB text. The rerun in between measured 414.25 µs and 17.36 µs — 20% and 37% faster for this parser, while the reference parser gained only 3%, which moved the catalogue ratio from 14.8× to 18.0×. That job drew a faster machine, and the numbers below are the ones that reproduce. So: an absolute time belongs to a runner, a ratio inside one run is stable to roughly ±20%, and a single ratio is indicative rather than exact.

This run includes `aeb445d`, which stopped allocating a tuple per tag and per entity reference. Measured back to back on one machine, that change took the catalogue parse from 956 µs to 384 µs, deep nesting from 158 µs to 53 µs, the entity-heavy read from 861 µs to 300 µs, and the attribute parse from 209 µs to 153 µs.

## Who the other side is

`Milky2018/xml` is published from [`moonbit-community/xml-mbt`](https://github.com/moonbit-community/xml-mbt) — its `moon.mod` still names `Milky2018/xml`. The 0.6.1 dependency resolves to that repository's current `main` up to a `#derive` syntax migration in three files, so the comparison below already runs that project's code.

There are three sets of tables. The first uses fixtures written for this project. The second reproduces the seven cases from `xml-mbt`'s own benchmark file, so the comparison also covers the shapes its author chose to measure. The third, further down, brings in the four other XML parsers published on mooncakes.

## Fixtures

| Fixture | Shape |
| --- | --- |
| catalogue | 2000 `<item>` elements, three attributes each, two child elements with text, 260 KB |
| attributes | 1000 `<row>` elements with 20 attributes each, no text |
| deep nesting | 2000 nested `<a>` elements |
| 2 MiB text | one `<root>` holding a single 2 MiB text node |
| entities | 5000 repetitions of `&amp;&lt;&#65;&#x42;` |
| namespaces | 1000 `<p:item q:id="...">` elements under two declarations |

## Parse

| Workload | native | wasm | wasm-gc |
| --- | ---: | ---: | ---: |
| catalogue | 520.77 µs vs 7.70 ms (**14.8×**) | 969.20 µs vs 23.02 ms (**23.8×**) | 1.41 ms vs 12.30 ms (**8.72×**) |
| attributes | 240.42 µs vs 8.20 ms (**34.1×**) | 322.37 µs vs 21.82 ms (**67.7×**) | 285.39 µs vs 6.62 ms (**23.2×**) |
| deep nesting | 62.94 µs vs 556.95 µs (**8.85×**) | 174.47 µs vs 1.95 ms (**11.2×**) | 96.61 µs vs 416.72 µs (**4.31×**) |
| 2 MiB text | 166.36 µs vs 11.15 ms (**67.0×**) | 202.33 µs vs 26.82 ms (**133×**) | 1.03 ms vs 26.93 ms (**26.1×**) |
| entities | 27.48 µs vs 6.72 ms (**245×**) | 30.47 µs vs 20.48 ms (**672×**) | 81.98 µs vs 7.10 ms (**86.6×**) |
| namespaces | 69.73 µs vs 794.39 µs (**11.4×**) | 135.02 µs vs 2.70 ms (**20.0×**) | 96.02 µs vs 793.14 µs (**8.26×**) |

Each cell is `justjavac/xml` then `Milky2018/xml`, with the ratio in bold.

## Read all text

Parse plus concatenate every text node, which forces entity decoding on the justjavac side and reuses the strings the reader already built on the other.

| Workload | native | wasm | wasm-gc |
| --- | ---: | ---: | ---: |
| catalogue | 749.35 µs vs 8.26 ms (**11.0×**) | 1.34 ms vs 24.03 ms (**17.9×**) | 2.02 ms vs 12.43 ms (**6.15×**) |
| entities | 177.47 µs vs 6.81 ms (**38.4×**) | 339.55 µs vs 20.51 ms (**60.4×**) | 412.17 µs vs 7.26 ms (**17.6×**) |

## The `xml-mbt` cases

These are the seven benchmarks in `xml-mbt`'s `parser_bench_test.mbt`, rebuilt verbatim in `bench_upstream.mbt`. Only the `keep` expression differs, because the two libraries report results in different shapes.

| Case | native | wasm | wasm-gc |
| --- | ---: | ---: | ---: |
| 1024 character references | 1.94 µs vs 339.76 µs (**175×**) | 3.09 µs vs 1.14 ms (**369×**) | 4.09 µs vs 247.80 µs (**60.6×**) |
| 4096 character references | 5.95 µs vs 1.38 ms (**232×**) | 7.51 µs vs 4.57 ms (**609×**) | 15.35 µs vs 1.00 ms (**65.1×**) |
| 16384 character references | 22.38 µs vs 5.70 ms (**255×**) | 24.83 µs vs 18.44 ms (**743×**) | 66.71 µs vs 5.15 ms (**77.2×**) |
| 4096 references, read all text | 43.37 µs vs 1.42 ms (**32.7×**) | 72.68 µs vs 4.51 ms (**62.1×**) | 79.91 µs vs 1.07 ms (**13.4×**) |
| namespace depth 256, parse | 26.36 µs vs 1.01 ms (**38.3×**) | 45.96 µs vs 2.08 ms (**45.3×**) | 32.56 µs vs 1.15 ms (**35.3×**) |
| namespace depth 256, resolved | 460.17 µs vs 1.21 ms (**2.63×**) | 698.73 µs vs 2.70 ms (**3.86×**) | 673.51 µs vs 1.34 ms (**1.99×**) |
| namespace depth 1024, parse | 72.32 µs vs 1.35 ms (**18.7×**) | 145.56 µs vs 2.94 ms (**20.2×**) | 94.02 µs vs 1.46 ms (**15.5×**) |
| namespace depth 1024, resolved | 600.93 µs vs 1.74 ms (**2.90×**) | 1.07 ms vs 4.57 ms (**4.27×**) | 791.34 µs vs 1.93 ms (**2.44×**) |
| 2 MiB text | 166.62 µs vs 10.96 ms (**65.8×**) | 202.76 µs vs 26.98 ms (**133×**) | 1.03 ms vs 27.06 ms (**26.3×**) |
| 4096 children from nested entities | 1.15 µs vs 9.96 ms | 3.22 µs vs 29.99 ms | 1.65 µs vs 11.03 ms |

The namespace cases appear twice. Their `NamespaceReader` resolves every element's namespace while reading, while this parser resolves lazily, so a plain `parse` does not do that work. The `resolved` row walks every element and calls `namespace_uri()`, which is the like-for-like comparison; the `parse` row shows what deferring the work costs.

The nested-entity row has no ratio on purpose. Their reader parses entity replacement text as markup, so `&c;` produces 4096 `<child>` elements. This parser expands replacement text as characters, so the same input yields one text node holding the markup literally. The work is not the same, and the second row is a known gap rather than a win — see "Scope" in the top-level README.

## The rest of the ecosystem

`Milky2018/xml` is the parser this project was written against, but it is not the only one on mooncakes. Four more are measured here, all on the same generated `String`, all reaching a materialized document:

| Package | Version | Shape | What `keep` reads |
| --- | --- | --- | --- |
| [`marianoguerra/xml`](https://github.com/marianoguerra/syndication-mb) | 0.2.2 | minimal pull parser and writer, in the spirit of `quick-xml` | `Document::read_from`, then the root name |
| [`moonbit-community/XMLParser`](https://github.com/moonbit-community/XMLParser) | 0.2.7 | parser combinators over `Iter[Char]` | `xml_from_string`, then the root's child count |
| [`moonbitstack/moonxml`](https://github.com/moonbitstack/moonxml) | 0.2.2 | HTML, XML, and SVG over one mutable tree, with CSS selectors and XPath 1.0 | `loads`, then the root name |
| [`thy1016/moonxml`](https://github.com/thy1016/moonxml) | 0.3.2 | spec-shaped, namespace-aware model for RDF tooling | `parse`, then the root's qualified name |

Each library is called through its own public entry point, in `bench_ecosystem.mbt`. Only the `keep` expression differs, because the six libraries report their result in six shapes.

Two of the six fixtures are missing from this section, both because the other side does not survive them:

- **deep nesting** (2000 levels): `moonbit-community/XMLParser` overflows the stack; `moonbitstack/moonxml` stops at its own limit, `513 exceeds the limit of 512 at 1536`.
- **2 MiB text**: `moonbit-community/XMLParser` did not finish 1 MiB within 120 s, so 2 MiB was not attempted.

What is left is four fixtures that every library parses. The first column is this parser's absolute time, so the ratios have a base; the rest are `other ÷ ours`, computed inside a single run.

### catalogue (2000 items, 260 KB)

| Backend | justjavac/xml | marianoguerra | Milky2018 | thy1016 | moonbitstack | community/XMLParser |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| native | 522 µs | 6.5× | 14× | 24× | 141× | 142× |
| wasm | 969 µs | 12× | 24× | 35× | 934× | 250× |
| wasm-gc | 1.45 ms | **1.9×** | 8.6× | 9.2× | 2,048× | 51× |

### attributes (1000 rows × 20 attributes, no text)

| Backend | justjavac/xml | marianoguerra | Milky2018 | thy1016 | moonbitstack | community/XMLParser |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| native | 234 µs | 8.6× | 34× | 50× | 27× | 166× |
| wasm | 325 µs | 21× | 67× | 91× | 47× | 388× |
| wasm-gc | 287 µs | 4.8× | 24× | 35× | 14× | 118× |

### entities (5000 × `&amp;&lt;&#65;&#x42;`)

| Backend | justjavac/xml | marianoguerra | Milky2018 | thy1016 | moonbitstack | community/XMLParser |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| native | 26.8 µs | 67× | 240× | 264× | 4,170× | 585× |
| wasm | 30.4 µs | 173× | 674× | 712× | 53,289× | 1,762× |
| wasm-gc | 82.5 µs | 25× | 88× | 102× | 55,731× | 205× |

### namespaces (1000 `<p:item q:id="...">` under two declarations)

| Backend | justjavac/xml | marianoguerra | Milky2018 | thy1016 | moonbitstack | community/XMLParser |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| native | 70.5 µs | 5.1× | 11× | 23× | 20× | 123× |
| wasm | 137 µs | 9.5× | 19× | 38× | 30× | 212× |
| wasm-gc | 95.2 µs | **2.3×** | 7.9× | 19× | 11× | 76× |

These four tables are from the same run as everything above. Every cell is a ratio taken inside that run, which is the quantity that survives a shared runner. The largest cells are also the least precise: `moonbitstack/moonxml` at 2.9 s and 4.6 s, and `community/XMLParser` at 74 ms on native, were sampled once or twice before the per-benchmark budget ran out, so treat four-digit ratios as order of magnitude.

### What separates them

`marianoguerra/xml` is the closest on every fixture and every backend. Across its twelve cells the range is 1.9× to 173×, and seven are under 10×; the best cell any of the other four manages is 7.9×, on `wasm-gc` namespaces. It is also the closest in design — a minimal pull parser in the spirit of `quick-xml`, with no combinator layer and no HTML-oriented model to carry — so its row is the fairest comparison here: the gap is per-token work, not a different strategy.

`moonbitstack/moonxml` is not uniformly slow, and its profile is the interesting one: 14× on `attributes` and 11× on `namespaces` on `wasm-gc`, but 2,048× on `catalogue` and 55,731× on `entities`. Its cost tracks text nodes rather than markup. On native it spends 12× longer on `catalogue`, which has 4,000 text nodes and 6,000 attributes, than on `attributes`, which has no text and 20,000 attributes. Something in the text path is per node and expensive; that is where a reader of that library would look.

`moonbit-community/XMLParser` pays per character, since a combinator over `Iter[Char]` allocates at every step, and lands between the flat parsers and the tree-heavy ones: 51× on `wasm-gc` for `catalogue` against 142× on native. `thy1016/moonxml` builds the heaviest model of the five and sits between 1.1× and 2.0× the reference parser's time on native, so most of its distance from this parser is the reference's distance.

`entities` separates the field more than any other fixture, 67× to 4,170× on native, because entity handling is where the implementations disagree about *when* to work: eagerly while scanning, eagerly into strings, or not until read. This parser does the last, which is why the ratio there is two orders of magnitude rather than one.

## macOS and Linux

The two runners are not equally fast, for either parser, so absolute times are not a platform property. What differs is by how much each side gains:

| Fixture | Linux | macOS | macOS ÷ Linux |
| --- | ---: | ---: | ---: |
| catalogue parse | 521 µs | 372 µs | 0.71× |
| attributes parse | 240 µs | 204 µs | 0.85× |
| deep nesting parse | 63 µs | 52 µs | 0.82× |
| namespaces parse | 70 µs | 54 µs | 0.77× |
| read all text, catalogue | 749 µs | 565 µs | 0.75× |
| 2 MiB text parse | 166 µs | 85 µs | **0.51×** |
| entities parse | 27.5 µs | 6.8 µs | **0.25×** |

The same fixtures for the reference parser move by 0.82× to 0.90× — evenly, on all seven. This parser moves by 0.71× to 0.85× on the five allocation-bound ones and much more on the two scan-bound ones: twice as fast on the 2 MiB text, four times on `entities`. Those two are a scan for the next `<` plus a handful of allocations, and macOS runs that scan far faster per byte, which is the work this design leans on most. The reference parser allocates per event and does not get the same benefit.

Because the direction is a ratio, it survives the disagreement between hosts:

| Case | macOS | Linux | |
| --- | ---: | ---: | --- |
| 16384 character references, parse | 831× | 255× | macOS 3.3× wider |
| 2 MiB text, parse | 118× | 67.0× | macOS 1.8× wider |
| namespace depth 1024, resolved | 3.2× | 2.9× | equal |
| 4096 references, read all text | 30.3× | 32.7× | equal |

Both used to be much worse. The text paths were 25-60% slower per byte on macOS than on Linux while `parse` itself was faster, which pointed at the decoder rather than at the tree: it copied every text node out of the source before decoding it, and sliced every entity name into its own string, so a 4096-reference node made 4096 small allocations. Decoding now runs over a source range and compares names in place, which took that case from 11.6× to 23-28× at the time; removing the tuple per reference carried it to 30-33× on both hosts.

The `wasm` and `wasm-gc` backends are slower on `macos-latest` across the board, for both parsers, because the wasm runtimes differ between the runners. Only the ratios are comparable there.

## What the numbers say

The widest gaps are on input where the reference implementation's per-token work dominates: entity-heavy text, which it expands while scanning, and attribute-dense markup, where it allocates an attribute array and a name string per element. `entities` runs 245× on native and 672× on `wasm`; `attributes` runs 23× to 68×.

The narrowest is namespace resolution on `wasm-gc`, at 2.0×. Both sides keep a per-scope binding table, and `wasm-gc` makes the object churn behind the reference's scopes cheap, so the remaining difference is mostly the tree this side builds anyway. Resolving 1024 nested elements under 512 declarations was 170 ms before the scopes were made shared and indexed; it is 791 µs now.

`2 MiB text` on `wasm-gc` is the one workload where the justjavac side is far slower than on the other backends (1.03 ms against 166 µs on native). The parse is a single scan for `<`, and the core library's `StringView::find` is vectorized on native and wasm but not on `wasm-gc`, so the scan itself is what costs. It is still 26× faster than the reference.

Across the whole field the nearest competitor is `marianoguerra/xml`: 1.9× on the `wasm-gc` catalogue, 2.3× on `wasm-gc` namespaces, and never worse than 173×. No other library comes closer than 7.9× on any fixture, and the order of the six barely moves between backends — `Milky2018/xml` and `thy1016/moonxml` trade places on the `wasm-gc` catalogue at 8.6× and 9.2×, which is inside the noise.

## Reproduce

From the repository root:

```sh
moon bench -p justjavac/xml-benchmark --target native --file bench.mbt
moon bench -p justjavac/xml-benchmark --target native --file bench_upstream.mbt
moon bench -p justjavac/xml-benchmark --target native --file bench_ecosystem.mbt
```

Replace `native` with `wasm` or `wasm-gc` for the other backends. All three files have to be named separately: `--file` takes one path.
