v4 fabric: each pass of a step walks only the places that hold a node (mechanism A)

The fabric keeps its occupied places as a list in ascending order, and
the step's nine passes walk that list in the same order as before. A
place removed mid-walk keeps its link, so the walk goes on to the next
node.

Test first: test_fabric_walk.c, 13 checks.

Every existing fabric and mesh test passes with the same checks, and so
do test-32 and ASan/UBSan. The host mesh boot runs POST 990/990. Over
five alternating runs its median falls from 11.695 s to 7.999 s.

Logs are in logs/20261117-mechanism-a.

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-17 22:23:58 +00:00
parent d34fa415c3
commit edbb79e2af
9 changed files with 802 additions and 11 deletions
+9
View File
@@ -56,6 +56,7 @@ typedef struct {
int asleep;
int cut; /* not 0: a wire of this node's has been cut since it was last woken by that (operation 9) */
v4_wire wire[V4_PORTS];
unsigned next; /* the walk (mechanism A): the next place that holds a node, plus one; 0: none. The fabric's own. */
} v4_place;
typedef struct {
@@ -65,6 +66,7 @@ typedef struct {
unsigned queue_count, queue_free; /* how many, and how many are not on a wire */
unsigned queue_first; /* the first that is not, and the index plus one; 0: none. Each links to the next by its `head`. */
v4_cell gone_error; /* see v4_fabric_gone_error; 0: none */
unsigned first; /* the walk: the first place that holds a node, plus one; 0: none */
} v4_fabric;
/* An empty fabric with room for `capacity` nodes, in `places`. */
@@ -115,6 +117,13 @@ void v4_fabric_gone_error(v4_fabric *f, v4_cell code);
* a word at a time, which executes from its ports and has asked nothing of
* a wire. */
/* THE WALK (mechanism A, docs/v4.0.0/plans/2026-11-17-the-fabric-step-cost.md). The places that hold
* a node, in ascending number: each pass of a step walks these, not every place, in the same order.
* v4_fabric_first gives the first, v4_fabric_next the one after `id` -- also when `id` has just been
* removed -- and each gives the capacity when there are no more. */
unsigned v4_fabric_first(const v4_fabric *f);
unsigned v4_fabric_next(const v4_fabric *f, unsigned id);
/* More room: `places`, of `capacity` no less than before, takes over; every
* node keeps its number and its wiring. */
void v4_fabric_grow(v4_fabric *f, v4_place *places, unsigned capacity);
+44 -11
View File
@@ -29,7 +29,38 @@ void v4_fabric_init(v4_fabric *f, v4_place *places, unsigned capacity)
f->queue_free = 0;
f->queue_first = 0;
f->gone_error = 0;
for (i = 0; i < capacity; i++) clear_place(&places[i]);
f->first = 0;
for (i = 0; i < capacity; i++) { clear_place(&places[i]); places[i].next = 0; }
}
unsigned v4_fabric_first(const v4_fabric *f)
{
return f->first ? f->first - 1u : f->capacity;
}
unsigned v4_fabric_next(const v4_fabric *f, unsigned id)
{
if (id >= f->capacity) return f->capacity;
if (f->place[id].node) return f->place[id].next ? f->place[id].next - 1u : f->capacity;
for (id++; id < f->capacity && !f->place[id].node; id++) { } /* removed while walked: the next that holds one */
return id;
}
/* a node is now at `id`: into the walk, in order */
static void walk_in(v4_fabric *f, unsigned id)
{
unsigned *at = &f->first;
while (*at && *at - 1u < id) at = &f->place[*at - 1u].next;
f->place[id].next = *at;
*at = id + 1u;
}
/* the node at `id` is going: out of the walk. Its own link is kept, so a walk at it goes on. */
static void walk_out(v4_fabric *f, unsigned id)
{
unsigned *at = &f->first;
while (*at && *at - 1u != id) at = &f->place[*at - 1u].next;
if (*at) *at = f->place[id].next;
}
void v4_fabric_gone_error(v4_fabric *f, v4_cell code)
@@ -42,7 +73,7 @@ void v4_fabric_grow(v4_fabric *f, v4_place *places, unsigned capacity)
unsigned i;
if (capacity < f->capacity) return;
for (i = 0; i < f->capacity; i++) places[i] = f->place[i];
for (; i < capacity; i++) clear_place(&places[i]);
for (; i < capacity; i++) { clear_place(&places[i]); places[i].next = 0; }
f->place = places;
f->capacity = capacity;
}
@@ -54,6 +85,7 @@ int v4_fabric_add(v4_fabric *f, v4_fabric_node *storage, v4_cell port_base)
if (i == f->capacity || !storage) return -1;
clear_place(&f->place[i]);
f->place[i].node = storage;
walk_in(f, i);
v4_node_born(&storage->n, port_base);
v4_exec_reset(&storage->es);
v4_heat_reset(&storage->h);
@@ -213,6 +245,7 @@ v4_fabric_node *v4_fabric_remove(v4_fabric *f, unsigned id)
if (id >= f->capacity || !f->place[id].node) return 0;
for (k = 0; k < V4_PORTS; k++) cut(f, id, k);
storage = f->place[id].node;
walk_out(f, id);
clear_place(&f->place[id]);
return storage;
}
@@ -425,7 +458,7 @@ unsigned v4_fabric_step(v4_fabric *f)
unsigned i, k, done = 0;
/* which of its wires have a message waiting, for each node to fetch */
for (i = 0; i < f->capacity; i++)
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
if (awake(f, i)) {
unsigned have = 0;
for (k = 0; k < V4_PORTS; k++) {
@@ -437,17 +470,17 @@ unsigned v4_fabric_step(v4_fabric *f)
/* a node that is blocked fetches nothing, and is looked about for when
* it is next to execute */
for (i = 0; i < f->capacity; i++)
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
if (awake(f, i)) {
const v4_node *n = &f->place[i].node->n;
if (!n->stopped && !n->asking && !n->doing && !(n->reading && !n->given)) look_about(f, i);
}
for (i = 0; i < f->capacity; i++)
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
if (awake(f, i)) done += v4_exec_step_word(&f->place[i].node->n, &f->place[i].node->es, &f->place[i].node->h);
/* a node that fetched which wires have a message has seen what had come */
for (i = 0; i < f->capacity; i++)
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
if (awake(f, i) && f->place[i].node->n.have_fetched) {
for (k = 0; k < V4_PORTS; k++)
if (f->place[i].wire[k].rx) f->place[i].wire[k].rx->arrived = 0;
@@ -456,28 +489,28 @@ unsigned v4_fabric_step(v4_fabric *f)
/* the operations on the wires, each whole; the two that wait are looked at last, so that
* what every other node has put or taken this step has been, whatever their numbers */
for (i = 0; i < f->capacity; i++)
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
if (awake(f, i) && f->place[i].node->n.doing && f->place[i].node->n.do_op != 8 && f->place[i].node->n.do_op != 9) {
v4_cell how;
(void)operate(f, i, &how);
v4_node_done(&f->place[i].node->n, how);
done++;
}
for (i = 0; i < f->capacity; i++)
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
if (awake(f, i) && f->place[i].node->n.doing) {
v4_cell how;
if (operate(f, i, &how)) { v4_node_done(&f->place[i].node->n, how); done++; }
}
for (i = 0; i < f->capacity; i++)
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
if (awake(f, i) && f->place[i].node->n.asking) done += hand_over_write(f, i);
for (i = 0; i < f->capacity; i++)
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
if (awake(f, i) && f->place[i].node->n.reading && !f->place[i].node->n.given) done += hand_over_read(f, i);
/* a node blocked at one port that nothing is wired to; or blocked writing a
* word to what will not read one (fabric.h, A WORD NO ONE WILL READ) */
if (f->gone_error != 0)
for (i = 0; i < f->capacity; i++) {
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i)) {
v4_node *n;
if (!awake(f, i)) continue;
n = &f->place[i].node->n;
+73
View File
@@ -0,0 +1,73 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* test_fabric_walk.c -- mechanism A (docs/v4.0.0/plans/2026-11-17-the-fabric-step-cost.md 4): the fabric
* keeps the places that hold a node, and a step walks them, not every place. Their order is the places'
* order, as before: v4_fabric_first and v4_fabric_next give the occupied places in ascending number, and
* the capacity when there are no more. Checked: empty; nodes added, removed and added again (the order kept);
* a node removed while the walk is at it (the walk goes on to the next); one node in sixteen places (a walk
* of one, the boot's case); and grown room (the walk kept).
*/
#include "v4/fabric.h"
#include <stdio.h>
#include <string.h>
static int failures = 0, checks = 0;
#define CHECK(c,...) do{checks++; if(!(c)){failures++; printf("FAIL %s:%d: ",__FILE__,__LINE__); printf(__VA_ARGS__); printf("\n");}}while(0)
#define PB ((v4_cell)(V4_NODE_WORDS - 24u))
static v4_fabric_node pool[6];
static v4_place places[16], more[24];
static v4_fabric f;
/* the walk as a string of place numbers, e.g. "0 2 " */
static const char *walk(void)
{
static char s[128];
unsigned i, n = 0, guard = 0;
s[0] = 0;
for (i = v4_fabric_first(&f); i < f.capacity && guard < 64; i = v4_fabric_next(&f, i), guard++)
n += (unsigned)sprintf(s + n, "%u ", i);
return s;
}
int main(void)
{
unsigned k;
printf("v4 fabric walk tests\n");
v4_fabric_init(&f, places, 4);
CHECK(v4_fabric_first(&f) == f.capacity, "an empty fabric: nothing to walk");
for (k = 0; k < 3; k++) (void)v4_fabric_add(&f, &pool[k], PB);
CHECK(strcmp(walk(), "0 1 2 ") == 0, "three nodes: walked 0 1 2 (%s)", walk());
(void)v4_fabric_remove(&f, 1);
CHECK(strcmp(walk(), "0 2 ") == 0, "the middle one removed: 0 2 (%s)", walk());
CHECK(v4_fabric_add(&f, &pool[3], PB) == 1 && strcmp(walk(), "0 1 2 ") == 0, "one added takes place 1, and the order is kept (%s)", walk());
CHECK(v4_fabric_add(&f, &pool[4], PB) == 3 && strcmp(walk(), "0 1 2 3 ") == 0, "and one more at the end (%s)", walk());
(void)v4_fabric_remove(&f, 0);
CHECK(strcmp(walk(), "1 2 3 ") == 0, "the first removed: 1 2 3 (%s)", walk());
/* removed while the walk is at it: the walk goes on to the next, as the loop over every place did */
(void)v4_fabric_remove(&f, 2);
CHECK(v4_fabric_next(&f, 2) == 3, "a removed place's next is the next that holds a node (%u)", v4_fabric_next(&f, 2));
(void)v4_fabric_remove(&f, 3);
CHECK(v4_fabric_next(&f, 2) == f.capacity && v4_fabric_next(&f, 1) == f.capacity, "and none after: the capacity");
(void)v4_fabric_remove(&f, 1);
CHECK(v4_fabric_first(&f) == f.capacity, "all removed: nothing to walk (%s)", walk());
/* the boot's case: Hera alone in sixteen places */
v4_fabric_init(&f, places, 16);
(void)v4_fabric_add(&f, &pool[0], PB);
CHECK(strcmp(walk(), "0 ") == 0, "one node in sixteen places: a walk of one (%s)", walk());
CHECK(v4_fabric_step(&f) == 0 && pool[0].n.reading, "and the step runs it: blocked reading its ports");
/* more room: the walk is kept, and a node added in it is walked */
(void)v4_fabric_add(&f, &pool[1], PB);
v4_fabric_grow(&f, more, 24);
CHECK(strcmp(walk(), "0 1 ") == 0, "grown: the walk kept (%s)", walk());
for (k = 2; k < 6; k++) (void)v4_fabric_add(&f, &pool[k], PB);
CHECK(strcmp(walk(), "0 1 2 3 4 5 ") == 0, "and the nodes added after are walked (%s)", walk());
printf(" %d checks, %d failures\n", checks, failures);
return failures != 0;
}