Skip to content

From 108 Seconds to 3.4: Making Hindsight's On-Device Chess Analysis 32× Faster

Mohamed Almadih
11 min read
From 108 Seconds to 3.4: Making Hindsight's On-Device Chess Analysis 32× Faster

A few weeks ago I wrote about building Hindsight, a free chess game review tool for Chess.com and Lichess games. The web version runs Stockfish as WASM in your browser. Then I shipped an Android app built with Flutter, and that one runs Stockfish on the phone itself: no server, no queue, no bill.

The first build worked. It was also slow. On my Snapdragon 6 Gen 1 phone, reviewing a long game took 108 seconds. That's almost two minutes of watching a progress bar while your phone gets warm. After the work in this post, the same game takes 3.4 seconds.

This is how I got there, including the parts where my assumptions were wrong.


1. The Setup: What "Analysis" Actually Means

For every position in a game, Hindsight asks Stockfish for an evaluation and the best move. It compares that with the move you played and gives each move a verdict (Brilliant, Great, Best, Mistake, Blunder, and so on), plus accuracy and a rating estimate.

The original mobile pipeline was simple:

  • One Stockfish instance (via the multistockfish Flutter plugin, using its StockfishLight flavor)
  • Threads = min(3, cores - 1), so it could use several CPU cores
  • MultiPV = 2: return the two best lines for every position
  • Search to depth 14, Hash = 64 MB

All the numbers below come from one benchmark: a 75-move (150-position) Chess.com game at depth 14, run on a Snapdragon 6 Gen 1 (SM6450). That chip has 4 Cortex-A78 big cores and 4 Cortex-A55 little cores. I wrote a small benchmark app (integration_test/multipv_bench.dart) that runs the game and draws the timings on screen, because some phones hide app logs from logcat.

Baseline: 108.39s (a repeat run gave 96.13s).


2. Quick Wins Before the Rewrite

Before the deep work there were two easy changes, both measured on the phone.

Default to depth 14

The app's default quality was depth 16. On the phone, a 26-move game took 131 s at depth 16 and 50 s at depth 14, about 2.6× faster. The verdicts at depth 14 were good enough for a review, so quick (depth 14) became the default. Standard (16) and Deep (18) are still in settings.

Two engine lines instead of three

An early version asked for three lines (MultiPV = 3). The third line was only used for a 5-centipawn tie check. Dropping it was 1.4× faster on the phone and 1.6–2.5× faster on the web, and every grade stayed the same.

These helped, but the baseline in the rest of this post already includes both. The real problems were deeper.


3. Root Cause #1: More Threads Made It Slower

This was the most surprising finding. I assumed three threads would beat one. On this phone, it lost badly:

  • 1 thread × 3 (T3): 108.39s
  • T2: 87.48s
  • T1: 50.50s (2.15× faster than T3)

The cause is how Stockfish uses multiple threads. Lazy SMP runs several threads searching the same position and sharing a hash table. At deep searches this pays off. At shallow depths like 14–16, the threads mostly get in each other's way. I counted nodes:

  • T3 searched 183.26M nodes using 177.56 thread-seconds
  • T1 searched 19.75M nodes using 17.75 thread-seconds

That's 9.3× more nodes and 10× more CPU time to reach the same depth. On a phone, CPU time means heat and battery. Even at depth 16, T3 was still 2.66× slower than T1 (150.69s vs 56.55s).

The fix: Threads = 1. But one thread leaves seven cores idle, which matters later.


4. Root Cause #2: Paying for a Second Line We Rarely Use

MultiPV = 2 makes Stockfish compute the second-best line for every position. I checked the review logic to see where line 2 is actually read. It's used in only two places:

  1. The Great badge: you found the only good move, and line 2 is clearly worse.
  2. Retry alternatives: when you made a mistake, the app suggests a better move to try.

That applies to roughly 15–20% of plies. Yet every ply paid for it. On one thread, going from MultiPV=2 to MultiPV=1 cut the time from 50.50s to 20.03s, a 2.52× speedup.

