feat(v4): Phase 7 Task 2b -- pipelining and the hot-words cache, built, off as v3 (#189)

Rulings, Captain Bob 2026-11-13: "Build a hot-words cache"; then, v3 being found to have both off
(ENABLE_PIPELINING, ENABLE_HOTWORDS_CACHE: n in Kconfig.physics and kernel/Makefile), "Built, off as v3".

- The kernel, at every call (mesh.c): v3's hit check, the transition, the likeliest next word (kept as each
  count rises; v3 scans 1024 a call -- the same word but in a tie v3's Q48.16 rounding makes), speculation
  at v3's thresholds into the node's cache; a word past heat 50 promoted at its call (v3 promotes at a
  lookup; v4's heat is the kernel's). Counters from the owner's heap, given back when a word goes.
- The node (dict.f18): (LOOKUP) tries (HOT)'s xts first, in F18, calling only C@; the kernel keeps it true
  (entries that go, or whose name is defined again, taken out). Empty unless the build has the cache.
- Tests: test_mesh_boot.c as built (57 checks) and with both on (mesh-boot-on, 65). v4 test: all pass.
- Reported, v3's own: when full, its promote takes the next slot without looking for the word there (a few
  hot words churn all 32 slots); its prefetch misses are never counted. Both reproduced.
- amd64 boots, RESULT: PASS, but 174 s and 181 s against 2a's 146 s, while on the host the mesh boot is no
  slower: being measured. Not pushed (held to the Phase 7 boundary).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-13 21:52:01 +00:00
