CaliChess Guides

Guides ·

Building a 3000 Elo Chess Engine in Rust and WebAssembly

The engine behind calichess.cc was written from scratch in Rust, measured at about 3039 Elo on the CCRL Blitz scale, and compiled to WebAssembly so it runs in a browser tab. This is how, with the numbers.

In one breathEvery change to the engine had to win a match of real games against the previous version before it was kept. Changes that could not prove they helped were thrown away, even when they looked smart.

The rule: games decide, not intuition

Chess engines are full of ideas that sound good and make the engine weaker. So each change played the previous version in a sequential probability ratio test (SPRT): keep playing games until the statistics can say "better" or "not better" with set confidence.

Bar chart of Elo added per engine version, from +315 for the tuned evaluation to +13 for new king safety terms
Elo added by each accepted change. The first version, written from scratch, beat the old search 100 games to 0.

Measuring strength honestly

Beating your own older versions only says you improved, not how strong you are. For that, the engine played 120 games against each of five versions of Stash, an open-source engine with known ratings.

OpponentIts CCRL ratingRating our result implies
Stash 2127133072
Stash 2529323008
Stash 2830823059
Stash 3132073083
Stash 3433142948
All 600 games~3039

Speed: same thinking, less time

The biggest single gain did not change what the engine thinks at all. It searched the exact same tree of moves, node for node, about 1.9 times faster: hopeless moves are now thrown out before they are played on the board, memory is reused instead of cleared at every step, and scores already computed are kept. A fixed benchmark visited exactly 6,891,892 positions before and after, which proves the thinking did not change. That alone was worth about 117 Elo.

Position (5 s each, one thread, Apple M4)DepthPositions / s
Starting position192.28 M
Closed d4 middlegame202.40 M
Kiwipete201.86 M
Sicilian middlegame192.30 M
Bishop endgame313.34 M
Italian middlegame232.74 M

Into the browser

Rust compiles to WebAssembly with no code changes. The engine runs in a background thread (a Web Worker), so the board never freezes while it thinks. The download is 191 KB compressed. In the browser it reaches 1.0 to 1.5 million positions per second, and the move finder answers at depth 9 to 10 in about 0.2 seconds.

Nothing leaves the machine: the build fails if any network call appears in the engine, the app or the build scripts.

What did not work

IdeaResult
Singular extensions−8 ± 16
Evaluation re-trained on deep engine scores of Lichess positions−159 ± 43
Evaluation re-trained on its own search scores−21 ± 17
An evaluation cache+2% speed, inside the noise

One bug taught the biggest lesson: adding "skip captures that lose material" first cost 80 Elo. The idea was right; the function that judges a trade had a bug. After fixing it, the same idea gained +60.

The full write-up, with every commit and test, is in the case study on GitHub.

Questions

How was the Elo measured?

600 games at 8 seconds plus 0.08 s per move against five versions of the open-source engine Stash, whose CCRL ratings are known. The result was about 3039 CCRL Blitz, with a 95% range of 3007 to 3071.

Why Rust?

It is as fast as C for this kind of work, it catches many bugs before the program runs, and it compiles to WebAssembly without changes.

How fast is it in the browser?

1.0 to 1.5 million positions per second. Native, on an Apple M4, it does 1.9 to 3.3 million on one thread.

Is the code open?

Yes, under the MIT license, at github.com/Caliko06/chess-engine-browser.

Open the engine →