So I split the analysis into two passes:

  • Pass 1: MultiPV = 1 for every position, with a node cap.
  • Pass 2: for the few plies that might be Great or need a retry alternative, run a narrow searchmoves search on just the candidate moves.

Pass 2 adds only 4.8% more nodes over 30 games (115.84M → 121.43M), and it keeps the retry feature and the web/mobile review format identical.

Node caps, validated against 30 real games

Pass 1 also got a node limit, so a single hard position can't take over the run. I didn't want to pick a number by feel, so I re-ran a 30-game calibration set (2,114 moves, 60 player-games) with several variants and compared their accuracy scores against Chess.com's own numbers.

The 150k node cap won:

  • 2.84× less CPU and 2.97× fewer nodes across the 30 games
  • Accuracy error (MAE vs Chess.com) went from 3.50 down to 3.24, with bias of almost zero (+0.03)

Faster and closer to Chess.com. I also rejected a few ideas that looked faster on paper:

  • Shallower search in already-decided positions (|eval| >= 750): 3.29× faster, but it pushed accuracy error back to 3.50 and doubled the number of large misgrades. When you're losing badly, a blunder still has to be graded as a blunder.
  • A two-tier depth 12 → 14 refine: more than half the positions needed refining, so it was slower and less accurate (MAE 3.38).

The final caps scale with depth: 150k nodes for quick, 400k for standard, none for deep.

Positions that need no search at all

The last part of phase 1 was skipping work entirely:

  • Forced moves: if only one legal move exists, skip the search and derive its score from the next position's eval.
  • Repeated positions: identical positions (by placement, side to move, castling rights and en passant) are searched once and the result is copied.
  • Insufficient material: a bare-kings ending is a draw. Return 0 and move on.
  • Earlier mate stop: when a mate is found, stop at depth >= min(2 × mate distance, 10).

Phase 1 result: 108.39s → 10.8s, about 10× faster. Median time per position: 40 ms.


5. Root Cause #3: The Plugin Was the Ceiling

Ten times faster was great, but 7 of the 8 cores were still idle. The obvious next step was to run several single-threaded engines in parallel, each on different positions. The multistockfish plugin couldn't do that, for three reasons:

  1. One instance per flavor. It's a singleton, so I couldn't create a pool of engines.
  2. Generic ARM build. It compiled with plain NEON (-DUSE_NEON=8) and never used the ARMv8.2 dot-product instructions that modern phone CPUs support. Stockfish's NNUE evaluation benefits a lot from them.
  3. A race condition. In its I/O layer (sfio.cpp), a stream tie flushed output outside the lock, so bestmove lines sometimes came out duplicated. I reproduced it in 8 of 12 games, against 0 of 341 with standard streams.

It also shipped two engine libraries I never used: libmultistockfish_chess.so (1.6 MB) and libmultistockfish_variant.so (1.8 MB), so 3.4 MB per ABI of dead weight.

So I removed the plugin and built my own.

A custom Stockfish build

I vendored StockfishLight (Stockfish 19 with the p_hm net), compiled it with the Android NDK, and benchmarked compiler flags with bench 16 1 13:

  • base (plugin flags): 349,334 nps
  • +dotprod (-march=armv8.2-a+dotprod): 481,185 nps (+37.7%)
  • +dotprod +flto: 512,340 nps (+46.7%)

Not every phone supports dot-product instructions, so the app ships two engine libraries and picks one at runtime with a CPU feature check:

cpp
// libhindsight_nnue.so: the NNUE net is embedded once and shared by both engine builds
extern "C" int sf_has_dotprod() {
return (getauxval(AT_HWCAP) & HWCAP_ASIMDDP) != 0;
}

Each engine library is 1.48 MB, and the shared NNUE library is 1.17 MB, all built with -O3 -flto and 16 KB page alignment for newer Android versions. Dart talks to it through a small C FFI wrapper. Running several engine instances in one process needed some care: one-time global init with std::call_once, a mutex around the hash-clear path, and a lock-free atomic stop flag.