parent b880a6eaed
commit 3a70e824a2
19 changed files with 473 additions and 145 deletions
+140 -4
View File
@@ -148,17 +148,32 @@ static int is_word(unsigned place, v4_cell a)
return a > 4 && a < (v4_cell)V4_NODE_WORDS && (bits_of(place)[a >> 3] >> (a & 7)) & 1u;
}
/* a record emptied, its transition counters given back */
static void record_clear(v4_mesh_account *a)
{
if (a->transitions && the->release) the->release(the->self, a->transitions);
a->transitions = 0;
a->calls = 0; a->since = 0; a->xt = 0;
a->heat = 0; a->last_decay_tick = 0; a->temperature_q8 = 0; a->state_flags = 0;
a->total_transitions = 0; a->most_likely_count = 0; a->most_likely_next = 0;
a->prefetch_attempts = 0; a->prefetch_hits = 0;
}
static void hot_evict(unsigned place, v4_cell w, v4_cell xt);
/* an entry has been made, its code at xt: it has a record from now */
static void word_made(unsigned place, const v4_node *n, v4_cell xt)
{
v4_cell id;
if (!the->accounts || xt <= 4 || xt >= (v4_cell)V4_NODE_WORDS) return;
bits_of(place)[xt >> 3] |= (unsigned char)(1u << (xt & 7));
hot_evict(place, 0, xt); /* a word of its name cached before: out, as v3's evict_name */
id = n->mem[xt - 4];
if (id >= 0 && id < (v4_cell)V4_MESH_WORD_IDS) {
v4_mesh_account *a = &accounts_of(place)[id];
a->calls = 0; a->since = 0; a->xt = xt;
a->heat = 0; a->last_decay_tick = 0; a->temperature_q8 = 0; a->state_flags = 0; /* as v3's physics_metadata_init */
record_clear(a);
a->xt = xt; /* as v3's physics_metadata_init: all else 0 */
}
}
@@ -168,6 +183,7 @@ static void words_gone(unsigned place, const v4_node *n, v4_cell w)
v4_cell a;
if (!the->accounts) return;
if (w < 5) w = 5;
hot_evict(place, w, 0); /* and out of the cache */
for (a = w; a < (v4_cell)V4_NODE_WORDS; a++) {
v4_cell id;
if (!is_word(place, a)) continue;
@@ -175,8 +191,7 @@ static void words_gone(unsigned place, const v4_node *n, v4_cell w)
id = n->mem[a - 4];
if (id >= 0 && id < (v4_cell)V4_MESH_WORD_IDS && accounts_of(place)[id].xt == a) {
v4_mesh_account *r = &accounts_of(place)[id];
r->calls = 0; r->since = 0; r->xt = 0;
r->heat = 0; r->last_decay_tick = 0; r->temperature_q8 = 0; r->state_flags = 0;
record_clear(r);
if (the->forgot) the->forgot(the->self, place, id);
}
}
@@ -217,6 +232,114 @@ static void window_record(v4_mesh_physics *p, uint32_t id)
}
}
/* ---- the hot-words cache: the node's (HOT), which (LOOKUP) tries first (dict.f18) ---- */
/* do the entries at xt1 and xt2 have the same name? (dict.f18: the name's word address at xt-2) */
static int same_name(const v4_node *n, v4_cell xt1, v4_cell xt2)
{
v4_cell a = n->mem[xt1 - 2], b = n->mem[xt2 - 2];
unsigned len = (unsigned)((v4_ucell)n->mem[a] & 31u), cells = (len + 4u) / 4u, i;
if (((v4_ucell)n->mem[b] & 31u) != len) return 0;
for (i = 0; i < cells; i++) if (n->mem[a + (v4_cell)i] != n->mem[b + (v4_cell)i]) return 0;
return 1;
}
/* take out slot i, the rest moved down (v3's hotwords_cache_remove_at) */
static void hot_remove(v4_node *n, v4_cell hot, v4_cell i)
{
v4_cell count = n->mem[hot], j;
for (j = i; j + 1 < count; j++) n->mem[hot + 3 + j] = n->mem[hot + 3 + j + 1];
count--;
n->mem[hot] = count;
n->mem[hot + 3 + count] = 0;
if (n->mem[hot + 1] >= count) n->mem[hot + 1] = 0;
}
/* entries gone (from word address w up), or one whose name is now another word's (xt's): out of the cache,
* so that what (LOOKUP) finds there is always what the dictionary would give */
static void hot_evict(unsigned place, v4_cell w, v4_cell xt)
{
v4_fabric_node *x = v4_fabric_node_at(&the->f, place);
v4_cell hot = the->im->hot, i;
if (!x || hot <= 0) return;
for (i = x->n.mem[hot]; i-- > 0; ) {
v4_cell e = x->n.mem[hot + 3 + i];
if (e == 0 || (w > 0 && e >= w) || (xt > 0 && e != xt && same_name(&x->n, e, xt))) hot_remove(&x->n, hot, i);
}
}
#if ENABLE_HOTWORDS_CACHE
/* v3's hotwords_cache_promote: full, the next slot in turn is taken; else, if not there, it goes at the end */
static void promote(unsigned place, v4_cell xt)
{
v4_fabric_node *x = v4_fabric_node_at(&the->f, place);
v4_mesh_physics *p = &the->physics[place];
v4_cell hot = the->im->hot, count, i;
if (!x || hot <= 0) return;
count = x->n.mem[hot];
if (count >= V4_IMAGE_HOT_SIZE) {
x->n.mem[hot + 3 + x->n.mem[hot + 1]] = xt;
x->n.mem[hot + 1] = (x->n.mem[hot + 1] + 1) % V4_IMAGE_HOT_SIZE;
p->hot_evictions++;
return;
}
for (i = 0; i < count; i++) if (x->n.mem[hot + 3 + i] == xt) return;
x->n.mem[hot + 3 + count] = xt;
x->n.mem[hot] = count + 1;
p->hot_promotions++;
}
static int hot_has(unsigned place, v4_cell xt)
{
v4_fabric_node *x = v4_fabric_node_at(&the->f, place);
v4_cell hot = the->im->hot, i;
if (!x || hot <= 0) return 0;
for (i = 0; i < x->n.mem[hot]; i++) if (x->n.mem[hot + 3 + i] == xt) return 1;
return 0;
}
#endif
#if ENABLE_PIPELINING
/* Loop #4 (v3's physics_pre_execute, the pipelining half): was this word the one the word before it
* predicted? -- then the transition, the likeliest next word, and speculation on it */
static void pipeline(unsigned place, v4_mesh_physics *p, uint32_t id)
{
v4_mesh_account *prev, *spec;
uint32_t next;
if (p->prev_id >= V4_MESH_WORD_IDS) return;
prev = &accounts_of(place)[p->prev_id];
if (!prev->xt) return;
if (prev->most_likely_next == id && prev->prefetch_attempts > 0) { prev->prefetch_hits++; p->prefetch_hits++; }
if (!prev->transitions) { /* on its first transition, as v3 */
if (!the->alloc) return;
prev->transitions = (uint64_t *)the->alloc(the->self, (unsigned long)V4_MESH_WORD_IDS * sizeof(uint64_t));
if (!prev->transitions) return;
}
prev->transitions[id]++;
prev->total_transitions++;
/* the likeliest next: v3 scans every ID for the highest count, lowest ID on a tie; the same, kept as
* each count rises (v3 compares Q48.16 probabilities, which can round two counts equal: then it picks
* the lower ID where this keeps the higher count) */
if (prev->transitions[id] > prev->most_likely_count
|| (prev->transitions[id] == prev->most_likely_count && id < prev->most_likely_next)) {
prev->most_likely_count = prev->transitions[id];
prev->most_likely_next = id;
}
next = prev->most_likely_next;
if (prev->total_transitions < MIN_SAMPLES_FOR_SPECULATION) return;
if ((int64_t)((prev->transitions[next] << 16) / prev->total_transitions) < (int64_t)SPECULATION_THRESHOLD_Q48) return;
spec = &accounts_of(place)[next];
if (!spec->xt) return;
#if ENABLE_HOTWORDS_CACHE
if (p->l1_cache) {
spec->heat = V4_MESH_HOT_HEAT + 1u; /* as v3 */
promote(place, spec->xt);
prev->prefetch_attempts++;
p->prefetch_attempts++;
}
#endif
}
#endif
/* at `call` (exec.h), on a word -- any other target, nothing -- in v3's order (physics_pre_execute,
* V3-PARITY.md 2.1; Captain Bob, 2026-11-13: "Every call, as v3"): decay by the ticks since, heat +1,
* the count the Stadium is charged by, the temperature, the rolling window */
@@ -237,6 +360,16 @@ static void on_call(void *ctx, v4_node *n, v4_cell target)
a->heat++;
a->temperature_q8 = temperature(a->heat, a->temperature_q8);
if (p->l2_window) window_record(p, (uint32_t)id);
#if ENABLE_PIPELINING
if (p->l4_pipelining) pipeline(place, p, (uint32_t)id);
#endif
#if ENABLE_HOTWORDS_CACHE
/* hot enough, and not cached: in (v3 promotes when a lookup finds such a word in the dictionary; on
* v4 the heat is the kernel's, so it is done at the call where the word is hot -- the same words,
* at their call rather than their lookup) */
if (p->l1_cache && a->heat > V4_MESH_HOT_HEAT && !hot_has(place, target)) promote(place, target);
#endif
p->prev_id = (uint32_t)id;
}
a->calls++;
a->since++;
@@ -274,7 +407,7 @@ static void words_attach(unsigned place, v4_fabric_node *x)
unsigned k;
if (!the->accounts || !the->is_word) return;
for (k = 0; k < WORD_BITS_BYTES; k++) bits_of(place)[k] = 0;
for (k = 0; k < V4_MESH_WORD_IDS; k++) { accounts_of(place)[k].calls = 0; accounts_of(place)[k].since = 0; accounts_of(place)[k].xt = 0; }
for (k = 0; k < V4_MESH_WORD_IDS; k++) record_clear(&accounts_of(place)[k]);
for (xt = x->n.mem[im->latest], k = 0; xt > 4 && xt < (v4_cell)V4_NODE_WORDS && k < V4_NODE_WORDS; xt = x->n.mem[xt - 1], k++) {
word_made(place, &x->n, xt);
if (!told && named(&x->n, xt, "(TELL-DEFINED)")) told = xt;
@@ -295,6 +428,9 @@ static void words_attach(unsigned place, v4_fabric_node *x)
p->l3_decay = 1;
p->l2_window = 1; /* v3: "No config yet: recorded" */
p->effective_window_size = V4_MESH_WINDOW;
p->prev_id = V4_MESH_NO_WORD;
p->l4_pipelining = ENABLE_PIPELINING ? 1 : 0; /* the build's ceiling; L8 turns it within it */
p->l1_cache = ENABLE_HOTWORDS_CACHE ? 1 : 0;
}
x->es.on_call = on_call;
x->es.on_call_ctx = &the->kernel[place];