From 95b6de3447effee8176f727d708c63166c27f96a Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:42:26 +0100 Subject: [PATCH] PLAN: v0.7.0, better code; the budget to 10,000 lines rat at 1.48 times pt (FPC -O2 -Cr); seven steps from its profile and listings, with ulam's measured gains. Budget soft 10,000, hard 11,000; clarity first. --- diff --git a/CLAUDE.md b/CLAUDE.md index fedbec0..be893a1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -55,11 +55,15 @@ `Luis <42945842+ldiaz94@users.noreply.github.com>`, unsigned (`--no-gpg-sign`), no attribution trailer. Push to `main`. No pull requests unless asked. -- Budget: a soft limit of 8,500 lines of code (raised from 8,000 by - the owner's word, for register variables; blank and comment lines - not counted), with the compiler documented and commented throughout, - most of all where it is obscure or needs a compiler writer's - knowledge; at least 400,000 lines of raz a second. +- Budget: a soft limit of 10,000 lines of code, never past 11,000 + (raised from 8,500 by the owner's word, for better code; blank and + comment lines not counted), with the compiler documented and + commented throughout, most of all where it is obscure or needs a + compiler writer's knowledge; at least 400,000 lines of raz a second. +- Clarity comes first, before speed and size: the compiler must be a + model of plain code that a beginner can follow. Every rewrite of the + code it makes is explained where it is done, with the code before + and after, and in `doc/compiler.md`. - Style: clarity over cleverness; each unit opens with what it is, what it depends on and what it leaves out; comments say why. Match the report's plain, short prose in documentation. diff --git a/PLAN.md b/PLAN.md index 2b610ca..92a9695 100644 --- a/PLAN.md +++ b/PLAN.md @@ -5,8 +5,57 @@ stopping for the owner's review. A finished milestone is cut from this file: git keeps the history, and the report's section 12 the language's. -No milestone is under way. v0.6.0 is tagged; its steps 5 to 10 await +v0.7.0, better code, is under way. v0.6.0 is tagged; its steps 5 to 10 await the owner's review. + +## Under way: better code (v0.7.0) + +For: rat within 1.25 times the time of pt built by FPC `-O2 -Cr` +(today 634 ms against 428, 1.48 times), with the compiler still a +model of clarity. No change to the language. Measured on rat's 26 MB +workload, best of 9 interleaved runs, and on `make bench`. + +Where rat's time goes (ulam-prof): `Utf8.next` 22%, `Str.decode` 15%, +`Screen.put` 11%, `Parser.feed` 11%, `Screen.unsplit` 9%. Their +listings show the steps below. ulam's `doc/codegen.md` measured the +same rewrites in a compiler of the same kind; its gains are quoted. + +1. **A prologue that does only what it must.** A register variable + keeps a frame slot that is zeroed for nothing, and so do the slots + the registers are saved in: `Str.decode`, whose four variables are + all in registers, zeroes seven quadwords in a loop on every call. +2. **Two results in rax and rdx** for every procedure whose two + results are each one word, as a code procedure's are now; not + through a hidden pointer to the caller's slots. Changes a reading + of the report already taken: the owner's to approve. +3. **Values left where they are.** Gen's value stack keeps a value in + the place it is until it must move: a slice made from a slice + (`s[i..LEN(s)]`) isn't written to the frame and read back to be + passed; `v := v + x` adds into `v`'s register; a value is not moved + through `r8` on its way from one register to another; a value that + fits its type already isn't extended again (ulam's P1). +4. **Conditions as jumps.** `test` for a compare with zero; a `BOOL` + result tested where it is; a jump over a jump made one jump (ulam's + P7: with step 5, `heap` 11% faster); no dead jump after a `return` + or `break`; no check of what a slice's own length makes certain + (`s[i..LEN(s)]`'s upper bound). +5. **Addresses as the machine makes them.** An index scaled in the + address (`[rdi + rax*8]`), not by `imul` and `add`; a product by a + power of two as a shift; a constant on the right of `+ * & |`, + where it becomes an immediate (ulam's P5: `tree` 5 to 10% faster). +6. **`for` loops as FPC writes them**: stepped at the top, one compare + and one jump back at the bottom, the limit in a register when one is + free (ulam's P2: `matmul` 16% faster). +7. **Read by a beginner.** A reader with no part in this milestone + (a fresh agent) reads `gen.pas` and `doc/compiler.md` and lists + every place it can't follow; each is mended. `doc/compiler.md` + gains a section, "How the code is made good", walking one of rat's + procedures through the steps, before and after. + +Each step stops if it buys less than its lines: one that gains under +2% on rat and on no kernel is reverted and noted here. Short branches +(code 5 to 8% smaller in ulam's estimate) are left for after, measured +on their own. ## Next, when the owner says