Scheduling: the part I got wrong twice

With a pool of engines, the question is how to split the positions between them. I tried three approaches:

  • Static chunks (worker 1 gets plies 1–37, worker 2 gets 38–75, and so on): 3 workers took 22.39s, 4 workers 22.36s. The 4th worker added nothing, because everyone waits for whoever got the hardest middlegame slice.
  • Interleaving (worker n takes every n-th ply): better balanced, but adjacent positions no longer share a hash table, so each engine loses cache hits from the previous move.
  • tile4 work-stealing: workers grab 4 consecutive plies at a time from a shared queue. Neighbouring positions stay on the same engine (cache hits), and fast workers keep grabbing more (load balance).

With the full pipeline (dotprod + LTO, MultiPV 1, 150k nodes):

  • 3 workers, chunks: 5.13s
  • 3 workers, interleaved: 4.39s
  • 3 workers, tile4: 4.13s
  • 4 workers, interleaved: 4.14s
  • 4 workers, tile4: 3.35s

One more lesson: I tried pinning workers to the big cores with taskset. It made things slower: 4 pinned workers on the faster dotprod build took 18.19s, while 4 unpinned, interleaved workers on the base build took 15.26s. Android's scheduler moves threads between cores better than I can by hand, so I don't pin.

The pool size is min(4, cores / 2). The engine isolates stay warm between games, so a second review doesn't pay the startup cost again. Each engine has a 16 MB hash table (64 MB total on an 8-core phone). Cancelling a review takes less than 1 ms.

Phase 2 result: 3.2–3.4s. Median per position: 10–11 ms.


6. The Whole Journey in Numbers

Same game, same phone, depth 14:

  • Original (3 threads, MultiPV 2, plugin): 108.39s (1.0×)
  • 1 thread: 50.50s (2.15×)
  • 1 thread + MultiPV 1: 20.03s (5.41×)
  • Phase 1 (two-pass + 150k nodes + fast paths): 10.8s (~10×)
  • Phase 2 (custom FFI engine + dotprod/LTO + 4-worker tile4 pool): 3.35s (~32×)

On top of the speed:

  • ~10× less CPU time per review, so the phone stays cool
  • 3.4 MB per ABI of unused native code removed
  • Accuracy closer to Chess.com (MAE 3.50 → 3.24), not further away
  • No more duplicated bestmove lines from the plugin race

One honest caveat: these are measurements on one phone and one long game, and most rows are single runs. The 30-game calibration backs up the accuracy side, but your phone will give different absolute numbers.


7. What's Next

With the engine down to about 3 seconds, the slow part is now somewhere else:

  1. The AI coach call. The API waits about 3–6 seconds for the Llama 3.3 70B coach notes before it returns the review. That's now more than half the wait on a new game. The plan is to return the review right away and load coach notes in the background.
  2. Bring this to the web. The browser engine pool still uses the older MultiPV 2 pipeline. Porting two-pass, node caps and tile4 should give the web app roughly a 3× speedup.
  3. Precomputed opening evals: bundle evals for the ~8,000 known opening positions and skip the first moves of most games for free.

Takeaways

If you're running a compute-heavy engine on a phone:

  • Measure threads, don't assume. At shallow depth, one Stockfish thread was 2.15× faster than three.
  • Only compute what you read. Line 2 was used on ~20% of plies and was paid for on 100%.
  • Validate speedups against accuracy. The 30-game calibration is what let me ship the node cap with confidence, and what stopped me shipping the shortcut that misgraded blunders.
  • Your dependencies set your ceiling. The biggest jump (10× → 32×) needed replacing the plugin, not tuning it.
  • Let the OS schedule. Manual core pinning lost to Android's scheduler.

You can try the result on Hindsight. On Android, a long game now takes a few seconds to review instead of almost two minutes.

Related Articles