Documentation version dev
Documentation
On this page

Benchmarks

How the speed of mathconv is measured and guarded: the benchmark set, the instruction-count gate, CodSpeed, the published throughput and the allocation check.

You are reading the documentation of the development version on the dev branch, which is not released. The latest release is 0.1.0a4: this page in 0.1.0a4.

The benchmark set#

The benchmark measures how fast mathconv converts a fixed set of formulas. The set lives in bench/formulas/, one file per kind of formula and one formula per line, and is frozen: it changes only in a commit that says why. It is weighted like real data, where the median formula has 5 elements and one in a hundred has more than 48. Of the 2,147 formulas of presentation markup in the set, 82% have at most 8 elements and the median is 5.

FileFormulasHolds
tokens-small.txt1200real formulas of at most 8 elements without scripts, fractions or tables: single tokens, signs, units, short rows
scripts.txt700real formulas with subscripts, superscripts, accents, fractions and roots, up to 60 elements
rows.txt100real formulas of 9 to 20 elements made of rows, text, fences and spaces
tables.txt22real tables, and synthetic matrices and labelled equation systems
content.txt36synthetic content markup: rate laws, rules and function definitions shaped like those of systems-biology models, content generated from TeX, constructors, malformed applications, a product of 300 factors and applications nested 80 deep
large.txt26real formulas of more than 60 elements, and synthetic formulas of about 1,000 elements
malformed.txt87synthetic broken markup: truncated, unclosed and misordered tags, bad entities, stray text, unusual structure
pathological.txt12synthetic formulas that stress one dimension: nesting up to and beyond the depth limit, a row of 3,001 children, 1,100 entity references, a text run of 16,000 characters, 300 attributes

bench/formulas/manifest.toml lists every file and, for each line, its source: a row of the public corpus, with its document, source and licence, or a described synthetic formula. A test checks that every line has exactly one entry. ATTRIBUTION.md credits the documents the real formulas come from. Only formulas whose source licence permits redistribution are included.

cargo xtask bench

builds the benchmark driver in release mode and prints, for each file and each pair of source and target formats, the formulas converted per second on one core with a reused workspace, the best of several runs. Where Cachegrind runs, it also prints the instructions executed by one pass over each file. The whole command takes under a minute.

Three measurements use the benchmark set, each for its own purpose:

  • The benchmark gate decides whether a change may land. It counts the instructions of the formulas whose output a change leaves alone, against the commit the change is based on and against the last release, so that neither one slow commit nor many small ones go unnoticed.
  • CodSpeed keeps the history of every commit of dev and main and reports the change of each benchmark on a pull request, with flame graphs. Its numbers come from a CPU simulation, so they show changes, not the speed of a real machine.
  • The throughput job measures wall-clock speed on a GitHub-hosted runner, through the Rust core, the Python package and the npm package. Its figures are the ones the project publishes, in the README and in the release notes.

Times measured on a laptop vary with its load by tens of percent from one run to the next; they serve to explore a change locally and are never stated as the speed of mathconv.

The benchmark gate#

Time measurements on shared machines vary by several percent from run to run, so the gate counts executed processor instructions instead, using Valgrind's Cachegrind, which runs on Linux.

cargo xtask gate

builds the driver from the current tree, from the commit the change is based on and from the last release, runs them on the current tree's formulas, and compares the instructions of one pass of the current tree with each of the other two, for each file and pair. The base is the merge base with origin/dev; for a tree that is already on dev or main it is the previous commit; --base <revision> names another. The last release is the newest tag v* that the tree's commit reaches, whose tree the gate extracts and builds like the base.

A change that lets the engine convert something it skipped before runs more instructions without being slower at the same work, so the gate compares the results of the two trees before it applies a limit. For every formula, the driver computes a digest of the output, the display flag, where the output came from and the fallbacks, in both trees. A formula with the same digest in both trees does the same work; a formula whose digest differs has changed behaviour.

  • The formulas with unchanged results are held to the limits. Against the base commit, the gate fails when they execute more than 1% more instructions in total, or more than 3% more in any one file and pair. Against the last release, where the unchanged formulas are those whose result is the same as in that release, the budget is 3% in total and 5% in any one file and pair: steps below the limits of single commits add up, and the second comparison stops them from adding up to a slower release. In a file where some formulas changed, the unchanged ones are measured as a file of their own, so a slowdown in unchanged conversions still fails.
  • The files with changed results are printed in a second group, with their change and the number of formulas whose result changed, and are not subject to the limits. A large cost there is visible in the report, and a change that touches few formulas but costs a lot shows as a small count next to a large increase.

The report has one part for each comparison, headed by the commit it compares with, each showing both groups, and the gate names the comparison, file, pair and increase for every limit that fails. A cost that a change must have to be correct counts against the release budget too: a change that would exceed it lands after one that makes the engine faster elsewhere.

A pass is measured as the difference between a run that converts the file twice and one that converts it once, which leaves out the start of the program, the reading of the file and the growth of the workspace. The count is the same on every run of the same code, so the limits carry no allowance for noise.

Continuous integration runs the gate on every pull request, against the dev commit it is based on, and on every push to dev or main, against the commit that the branch pointed to before the push, so that the commits of one push are measured as one change. On systems other than Linux the command compares wall-clock times instead, with the same two groups, labels the comparison as such, and only warns when the total grows by more than 5% or a file and pair by more than 25%; the authoritative gate is the one in continuous integration.

Continuous benchmarking#

