Files
LithosAnanke/v4/system/post.c
T
Claude 281972abb2 perf(v4): POST at a boot's pace -- v3's per-suite put-back, and a lookup that rejects by two bytes (#189)
Ruled (Captain Bob, 2026-11-14): "Find the TCG cost first". v3's whole POST took the amd64
accept from about 3 m 15 s to 8 m 45 s, and aarch64 past its 570 s (logs/20261114-091651,
STOPPED, committed). Profiled on the host (gprof, mesh-boot): 87% of the time in POST's
lines, about 280,000 opcodes a line -- the interpreter's dictionary lookup.

- POST puts the dictionary back before each of v3's suites, as v3's run_test_suite does
  ("to remove test-created words"): a mark, (POST), forgotten and made again
  (V4_POST_SUITE). Only the dictionary: SCR, BASE and the rest stay as the cases left them.
- (LOOKUP) tries each entry by its count and first character first -- one fetch of the
  name's first cell, masked -- and only then its hidden flag and the whole name. The answer
  is the same; an entry that cannot be it costs a few opcodes, not some forty.

mesh-boot: 37.7 s -> 15.7 s, POST 990 of 990. make -C v4 test: all v4 tests passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
2026-11-14 13:26:45 +00:00

154 lines
6.6 KiB
C

/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* post.c -- POST's runner: the kernel feeds a case to the node and judges it
* from outside. See post.h; docs/v4.0.0/NUCLEUS.md section 6.3.
*
* Nothing here uses the C library: the bare-metal kernel links this file.
*/
#include "v4/post.h"
#include "v4/message.h"
#define SHOWN_OUT 255u /* how much of a failing case's printing is shown */
#define SHOWN_STACK 32u /* and how many of the values it left */
static const char state_line[] = "DECIMAL FORTH DEFINITIONS";
/* Before each of v3's suites the words the cases before it defined go, as v3's run_test_suite puts back LATEST and
* HERE (and nothing else: SCR, BASE and the rest stay as the cases left them). A mark, (POST), is made at a suite's
* start and forgotten at the next's: FORGET takes the dictionary, the word IDs and the kernel's records of them. A
* case that raised the fence above the mark (FENCE) has its FORGET refused, and the next mark is made above it. */
static const char *const put_back[] = { "DECIMAL FORTH DEFINITIONS", "FORGET (POST)", "CREATE (POST)" };
static unsigned length(const char *s)
{
unsigned n = 0;
while (s[n]) n++;
return n;
}
static void say(const v4_post_host *h, const char *s)
{
h->say(h->self, s, length(s));
}
static void say_number(const v4_post_host *h, long long v)
{
char text[24];
unsigned at = sizeof text;
unsigned long long u = v < 0 ? 0ull - (unsigned long long)v : (unsigned long long)v;
do { text[--at] = (char)('0' + u % 10u); u /= 10u; } while (u);
if (v < 0) text[--at] = '-';
h->say(h->self, text + at, (unsigned)sizeof text - at);
}
int v4_post_run(const v4_post_host *h, const v4_post_case *cases, unsigned count, v4_post_tally *tally)
{
static char out[V4_POST_OUT]; /* what the case being run has printed */
static v4_cell left[V4_DATA_DEPTH]; /* what it left on the stack, the top first: all of it, to compare (#189: v3's
cases leave 101); SHOWN_STACK of it shown */
unsigned i, k;
tally->tests = 0; tally->pass = 0; tally->fail = 0;
for (i = 0; i < count; i++) {
const v4_post_case *c = &cases[i];
const int kind = c->expect & V4_POST_KIND;
unsigned kept = 0, total = 0, len = 0, depth;
int errored = 0, came_back = 1, lost = 0, no_state = 0, quit = 0, ok, how;
/* a suite begins: what the cases before it defined goes, as v3 does after each suite */
if (c->expect & V4_POST_SUITE) {
for (k = i ? 0u : 2u, how = 0; how >= 0 && k < 3u; k++) /* the first suite: only the mark */
how = h->line(h->self, put_back[k], length(put_back[k]), out, V4_POST_OUT, &len);
if (how < 0) {
say(h, "POST: the node did not come back from putting the dictionary back before "); say(h, c->name);
say(h, "; "); say_number(h, (long long)(count - i)); say(h, " not run\n");
tally->fail++;
break;
}
}
if ((c->expect & V4_POST_KERNEL) && !h->kernel) continue; /* no kernel: its words are not there */
/* every case starts from the same state */
v4_dstack_reset(&h->n->ds);
how = h->line(h->self, state_line, (unsigned)sizeof state_line - 1u, out, V4_POST_OUT, &len);
if (how < 0) lost = 1;
if (how != V4_TEXT_COMPLETED) { /* what the node said of it is what is shown */
came_back = 0; no_state = 1;
total = len; kept = len < V4_POST_OUT ? len : V4_POST_OUT;
}
v4_dstack_reset(&h->n->ds);
for (k = 0; came_back && k < c->line_count; k++) {
len = 0;
how = h->line(h->self, c->lines[k], length(c->lines[k]), out + kept, V4_POST_OUT - kept, &len);
if (how < 0) { came_back = 0; lost = 1; break; }
if (how == V4_TEXT_ERROR) errored = 1;
if (how == V4_TEXT_QUIT) quit = 1; /* it went back to the terminal: it did not complete */
total += len;
kept = total < V4_POST_OUT ? total : V4_POST_OUT;
}
/* what it left, read before anything is judged */
depth = h->n->ds.depth;
for (k = 0; k < depth; k++) {
v4_cell v = v4_dstack_pop(&h->n->ds);
if (k < V4_DATA_DEPTH) left[k] = v;
}
v4_dstack_reset(&h->n->ds);
if (!came_back || quit) {
ok = 0;
} else if (kind == V4_POST_ERROR) {
ok = errored;
} else if (errored) {
ok = 0;
} else {
ok = depth == c->depth;
if (ok && kind == V4_POST_STACK) {
if (depth > V4_DATA_DEPTH) ok = 0;
for (k = 0; ok && k < depth; k++) ok = left[k] == (v4_cell)c->stack[depth - 1u - k];
}
if (ok && kind == V4_POST_HEAD) {
ok = c->output_len <= kept;
for (k = 0; ok && k < c->output_len; k++) ok = out[k] == c->output[k];
} else if (ok && kind != V4_POST_DEPTH) {
ok = total == c->output_len && total <= V4_POST_OUT;
for (k = 0; ok && k < total; k++) ok = out[k] == c->output[k];
}
}
tally->tests++;
if (ok) { tally->pass++; continue; }
tally->fail++;
say(h, "POST FAIL: "); say(h, c->name);
if (no_state) say(h, " could not be put in its starting state:");
if (quit) say(h, " ended with QUIT:");
say(h, " out<"); h->say(h->self, out, kept < SHOWN_OUT ? kept : SHOWN_OUT);
say(h, "> stack<");
for (k = depth < SHOWN_STACK ? depth : SHOWN_STACK; k-- > 0; ) { say_number(h, (long long)left[k]); say(h, " "); }
say(h, ">\n");
/* The node did not come back from a line: it is stopped, or will
* run for ever. Nothing more can be asked of it, so POST ends
* here and says which case it was. */
if (lost) {
say(h, "POST: the node did not come back from "); say(h, c->name);
say(h, "; "); say_number(h, (long long)(count - i - 1u)); say(h, " not run\n");
break;
}
}
say(h, "PARITY:V4_POST tests="); say_number(h, (long long)tally->tests);
say(h, " pass="); say_number(h, (long long)tally->pass);
say(h, " fail="); say_number(h, (long long)tally->fail);
say(h, "\n");
return tally->fail == 0;
}
unsigned v4_post_runs(const v4_post_case *cases, unsigned count, int kernel)
{
unsigned i, n = 0;
for (i = 0; i < count; i++) if (kernel || !(cases[i].expect & V4_POST_KERNEL)) n++;
return n;
}