From ed43cacfc4ac6cb7b88152e3794233e6b396cc13 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 15:30:18 +0100 Subject: [PATCH] Self: scan as fast as the seed; jump without a jmp reg A table for the symbols that are one byte each (83 ms to 75, the seed's), jump as push and ret, and the README says what the code generator could still do: match as a jump table, faster slice equality. --- diff --git a/self/Jump.raz b/self/Jump.raz index d86a3c3..b537d5e 100644 --- a/self/Jump.raz +++ b/self/Jump.raz @@ -49,7 +49,8 @@ mov r15, [rdi + 48]; mov rcx, [rdi + 56]; mov eax, 1; - db 0FFH, 0E1H; (* jmp rcx, which the assembler has no form for *) + push rcx; (* a jump to rcx, for which the assembler has no form: *) + ret; (* push the place, and return to it *) end jump; end Jump. diff --git a/self/README.md b/self/README.md index 01004df..5e7faa6 100644 --- a/self/README.md +++ b/self/README.md @@ -26,18 +26,28 @@ `werror`, `recovery`, `skipping`) have setters. The `sbuf` array, which can't be sliced outside, is `strBytes`. - **Going on after an error.** raz has no `setjmp`: `Jump.raz` is the - two in assembly, which the parser's recovery needs. The assembler has - no `jmp reg`, so it is written `db 0FFH, 0E1H;`. + two in assembly. The parser's 200 or so error sites are deep in its + recursion and all end in a jump back to the statement or declaration + being read, which is how the seed reports up to 20 errors in a run + (15 of the 267 tests of messages show more than one). The assembler + has no `jmp reg`, so `jump` is `push rcx; ret`. - **`NO_COLOR` set to nothing.** `Sys.env` gives "" for a variable that is unset and for one that is empty, so the port reads the second as - the first, as no-color.org has it. The seed reads it as set. + the first. no-color.org says a variable that is empty doesn't turn + colour off, so the port is the one that follows it and the seed + differs; no test depends on either. - **Strings are slices.** A message is a `[]U8` made by `Diag.say`, which keeps what it makes: messages are made only to be shown. A name from `nameOf` stays valid, since names are kept in chunks that are never moved; the seed's was a copy. - **No bit cast.** A real's bits and back go through a pointer. - **Speed.** On the 348,025 lines of `test/genbench.py`, 7.6 MB, the - seed's scanner takes 75 ms and this one 83 ms. Two things cost the - rest: a slice comparison, `a[0..n] = b[0..n]`, is 25% slower than the - same bytes in a loop, and a procedure call to ask whether a byte is - in a name (a table now). The compiler could mend the first. + seed's scanner takes 75 ms and this one 74. It took 121 at first. + What closed the gap, in the source: a byte loop in place of the slice + comparison `a[0..n] = b[0..n]` (97 ms), a 256-entry table for the + bytes of a name (84), and one for the symbols that are a byte each + (75). What is left for the code generator, which would help every + module: a `match` on an integer is a chain of compares, each loading + the selector from the frame and jumping over a `jmp` (`je L1; jmp L2`), + where FPC makes a jump table; and the slice comparison is slower than + the loop it stands for. diff --git a/self/Scan.raz b/self/Scan.raz index 3d921b6..4ddc262 100644 --- a/self/Scan.raz +++ b/self/Scan.raz @@ -12,7 +12,7 @@ not closed: the first such error stops the compiler. The parser goes on after its own errors, through recovery below. *) module Scan; -import Sys, Heap, Mem, Fmt, Diag, Jump; +import Sys, Heap, Mem, Fmt, Str, Diag, Jump; type NamePtr = ^U8; @@ -60,6 +60,8 @@ "+", "-", "*", "/", "=", "#", "<", "<=", ">", ">=", ":=", ":", ";", ",", ".", "..", "...", "(", ")", "[", "]", "{", "}", "^", "&", "~", "|", "_"]; + (* the operators that no longer one starts, so that one byte is the symbol *) + alone = "+-*/#;,()[]{}^~"; PoolChunk = 65536; var @@ -81,6 +83,10 @@ since the loop that reads a name asks it of every byte. *) nameChar: [256]BOOL; + (* The symbol a byte is by itself, as + is, or sNull: a table, since + most symbols are of this kind and a match would test them one by one. *) + single: [256]Sym; + cur: State; (* buf .. f; tok, ival, rval and id live above *) (* The name table: every distinct identifier once, found through an @@ -870,6 +876,11 @@ cur.tokPos := cur.pos; cur.tokLine := cur.line; cur.tokLineStart := cur.lineStart; + if single[c] # sNull then + INC(cur.pos); + tok := single[c]; + return; + end; match c of | 'A'..'Z', 'a'..'z', '_': start := cur.pos; @@ -915,10 +926,6 @@ INC(cur.pos); sbuf[slen] := 0; tok := sString; - | '+': INC(cur.pos); tok := sPlus; - | '-': INC(cur.pos); tok := sMinus; - | '*': INC(cur.pos); tok := sTimes; - | '/': INC(cur.pos); tok := sSlash; | '=': if cur.buf[cur.pos + 1] = '=' then slip(2, sEql, "comparing is written =, not =="); @@ -933,7 +940,6 @@ INC(cur.pos); tok := sAmp; end; - | '#': INC(cur.pos); tok := sNeq; | '<': INC(cur.pos); if cur.buf[cur.pos] = '=' then INC(cur.pos); tok := sLeq; else tok := sLss; end; @@ -943,8 +949,6 @@ | ':': INC(cur.pos); if cur.buf[cur.pos] = '=' then INC(cur.pos); tok := sBecomes; else tok := sColon; end; - | ';': INC(cur.pos); tok := sSemicolon; - | ',': INC(cur.pos); tok := sComma; | '.': INC(cur.pos); if cur.buf[cur.pos] = '.' then @@ -953,13 +957,6 @@ else tok := sPeriod; end; - | '(': INC(cur.pos); tok := sLparen; - | ')': INC(cur.pos); tok := sRparen; - | '[': INC(cur.pos); tok := sLbrak; - | ']': INC(cur.pos); tok := sRbrak; - | '{': INC(cur.pos); tok := sLbrace; - | '}': INC(cur.pos); tok := sRbrace; - | '^': INC(cur.pos); tok := sArrow; | '|': if cur.buf[cur.pos + 1] = '|' then slip(2, sOr, "\"or\" is written or, not ||"); @@ -967,7 +964,6 @@ INC(cur.pos); tok := sBar; end; - | '~': INC(cur.pos); tok := sTilde; | '!': if cur.buf[cur.pos + 1] = '=' then slip(2, sNeq, "\"not equal\" is written #, not !="); @@ -987,6 +983,11 @@ begin for i := 0 to 255 do nameChar[i] := isLetter(U8(i)) or isDigit(U8(i)); end; + for i := 0 to NumOps - 1 do + if (LEN(ops[i]) = 1) and (Str.find(alone, ops[i]) >= 0) then + single[ops[i][0]] := Sym(INT(sPlus) + i); + end; + end; chunkLeft := 0; nameCap := 1 shl 12; namePtr := ^NamePtr(grab(nameCap * SIZE(NamePtr)));