The benchmark gate compares a change with its base commit and keeps no history. CodSpeed keeps one result per commit of dev and main, compares each pull request with the latest result on the branch it targets, and links every benchmark to a flame graph. The codspeed workflow runs on every pull request and on every push to dev or main, in two jobs.

The rust job runs bench/benches/convert.rs, a divan benchmark through CodSpeed's compatible crate:

BenchmarkMeasures
convert_filethe whole file converted with a workspace that earlier conversions have grown, one benchmark for each file of the benchmark set and each pair: convert_file::scripts/mathml-latex
one_tokenthe fixed cost of one call, on a formula of one element
typical_formulaone call on a formula of five elements, the median of the set
typical_formula_fresh_workspacethe same call with a new workspace, which grows its buffers

The files come from the loader that the driver uses, so a file added to bench/formulas/ and listed in its manifest is benchmarked without any other change. The parse and lower stages are not public, so they are not measured on their own; the files that differ in structure locate a regression to a kind of input instead.

The python job installs the built wheel and runs bench/python/ with pytest-codspeed: convert on a formula of one element and on a typical one, and Converter.convert_batch and result_batch on the first 10 and the first 400 formulas of scripts.txt, each for both targets. These measure what a Python user calls, including the call and the creation of the returned strings and Result objects, so a change to the binding shows here and not in the Rust benchmarks.

CodSpeed runs each benchmark once in its CPU simulation, which counts the instructions of the whole process, native code included, and the cache misses they cause, and converts them into a time. Like the gate's counts, the numbers are the same on every run of the same code, and they leave out system calls. The simulation needs Python 3.12 or later. A report on a pull request lists the benchmarks whose value changed, with the change in percent; the benchmark's page shows the flame graph of the base and of the pull request. A change in one convert_file benchmark shows which kind of input got slower, a change in one_token a cost added to every call, and a change only in the Python benchmarks a cost in the binding.

The gate still decides whether a change may land: it compares only the formulas whose output is unchanged, and the report does not. Each job first builds and runs its benchmarks without CodSpeed, so a benchmark that fails stops the workflow. The upload step may fail without failing the job, because it depends on CodSpeed's service and its GitHub App.

To run the same benchmarks locally, as ordinary timings:

cargo bench --package mathconv-bench
pytest bench/python --codspeed

The second command runs in an environment with the built wheel and pytest-codspeed; on a machine without CodSpeed's simulation it measures wall-clock time.

Times from the simulation are estimates, not measurements of a machine. The simulation runs each benchmark once, so the single-call benchmarks include the cost of cold caches and report several microseconds where cargo bench reports a fraction of one; compare them with their own earlier values, not with the timings of cargo bench. Wall-clock numbers on dedicated hardware need CodSpeed's macro runners, which only repositories of organisations can use. In an organisation the rust job is copied with runs-on: codspeed-macro-x64-ryzen-9950x-ubuntu-24-04, cargo codspeed build --measurement-mode walltime --package mathconv-bench and mode: walltime.

Throughput#

The figures that the project publishes come from the throughput job of the wheels workflow, which runs on a GitHub-hosted ubuntu-latest runner on every pull request and every push. It runs

cargo xtask throughput --runs 5 --python <interpreter> --node <package> --runner <label>

which builds the benchmark driver in release mode and, for each file of the benchmark set and each pair, measures five ways of converting: the Rust core, as cargo xtask bench does; mathconv.convert of the installed Linux wheel, one formula per call; Converter.convert_batch, the whole file in one call; and convert and Converter.convertBatch of the npm package under Node.js. Each run of a measurement is the best of nine timed passes of at least 10 ms, and there are five runs. For each file and pair, and for the whole set, the result gives the formulas converted per second on one core: the median of the runs, the slowest run and the fastest. The rate of the whole set divides all its formulas by the total time of each run. The job writes the result to throughput.json, uploads it as the artifact throughput, and shows it as a table in the job summary. The file also names the commit, the day and the runner with its processor.

The figures are wall-clock times on shared virtual machines, so they vary from run to run by a few percent and from runner to runner by more; the spread of the runs shows how much. They describe the speed a user can expect on a similar machine. To tell whether a change is faster, compare instruction counts with the gate or CodSpeed instead.

Without --python, the command measures the Rust core only. Run on a laptop, it is a quick look at the speed of a change and nothing more.

A release states the speed of the commit it releases. cargo xtask release downloads the artifact of that commit's throughput job and commits it as bench/throughput.json; the GitHub Release carries the same file, and the release notes end with a paragraph of its figures.

The README states two figures for typical formulas, those of tokens-small.txt, scripts.txt and rows.txt: the real formulas of up to 60 elements, of which real documents are almost entirely made. The figures are MathML to LaTeX, through Converter.convert_batch, which is how bulk conversion runs, and through the Rust core; each is all the typical formulas divided by the time that the median rate of each file gives them. The release notes add the rates over the whole set, whose few large and stress formulas take most of its time. The README sentence sits between the marker comments <!-- speed:start --> and <!-- speed:end --> and is never edited by hand: cargo xtask throughput --readme writes it from the committed file, and cargo xtask check fails when the two differ.

The allocation check#

The design rule that no memory is allocated per node or per character is verified by a test in bench/tests/allocation.rs. It installs a counting allocator, converts the whole benchmark set once to warm the workspace up, converts it again, and counts the allocations of the second pass for each pair. The count is zero. The test records the count it expects and fails when the engine allocates a different number of times, so a change that adds an allocation to the conversion path fails cargo xtask check.

Edit this page on GitHub