raz / commit 95b6de34

commit
95b6de3447effee8176f727d708c63166c27f96a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
3e451d8e
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.

patch · log

2 files changed, +59 -6

CLAUDE.md modified +9 -5

@@ -55,11 +55,15 @@
5555 `Luis <42945842+ldiaz94@users.noreply.github.com>`, unsigned
5656 (`--no-gpg-sign`), no attribution trailer. Push to `main`. No pull
5757 requests unless asked.
58-- Budget: a soft limit of 8,500 lines of code (raised from 8,000 by
59- the owner's word, for register variables; blank and comment lines
60- not counted), with the compiler documented and commented throughout,
61- most of all where it is obscure or needs a compiler writer's
62- knowledge; at least 400,000 lines of raz a second.
58+- Budget: a soft limit of 10,000 lines of code, never past 11,000
59+ (raised from 8,500 by the owner's word, for better code; blank and
60+ comment lines not counted), with the compiler documented and
61+ commented throughout, most of all where it is obscure or needs a
62+ compiler writer's knowledge; at least 400,000 lines of raz a second.
63+- Clarity comes first, before speed and size: the compiler must be a
64+ model of plain code that a beginner can follow. Every rewrite of the
65+ code it makes is explained where it is done, with the code before
66+ and after, and in `doc/compiler.md`.
6367 - Style: clarity over cleverness; each unit opens with what it is, what
6468 it depends on and what it leaves out; comments say why. Match the
6569 report's plain, short prose in documentation.

PLAN.md (history) modified +50 -1

@@ -5,8 +5,57 @@
55 stopping for the owner's review. A finished milestone is cut from this
66 file: git keeps the history, and the report's section 12 the language's.
77
8-No milestone is under way. v0.6.0 is tagged; its steps 5 to 10 await
8+v0.7.0, better code, is under way. v0.6.0 is tagged; its steps 5 to 10 await
99 the owner's review.
10+
11+## Under way: better code (v0.7.0)
12+
13+For: rat within 1.25 times the time of pt built by FPC `-O2 -Cr`
14+(today 634 ms against 428, 1.48 times), with the compiler still a
15+model of clarity. No change to the language. Measured on rat's 26 MB
16+workload, best of 9 interleaved runs, and on `make bench`.
17+
18+Where rat's time goes (ulam-prof): `Utf8.next` 22%, `Str.decode` 15%,
19+`Screen.put` 11%, `Parser.feed` 11%, `Screen.unsplit` 9%. Their
20+listings show the steps below. ulam's `doc/codegen.md` measured the
21+same rewrites in a compiler of the same kind; its gains are quoted.
22+
23+1. **A prologue that does only what it must.** A register variable
24+ keeps a frame slot that is zeroed for nothing, and so do the slots
25+ the registers are saved in: `Str.decode`, whose four variables are
26+ all in registers, zeroes seven quadwords in a loop on every call.
27+2. **Two results in rax and rdx** for every procedure whose two
28+ results are each one word, as a code procedure's are now; not
29+ through a hidden pointer to the caller's slots. Changes a reading
30+ of the report already taken: the owner's to approve.
31+3. **Values left where they are.** Gen's value stack keeps a value in
32+ the place it is until it must move: a slice made from a slice
33+ (`s[i..LEN(s)]`) isn't written to the frame and read back to be
34+ passed; `v := v + x` adds into `v`'s register; a value is not moved
35+ through `r8` on its way from one register to another; a value that
36+ fits its type already isn't extended again (ulam's P1).
37+4. **Conditions as jumps.** `test` for a compare with zero; a `BOOL`
38+ result tested where it is; a jump over a jump made one jump (ulam's
39+ P7: with step 5, `heap` 11% faster); no dead jump after a `return`
40+ or `break`; no check of what a slice's own length makes certain
41+ (`s[i..LEN(s)]`'s upper bound).
42+5. **Addresses as the machine makes them.** An index scaled in the
43+ address (`[rdi + rax*8]`), not by `imul` and `add`; a product by a
44+ power of two as a shift; a constant on the right of `+ * & |`,
45+ where it becomes an immediate (ulam's P5: `tree` 5 to 10% faster).
46+6. **`for` loops as FPC writes them**: stepped at the top, one compare
47+ and one jump back at the bottom, the limit in a register when one is
48+ free (ulam's P2: `matmul` 16% faster).
49+7. **Read by a beginner.** A reader with no part in this milestone
50+ (a fresh agent) reads `gen.pas` and `doc/compiler.md` and lists
51+ every place it can't follow; each is mended. `doc/compiler.md`
52+ gains a section, "How the code is made good", walking one of rat's
53+ procedures through the steps, before and after.
54+
55+Each step stops if it buys less than its lines: one that gains under
56+2% on rat and on no kernel is reverted and noted here. Short branches
57+(code 5 to 8% smaller in ulam's estimate) are left for after, measured
58+on their own.
1059
1160 ## Next, when the owner says
1261