Sami Bentaleb
All work

Tetris Lab

A browser-based Tetris solver. Give it a sequence of pieces and a target number of lines to clear, and it searches for a way to do it.

Year
2023, rewritten 2026
Stack
Rust, WebAssembly, Vite, TypeScript, Canvas 2D
Tetris Lab: a board, the piece sequence and the search's result.

The problem

Given a 20 by 10 Tetris board and a known sequence of pieces, can a search clear a target number of lines, and how? I wanted anyone to be able to check the answer, so Tetris Lab runs the search inside the visitor's browser tab and sets the visitor's own results beside a recorded run.

The first version, from November 2023, was a Python solver behind a Flask app. In 2026 I rewrote it as a Rust engine compiled to WebAssembly.

How the search works, written out beside the tool it describes.

My part

I built the search engine, the WebAssembly build and the browser lab on my own. For the 2026 rewrite I also checked in the seeded games behind the published results, with 2,000 games per configuration.

  • 2,000seeded games per configuration behind the published results

Architecture

One Rust engine, compiled to wasm32 with a handful of raw exports and no glue code. It runs a layered beam search over the known piece sequence. Each layer is one piece, a board is ten 32-bit column words, duplicate boards are dropped within a layer, a move that tops out is avoided where another exists, and the last three pieces are searched exactly. The evaluator sits behind a trait, and the shipped one is a Dellacherie and Thiery evaluator with eight fixed-point features, chosen by an ablation and not by preference.

The engine crate does no I/O and imports no wasm or JavaScript types, and the wasm crate holds the exported interface and nothing else. That split is what lets the native benchmark and the browser be required to give the same answer. The page is Vite, vanilla TypeScript and Canvas 2D with no runtime dependencies, and the search runs in a worker. The site is static files on Cloudflare: no server compute, no database, no account.

Six gates run on every push: the generator's fingerprint, manifest agreement, replay validity, native and wasm parity, determinism and size budgets. A deploy only follows a green run.

The Layers view: one row for each layer of the beam search.

Hard parts

Choosing the representation by measurement. The browser needs about ten million board evaluations a second. A spike ported the solver and benchmarked it on identical games. Scalar Rust over ten 32-bit column words reached 16.4 million evaluations a second in Node, against 8.6 thousand for the original numpy solver. 64-bit words were 2.1 times slower in wasm, and a SIMD version ran at 0.85 times the scalar speed because SIMD128 has no count-leading-zeros instruction, so the build stayed scalar.

Making the browser's number trustworthy. The engine has no random numbers and breaks ties by insertion order, so a game is reproducible from its seed. Scores are fixed-point integers, so the native benchmark and the wasm build agree exactly. Changing the order in which moves are visited changes results, so it has to show up as a deliberate update to a committed fingerprint.

Staying inside budgets. The wasm is 58,852 bytes and the whole page is 77,495 bytes gzipped, against a 150 KiB budget enforced by a gate. A beam width of 1,600 was measured and rejected because the default configuration missed its 200 ms budget at 203 ms natively, so the slider stops at 800.

The win-rate tab sets the visitor's own run beside the recorded one.

Outcome

Tetris Lab is live at lab.samibentaleb.com and its source is public. At the default beam width of 25, the 90th percentile solve takes 4.5 ms in wasm on the machine that recorded the reference run, and that run wins 1,000 of 1,000 games in both configurations. The 2023 Python version is still in the repository's git history.

  • 1,000 of 1,000games won by the reference run
  • 4.5 ms90th percentile solve in wasm
  • 25the default beam width

Video