feat: collect the tree when the node arena is full

Tree reuse keeps most of the previous tree, so the arena fills up fast.
In a 240-move self-play diagnostic of main at 8+0.08 with Hash=64, the
reused subtree alone was often 3.0-3.3M of the 3.35M nodes, and the tree
was full on 90 moves (37%). A full tree cannot expand anything any more;
for the rest of the move it only re-evaluates leaves.

Now a failed allocation triggers a collection after the playout. The
children of every node below a visit threshold are dropped (the node
keeps its visits and Q and becomes a leaf again, expanded anew on its
next visit), and the rest is packed in place with the same block
compaction as tree reuse. The threshold is the smallest one that brings
the tree down to half the arena; visits only shrink going down, so one
pass over the tree gives the size kept for every threshold.

In the same diagnostic with this change the tree was collected on 27 of
158 moves, once each, and search kept expanding afterwards; a collection
of a full 64 MB tree costs about 20-30 ms.

Checked: the -DRIGBY_VERIFY_TREE build verifies the tree after every
collection; UCI-driven self-play passed 700 checks / 428 collections
with 1 MB Hash and 476 checks / 205 collections with 16 MB, including
repeated searches of the same position and a root at halfmove 99.
uci, go wtime/nodes/infinite, stop and quit work. The bench is
unchanged because the bench never fills the tree.

Rebased from the earlier tree-gc branch onto main with the value
network and second-visit expansion.

Bench: 546018

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-15 10:52:06 +01:00
1 parent 0d5713f7f0
commit 967e0e0b90
3 files changed
+76 -11

No files matched your search

+12 -1
View File
@@ -38,6 +38,9 @@ public:
// Makes `new_root` the root and packs its subtree to the front of the arena;
// everything else is dropped. Returns the number of nodes kept.
size_t reroot(u32 new_root);
// Frees memory when the arena is full: the least visited parts of the tree
// are cut back to leaves until at most `keep_fraction` of it is in use.
size_t collect_garbage(double keep_fraction);
Node& operator[](u32 idx) { return nodes_[idx]; }
const Node& operator[](u32 idx) const { return nodes_[idx]; }
@@ -49,6 +52,11 @@ public:
private:
std::vector<Node> nodes_;
size_t used_ = 0;
// Keeps the subtree of `new_root`, dropping the children of every node with
// fewer than `min_visits` visits, and packs it to the front in place.
size_t compact(u32 new_root, u32 min_visits);
u32 prune_threshold(size_t target) const;
};
struct SearchLimits {
@@ -114,7 +122,10 @@ private:
u64 iterations_ = 0;
u64 depth_sum_ = 0;
int seldepth_ = 0;
bool tree_full_ = false;
bool gc_pending_ = false;
int gc_runs_ = 0;
// Share of the arena kept after a collection.
static constexpr double GC_KEEP_FRACTION = 0.5;
void playout();
// First visit of a node: settles terminal states, otherwise evaluates.