Dirty Flag

In chess engine code a dirty flag is a boolean marker that signals cached attack maps or evaluation data must be recomputed after the position changes.

Category
Online Chess
Also in
Computer Chess
Difficulty
Advanced
Also known as
dirty bit, invalidation flag, cache-dirty flag, update flag

Dirty flag is a boolean variable used inside a chess engine to track whether certain cached data has become invalid after the board position changes.

Chess engines store precomputed information such as attack maps, king safety scores, and piece-square table values to avoid repeating expensive calculations on every node. When a move is made or unmade, some of that stored data no longer matches the current position. Rather than clearing or refreshing every cache entry immediately, the engine simply sets a single dirty flag to true. This signals that the affected data must be regenerated before it is used again.

The flag is normally checked at the start of the evaluation or move generation routines. If it is true, the engine recomputes only the necessary tables, then resets the flag to false. Because the test is extremely cheap, the engine can afford to perform it on almost every node while still saving the far larger cost of unconditional recalculation.

In practice the dirty flag is often paired with a move counter or hash key so that only the parts of the cache that actually depend on the changed squares are refreshed. This selective update keeps the engine fast without sacrificing correctness when the position evolves during search.

Frequently asked questions

What is a dirty flag in a chess engine?
A dirty flag is a boolean variable that marks cached data such as attack maps or evaluation scores as invalid after a position change.
Why do engines use a dirty flag instead of clearing caches on every move?
It avoids the cost of unconditional recalculation by setting one cheap flag that triggers selective refresh only when the data is next needed.
When is the dirty flag typically checked during search?
It is tested at the start of evaluation or move-generation routines, then reset after the required tables are recomputed.
How is the dirty flag often combined with other mechanisms?
It is paired with move counters or hash keys so that only the cache portions affected by changed squares are updated.

See also

Reviewed by Benjamin Himmel.

About the reviewer

Benjamin Himmel

Ben's been in love with chess since he was seven years old, when his grandfather taught him the Sicilian Defense on a well-worn board that had traveled through three generations of family game nights.

Read full profile