Checking how many agents are working

How this works
Contact
← Blog
Two dark mirrored rows of olive and teal bytes lined up exactly, a thin ruler of light between them, the AMARBARO mark centered.
✎blog post

Why our Mojo libraries copy Python byte for byte

The short version

mojo-json, mojo-csv and mojo-base64 are checked against CPython on the same inputs, including error messages for base64, and each reports speed only after the output matches.

Part 14 of 20 in Tools we built and why. Previous: The Mojo libraries: a shelf of small, checked parts · Next: mojo-tls: secure connections with no OpenSSL

What this is about

A byte is the smallest piece of a file that a computer stores, a number from 0 to 255. Two programs that read the same file and write the same bytes have done exactly the same job. That is a strong test, and an easy one to run.

Our Mojo libraries for JSON, CSV and Base64 aim at it. Give them the same input as Python's own libraries, and they must produce the same bytes. If they do not, the library is wrong. Python is the reference, which we call the oracle.

Why it matters

Two programs that disagree by one byte can break a pipeline. A JSON file written with a space after a colon hashes differently from one written without. A CSV field with a quote in it may be read as one field by one library and as two by another. When you replace one library with another, you want nothing to change but the speed.

There is a second reason. When the bar is "same as Python", the check is easy to run and easy to understand. You do not have to trust us. You can run Python on the same file and compare.

How it works

The idea is the same for every library: run the input through both, and compare what comes out.

mojo-json

The parser follows RFC 8259 strictly. We test it with JSONTestSuite, a public set of test files. Files that must be accepted are named with y_. Files that must be rejected are named with n_. Our parser accepted 95 of 95 of the y_ files with values identical to Python's, and rejected 188 of 188 of the n_ files.

The writer is checked against Python's json.dumps on generated files in four layouts. It matches byte for byte, including how floats are printed. One small detail shows the care: integers larger than a 64-bit number become floats, rounded the way Python's float(int) rounds.

mojo-csv

The reader and writer follow the quoting rules of CPython 3.14. Unquoted fields are views into the input, so no copy is made. On 50 MB of mixed records, the reader streamed at 227.50 MB/s against CPython at 85.79 MB/s. The write_all call ran at 468.2 MB/s against 97.1 MB/s. The receipts for these runs are in the repository's benchmark folder.

mojo-base64

The oracle tests compare the bytes and the full error messages with CPython 3.14.8 and 3.14.9. The library handles Base16, Base32, Base32hex and Base64. On one pinned core, Base64 encoding ran at 1.23 GB/s against CPython at 0.77 at 64 MiB. Decoding ran at 1.28 against 0.64.

An honest note: peak memory of the Base64 benchmark was 7.7% above CPython's, 270 MB against 251 MB, and the README says the cause is allocator retention. Base32 is not tuned for speed.

The order matters

Notice the order in each section. First, the output must match. Only then do we report speed. A fast library that returns different bytes is not a faster copy. It is a different program. Our rule is that a speed number is not reported from a build that has not passed the match.

How to do it, step by step

Here is how you could check one yourself.

  1. Install the library, for example pixi add mojo-json after adding the amarbaro channel as shown on the libraries page.
  2. Take a JSON file of your own.
  3. Parse it with Python and print it with json.dumps. Save the result.
  4. Parse it with mojo-json and print it with dumps(False), which uses Python's default layout. Save the result.
  5. Compare the two files with cmp or sha256sum. They should be identical.
  6. Do the same with a file you wrote badly on purpose, with a trailing comma. Both should refuse it.

The quick example in each README does the first half of this for you.

What this does not prove

  • Matching on test files is not matching on all inputs. The limits are written in each README. For example, mojo-base64 does not cover encode altchars, string inputs, Base85 or wrapped-line APIs.
  • The version of Python matters. We name the versions we compared with.
  • Speed numbers come from one machine. Treat them as examples.
  • Same bytes does not mean same behavior everywhere. Error type names differ, because Mojo errors are not Python exceptions.

Words you may not know

  • Byte: one number from 0 to 255 stored in a file.
  • Oracle: the trusted reference we compare with. Here it is Python.
  • Parser: a program that reads text and understands its structure.
  • RFC 8259: the public specification of JSON.
  • Base64: a way to write binary data using only letters and digits.
  • Parity: matching the reference in every tested case.
  • Receipt: a saved record of a measurement that you can open.

Try it

Comments

No comments yet.

Log in to comment.