Tag: Small-Python

Why Small Python

CPython is the official one; PyPy is a JIT. Jython is old. GraalPy needs the JVM. MicroPython is for chips. Pydantic Monty is… okay, the closest to what I need, but its subtle differences from CPython here and there are such a pain in the ass.

I want something super lean with the least surprises. Only the core Python language features and builtin functions are needed. Whatever CPython produces, Small Python produces too. For other capabilities, you register functions in your host language. It should also be metered, so that when the runtime runs LLM-generated code, it can abort, pause, or resume.

[…297 words]

Manyan: A Faster TLA+ Model Checker in C11

In the past few days, I have been working on a fun project: Manyan, formerly TLAPlusPlus.

While running TLA+ specs for Small Python, one thing that really blocked me was the time it took to run them. I use a lot of TLA+ specs to model the C implementation, and the expensive run time kills me.

I was never into Java. So I asked Claude Code to build one in C11. Just one C file and one header file. I asked it to produce a language specification first, then test262-style test specs, and then use TLC as the oracle. The outcome is amazing: it’s slightly faster than the official Java version. v0.1.0

Making it actually pass all the Small Python TLA+ specs is a real-world testament. To make that happen, more bugs were fixed. v0.2.0

I wasn’t satisfied. I wanted it faster. So after another overnight session, v0.4.0 made it 4x-20x faster than TLC.

I wanted to share it with the world, but I wasn’t confident it actually worked well. So I asked the agent to find more real-world examples, and in turn it fixed more bugs. v0.5.0

I shared it with the Reddit community (post) and got some good feedback from Markus Alexander Kuppe (core TLA+ maintainer) and Fabrício Bracht (author of TLA-RS). v0.6.1 was made from that feedback while I was playing chess, Chinese chess, poker, and football with my family on Sunday.

Kuppe also suggested tlaplus/examples to me, and I gave it another bench. That is nice: a 20x speed-up, with 70% of the examples covered (the rest are Java-specific or use Apalache symbols).

Kuppe also suggested a rename. I took it seriously and renamed it Manyan. Manyan (曼衍, màn yǎn, said “mahn-yen”) comes from Zhuangzi’s Qi Wu Lun (齐物论): “和之以天倪,因之以曼衍.” It evokes continuous unfolding and transformation.

So this is the short story of making software that actually solves my own pain and can benefit others.

Rust vs C for AI Agents

AI agents are good at producing Rust code, way too good. But I think it’s worth evaluating whether agents would do the same using the C language. I wonder if there is a formal investigation on Rust vs C regarding development velocity.

Rust is known for memory safety, but that doesn’t mean a program written in C is memory unsafe, especially with the recent trend of adding formal verification (TLA+, Lean, Blend, etc.) to code. It used to be impractical to ship something like TLA+ specs along with C code, but with AI it’s almost zero friction.

As a result, when I was working on Small Python, in one of the sessions Claude Code kept trying to find ways to segfault the program, yet it failed. After several hours of trying, it gave up and claimed the implementation was rock solid.

Adding Metering to Small Python

Today is another day of me staying completely outside my home office. I enjoyed a short road trip, dined on Chicken Alexander, and watched my wife play chess with my son (he won in the end with a promotion). Meanwhile, Claude Code kept running, and as of writing, it is still running. Wow. What an entire day!

What it’s working on is a metering feature for Small Python, which is essential for the upcoming sandbox features.

A traditional language wouldn’t pay much attention to this feature, because its programs are supposed to run as long as possible, like server applications or GUI applications. Small Python is different. It is intended to be embedded or run by agents, so being metered is essential: the host application must be able to stop the code if it eats too much.

The current design is pretty much aligned with QuickJS. It introduces meters over running steps and memory, so that code like while True: pass won’t run forever.

A fun thing I observed during the run is that it often reverts its changes or keeps refactoring. That’s because I have a rule: optimize or revert if the benchmark shows degradation. It even reverted once because of a +0.01% performance loss that it couldn’t find a way to work around. I feel I may be too harsh, and I doubt I could do it myself, but Opus 5.5 did it pretty well.

As the sessions went on, I gained more confidence. It’s not just passing the tests, the fuzzing, the formal verification, and the benchmarks. It’s how it gets there: it’s showing the characteristics of a C programmer with 30 years of experience.

Small Python v0.4.0: Close to CPython Speed

This is a follow-up to the Small Python idea. I burned one more day of tokens to benchmark it and improve the performance. The setup is the same: one single Claude Code session, with /remote-control enabled so that I can prompt it while I’m out. In the morning I attended a local farm event, and in the afternoon we went trick-or-treating for Halloween.

The agent is capable of finding opportunities and fixing them correctly. Often it can run a few hours before it prompts me with more potential things to fix. I generally just ask it to continue.

At one point it stopped and claimed it wouldn’t gain much more, while it was still 1.3x–1.5x slower than CPython. I gave it a few ideas, and now it runs at 0.98x–1.1x of CPython.

In the end, I’m more than happy to ship a new version: Small Python v0.4.0.

Building a Single-File Python Interpreter

I’m working on a fun project: small-python. Implement a QuickJS-like single C11 source file that can do the same for Python language core, no Lib included. This goal is ambitious and it could be used for agents to run untrusted code.

To make that happen, I cannot simply say “hey Claude, implement a QuickJS-like project but for Python.” Instead, a few constraints are needed:

I’ve burned about $450 so far. Mostly using one single Claude Opus 5.5 session. The agent has been running for two days, and I’m steering, playing with my son, or going to sleep in the middle. Typically one steering session can run for several hours as I often say “don’t stop until.”

To be fair, I peeked at the C code once. It’s pretty understandable—just like the C parser code I wrote over the years. Nothing magic, but it’s substantial already.