Skip to content

Mimic 0.4.3

Latest

Choose a tag to compare

@github-actions github-actions released this 28 Nov 08:31
· 2 commits to main since this release
31d87df

Step 0.4.3, loops and values in registers: a memory check in debug builds that stops at the first use of a freed value; every run fixture checked five ways in CI, and random valid programs run the same ways (seven daily seeds on the frozen release, rerun until all agreed; the three bugs they found fixed: an element of a list in registers assigned with an index of unknown type, a list released inside a later argument of the call holding it, and list headers and record fields read as never-changing memory, which let a read move past a release; that last fix gave back some speed, accepted by the owner: lcs up to 20% slower, regex 5–6% on one core); and the held-out set (40 programs written plainly, never used for tuning) made faster without a change to them: Mimic's time over C's on one core, as a geometric mean, from 4.65× to 3.61× on Linux, 4.18× to 3.17× on macOS and 4.33× to 3.33× on Windows. Loops that grow a short list, tuples holding lists and the steps of qoi go through the Mimic IR; text mapped character by character is one pass (caesar 2.7× faster); a function value capturing nothing is made once; push onto a short list stays inline; Windows release executables are a third of their size; ⍬ and x ⥊ [n] are typed; and tools/rc-count.py counts the values made in loop steps. Moved to 0.4.4, with the owner's approval and recorded in docs/versions.md: qoi within 1.5× of C and paths within 1.3× (now 1.9× and 1.75×), escape analysis, loops making no values in their steady state, and the gaps.md rows still open for 0.4.3.

Added

  • Values made in loop steps, counted: a MIMIC_COUNT_RC=1 build also counts the values made while an evolve or settle step runs (past each loop's first step, which may copy a start still shared elsewhere), and tools/rc-count.py lists the benchmarks whose loops make any in their steady state.
  • A matrix row assigned in one write: m[i] = [10 40 70] replaces row i (a number fills it; with more dimensions, fewer indices than dimensions replace that cell, cube[1, 0] = [7 8]), copied first when shared, a byte matrix keeping bytes. It stopped the program with needs one index per dimension; a list of the wrong length says how long the row is.
  • Character literals: `a` is the number 97 (a character's code, as char_code("a"); escapes `\n`, `\ , \x1b , \u{e9} ). Text work on bytes compares and computes with characters as numbers, (c - a + 13) % 26 + a ``, instead of making a one-letter string for each one.
  • ⍬, an empty list with a type (digraph \z): ⍬ is an empty [i64], ⍬[f64], ⍬[str], ⍬[Point] an empty list of that element type. A bare [] has no element type and, where nothing gives it one, sent the code using it the slow generic way; the prelude's [0] ↓ 1 (and [0.0] ↓ 1, …) for an empty typed list is now ⍬.
  • Every keyword has a glyph, each with a digraph: ⇑ pub, ⇄ mut, ⍰ if, ⋄ else, ⏎ return, ⇐ use, ≔ as, ⌁ extern, ☡ unsafe, ✓ trusted, ⊡ type, ⊨ match, ⊕ with, ● true, ○ false, and ⊢ test, ∀ for (property tests), ⎘ include, beside § module and ƒ fn. Words and glyphs mix freely; mimic fmt writes glyphs back as words, mimic glyphs lists them all, and the editor grammars (TextMate, tree-sitter) colour them as their words.
  • e else { … } is an expression: the value inside an Option or Result (Some, Ok), else what the block gives, anywhere an expression goes: if 1 < ≢ args { parse_int(args[1]) else { 1086 } } else { 1086 }, xs ¨ (s => parse_int(s) else { 0 }), and let port = parse_int(s) else { 8080 } (the block may still leave with return, as let … else did). The block must give the type inside (E0401) or something (E0504); a value that is not an Option or Result is E0406. Result gets its fallback this way too.
  • A memory check in debug builds. A freed value is marked, filled with junk and kept a while before its memory is used again; retaining, releasing or reading it afterwards stops the program at once, naming what was done and what the value was (memory check: a value ([i64], 4 elements) was read after it was freed). mimic lldb stops right there, with the stack. Release builds are unchanged.
  • Random programs, nightly (tools/fuzz.py, the fuzz workflow): valid programs written at random (numbers, lists, functions that call themselves with a list they grow, copies changed in place) run in release and debug, without the Mimic IR, on one thread and on many; all must print the same. A disagreement is kept with each way's output, to become a fixture.
  • math::to_bits, math::from_bits (and to_bits32/from_bits32 for f32), bytes::of_f64s and bytes::to_f64s: a float's bits as an integer, and floats as little-endian bytes, both exact (weights saved and read back bit for bit).
  • ∧/ and ∨/, the and and or reductions (all and any written as glyphs, as in APL): same ▷ ∧/, ∨/ flags.
  • math::tanh, math::sinh, math::cosh, math::log1p, math::expm1 and math::sigmoid (libm calls that fuse into array kernels like math::exp; log1p and expm1 exact near zero; sigmoid without overflow for large negative inputs), and unwrap_or for an Option: parse_int(s) ▷ unwrap_or(300). Reported by a user.

Changed

  • Text mapped character by character is one pass: chars(t) ¨ (c => …) ▷ join(sep) with a function giving text walks the bytes of t once, hands the function each character and appends its text at once: no list of one-character strings made, none joined. A pure function is asked once for each ASCII character, its texts kept for the rest of the loop. The held-out caesar 2.7× faster on one core; translit added to the corpus for it.
  • push onto a short list stays inline: a list of numbers in one of the runtime's pools (up to 256 bytes) told the compiled code nothing of its room, so every push onto it called the runtime, which looked up its block's size. The compiled code now knows the pools' sizes. A row grown to 45 numbers called the runtime for its first 32; levenshtein 32% faster.
  • A function value that captures nothing is made once: run(b, p, x => alpha(x) or digit(x)) or run(b, p, digit) made a new function value at every call and freed it after; one is now made the first time and shared, never freed. tokenize's scanning loop made 1.07 million values, now none; 8% faster.
  • A tuple holding a list, taken apart at once, stays compiled whole: let (bytes, op) = chunk(p, prev), a tuple of 2 to 4 numbers, lists and records, no longer sends the function back to the old code generator. The Mimic IR calls the version of the callee that gives its parts (no tuple made) and holds each part as a value of its own, a part not named released at once. qoi's encode_step and decode_step are now compiled whole. In a record update, a short list kept in registers is appended with no list made for it, concat(s.px, p ⥊ [n]) appends n copies in place, and an empty list appended is no call at all.
  • A loop that grows a short list stays compiled whole: first ▷ evolve(48, w => push(w, …)), its start a list of up to 16 numbers kept in registers and its state a list on the heap, puts the start on the heap once and pushes in place at each step. The loop went back to the old code generator, which copied the list at every push: sha256's message schedule freed 97 values per block, now 1 (3.18 million values in the benchmark, now 33 thousand).
  • gather(^), gather(&), gather(|) (and trail) over integers fold in a loop, as gather(+) does: the runtime's generic fold called a function value with two boxed numbers for every element. siphash (hashes ▷ gather(^)): reference-count operations from 4 million to 8, 25% faster on one core.
  • [256 -1 255] is three numbers: in a list written out, a - with a space before it and a digit right after is a negative element, not a subtraction (which gave [255 255] with no error). [a - 1], [a-1], an index xs[n -1] and parentheses inside a list still subtract; ¯1 works everywhere as before.
  • A list of strings written out and indexed at once makes only the element picked: ["" "-alpha.{k}" "-beta.{k}"][i], its elements literals, variables or text formatted from them, is a switch on i that formats one string, not the list and every string in it (an index outside it: the list made and indexed as before, the same error). semver: reference-count operations from 41 million to 20 million.
  • A list of lists no longer sends a function back to the old code generator: the Mimic IR holds, passes on and releases it as any other list (its rows read by the old path, as for a list of strings), so the rest of the function is compiled through the IR. vm, whose program is a list of [operation, argument] pairs: its machine loop compiled whole, reference-count operations from 46 million to 51, 4× faster.
  • A loop whose state is a record or short list kept in registers is compiled whole, a short list read and changed at any index there too: xs[i] and put(xs, i, x) on a list of up to 16 numbers kept in registers (a machine's tape in its record) are checked against its length and stay in registers, so a step changing it no longer builds the whole state on the heap to do so. A loop's first state with an empty list in it, (0, []), takes the list's type from the step. brainf: reference-count operations from 42 million to 5 thousand, 3× faster.
  • A tuple of a list and a number (and such: 2 to 4 numbers or values on the heap) is returned as its parts: where the caller takes it apart at once (let (p, root) = find(parent, i)), the function gives its parts in registers, with no tuple made, filled, checked and freed; a tuple written out as the result, in any branch, or handed on from another such call, makes none. A caller that keeps the tuple gets one, as before. Held-out unionfind 6.2 s → 3.2 s, bfsgrid 1.3 s → 0.45 s, textstats 18% faster.
  • char_of(n) of a number takes it as it is: no boxed number made and checked for each character (the same errors for a number that is not a character). The held-out caesar 12% faster.
  • A list of truth values is typed as one when passed or returned: a parameter declared [bool] given [] (or a list of unknown type) is compiled for [bool], the list converted once at the call, and a function declared -> [bool] gives back a typed list, as [i64] and [f64] already did. The held-out bfsgrid read its walls through the generic path at every step: 2.0 s → 1.3 s.
  • join writes its string once: the length is added up first and the parts copied into the new string, instead of a buffer grown (and reallocated) as it goes and then copied again. A word of a few letters joined millions of times: the held-out textstats 24% faster.
  • v[i] = x on a let mut list of numbers is written in place, with nothing counted per step: the number stored directly where the list is held once (else copied first, as before, with the same errors); v[i] read without a reference of its own; and let mut v = xs, changed and then handed on (fill(n - 1, y, v)), moves the list there instead of keeping a reference. The held-out histogram about 15 times as fast.
  • xs ¨ f over a list of lists is one compiled loop (split over the cores when long): each row is lent to f as it is, a lambda that captures or a named function, which returns a number, a truth value, a string or a record (g ¨ (row => row[c]), rows ¨ group_ok). The held-out sudokucheck about twice as fast.
  • xs ¨ f over a list of strings is one compiled loop (split over the cores when long), as it already was over numbers and records: f, a lambda that may capture values (chars(text) ¨ (c => shift(c, k))), gets each string lent and returns its string or number directly, instead of one boxed call per element with its arguments checked each time. markdown, soundex and template 17–29% faster; the held-out caesar from 50× C to 35×.
  • settle and evolve over a record or a list are compiled through the Mimic IR, as they already were over numbers and values kept in registers: the loop holds the state, each step takes it on (changing it in place) and the first state is left as it was when it is still read after the loop. A record built from short lists (S { heap: [x], … }) goes through the IR too. paths' search is in the IR now.
  • concat(s.f, v ⥊ [n]) on a record's list of numbers writes the n copies into the list itself when the record holds it alone: no list of them is made first and joined (qoi's decoder repeating a pixel for a run).
  • if c { [x] } else { [] } taken at every step allocates nothing for the []: a [] typed by the branch next to it is one shared empty list, made once (a change to it copies, as to any shared list), and concat(s.out, flush) with an empty flush leaves the record's list as it is, without a call. A QOI encoder's flush of a run: 46 ms → 29 ms a frame.
  • A small helper returning a tuple, unpacked just before a record update, no longer allocates: let (bytes, op) = chunk(p, prev) followed by { s with out: concat(s.out, bytes), ops: put(s.ops, op, …) } inlines chunk and makes the update in each of its branches, where bytes is a list of known length kept in registers and appended to the record's own list in place; concat(s.out, concat(flush, bytes)) grows s.out instead of building the inner list. qoi on one core: 0.87 s → 0.47 s.
  • A record's list grows in place when the new list is chosen by an if first: let out = if k == 0 { push(s.out, x) } else { concat(s.out, ys) } followed by { s with k: s.k + 2, out: out } gives each branch the update, so a record held once has its list changed in place (as { s with out: push(s.out, x) } already did) instead of copied at every step. An LZ77 decoder's settle over a 512 KiB output: 1.71 s → 0.03 s.
  • push, put and concat of a short list on a record's list of numbers are done inline when the record holds the list alone and it has room: the numbers are stored and the length raised where the code runs, without a call into the runtime (as put on a variable's list already was). A QOI encoder written with them: 0.49 s → 0.35 s on one core.
  • if c { xs } else { [] } is a list of xs's numbers ([i64], [f64] or bytes), not a list of unknown type: the [] takes the other branch's type, as a literal number already did, so code using the result is compiled for its numbers instead of the generic path.
  • Lists of lists are typed by their rows ([[i64]], [[f64]], a matrix built by ⍳ n ¨ (r => ⍳ n ¨ …)): grid[r][c] is a bounds check and a load, where it was two lookups through the runtime with type checks. Conway's Life on a list of rows: 161–338× C before, 5.4× now.
  • Text one character at a time is faster: chars and char_of share one string per ASCII character instead of making a new one each time, and char_code of ASCII text is read in place (of a literal, char_code("a"), it is a constant). A Caesar cipher over 10 MB: 8.8 s to 3.2 s, 1.27 GB to 365 MB.
  • put(xs, i, xs[j]) changes xs in place when it is the list's last use, also as the first argument of another call (insert(put(xs, j + 1, xs[j]), j - 1, v)): the element is read first. It copied the list at every call; a hand-written insertion sort of 20,000 numbers went from 4.4 s and 3 GB to 0.37 s and 2.5 MB.
  • xs ↑ k and xs ↓ k shrink the list in place where it is used for the last time and held nowhere else (a heap's pop, h ↑ (n - 1), copied the heap every time); a read guarded by an if after the take (if n > 1 { rest[0] = h[n - 1] }) is made before it. A heap of a million numbers used over 4 GB; it uses 11.5 MB.
  • A float prints as the shortest text that reads back as the same number ("{x}", print): 15 significant digits where those do, as before, and 16 or 17 where they do not, so parse_float("{x}") is always x. 0.1 + 0.2 now shows as 0.30000000000000004, as in Rust, Go, JavaScript and Python.
  • A list returned in a tuple, or handed back to the function that holds it, is changed in place: let (p, root) = find(…) moves p at its one use, a list passed to the function itself or returned in its result is handed over, and a variable read by another argument of the same call (find(parent, parent[i])) is let go once the arguments are read. A union-find over a million elements used over 4 GB; it now uses 137 MB.
  • A pure ¨ over a short list runs on one core unless its elements are worth sharing out: up to 16,384 elements, a function's first such ¨ is shared out as before while its cost per element is timed, and later ones go to the other cores only when that cost pays for waking them (else they run on one core, timed as they go). A sudoku check of 20,000 grids ran 6× slower on 12 cores than on one (about 4 s, mostly in the kernel); it now takes 0.7 s.
  • push(push(xs, a), b) grows xs in place: the outer push is given the list the inner one returned, held nowhere else, so it no longer copies it. A text of 10 MB built that way in a recursion took over 300 s; it takes 2.65 s.
  • Reads of a list are made before the changes that follow its copy: let mut s = h then s[i] = h[j] s[j] = h[i] (a swap), and f(push(h, v), ≢ h), now change h in place instead of copying it at every step; a heap's 20,000 pushes took 1.75 GB, now 2 MB.
  • A function whose last act is calling itself runs as a loop, however deep, in release and debug builds: what the call no longer needs (its locals, a string built on the way, a tuple's parts, a list copied with let mut v = xs and changed) is released or handed over before the call, so the call becomes a jump. Recursions a million calls deep over lists, strings and records used to overflow the stack and stop with a segmentation fault; 11 of the 12 held-out programs that did now finish (sudokucheck no longer crashes but is slow).
  • A list of any element type grown by push (or changed by put or concat) where it is used for the last time is changed in place, strings, floats, records and maps included: gen(n - 1, y, push(acc, v)) hands the same list down the recursion instead of copying it at every step (before, only lists of i64 were, so building a list of strings this way took time and memory growing with the square of its length). A map threaded through a recursion with put is changed in place too. Found by the held-out set (BENCHMARK.md): most of its programs build their input this way.
  • A parameter declared as a list of numbers (xs: [i64], [f64]) is that list: given a list of unknown type ([], the result of flatten, a list grown from []), the function used to be compiled for the unknown type, on the generic path, and the declaration only checked; now it is compiled for the declared type and the call converts the list (a list already of that type is passed as it is). bigfactorial in the held-out set (a list of digits started as []): 39 s → 0.9 s. Reported by a user too (bytes: [i64] showing as [?]).

Fixed

  • A list read inside a later argument of the call that takes it is not released there: in put(acc, 0, if c { acc[0] } else { +/ acc }) the reads in the value counted as the list's last use, so it was released while put held it, and put wrote into freed memory (a crash, or wrong numbers). A use is now the last only when every other use runs before it. Found by the nightly random programs.
  • A list's length is never read after the list is gone: header loads were marked as reading memory that never changes, so the optimizer could move a ≢ past the release of its list and read the next list made in the same memory (≢ (push(xs, 8) + 7) gave another list's length). They are plain loads now, marked instead as reading headers, which the numbers a loop writes never touch (type-based alias information), so loops still keep them in registers. Found by the nightly random programs.
  • m[i] = v on a short list kept in registers, with an index or value of unknown type, stopped the program ("index: got unit value"): the general path read the list's numbers as a pointer. It now puts the list on the heap for the runtime and back. Found by the nightly random programs.
  • Windows release builds are about a third of their size: the MinGW libraries linked in carry DWARF debug information, and release links kept it (236 KB of a 372 KB hello world; real programs about 600 KB). Release .exes now leave it out (keeping the symbols mimic profile names functions by): hello world 132 KB, qoi 235 KB, http_route 193 KB. Debug builds keep it.
  • ⍬ is a typed empty list: ⍬, ⍬[f64] (and let z: [i64] = []) were lists of unknown element type, so a function using one, or an if with one as a branch, went the generic way. The empty list now takes the declared list of numbers.
  • x ⥊ [n] of a number is a list of that number's type (0 ⥊ [64], prev ⥊ [k + 1]): it was of unknown type, which even let let s: str = 0 ⥊ [3] through the check.
  • A let an earlier pass left unknown takes its value's type: the checker types a function more than once, and a plain let joined each pass's type with the one before, as let mut does. An early pass, before a callee's result was known, left if r > 0 { run_byte(r) } else { [] } a list of unknown type for good.
  • Deep recursion the net cannot take over no longer crashes: a function that calls itself other than last and gives back a list (or a tuple holding one) ran on the 8 MB main-thread stack and crashed past about 300,000 calls. The main thread's stack is now 512 MB, reserved and used only as it grows, on macOS (written into the executable, which is signed again), Windows (the linker's reservation) and Linux (the stack limit raised at start): 1,000,000 calls deep take 34 MB, 5,000,000 take 160 MB.
  • A settle or evolve loop made through the Mimic IR put its exit between the test and the step, and sieve's crossing-out loop ran 8–9% slower on Linux than in 0.4.2; the exit comes after the step again.
  • evolve with its arguments swapped, x ▷ evolve(step, n), is an error (E0401, saying the count comes first) instead of a program that never runs the step; a number, string or list given where settle, evolve, gather, trail or ¨ needs a function is an error too.
  • fs::temp_dir() was /tmp on Windows, which does not exist there; it is the user's temporary directory (TMP, TEMP).
  • k ↑ xs and k ↓ xs on a list of records (the count on the left) stopped the program with "take/drop count must be an integer scalar"; the count may be on either side, as for lists of numbers.
  • _ binds nothing: a value dropped on purpose can go to _ any number of times in a function (the second _ ← … was E0302).
  • A local bound to a field of a borrowed record (let a = f.opt where the record holds lists), handed to an expression the IR embeds, was released twice: memory corrupted, a later unrelated call crashed.
  • A program that crashes (a bad memory access, a stack overflow) now flushes what it printed, names the signal on stderr (mimic: the program crashed: SIGSEGV …), and keeps the signal's exit status; mimic test reports a test that died from a signal as crashed.
  • A value read in a match guard and used again by a later arm could be handed over in the guard and be gone in the later arm (Prop(k, v) if k == name => …, _ => prop(rest, name)).
  • A short list grown past the smallest memory pool (a word built letter by letter) kept a 4 KB block that was never given back when it was freed: 180,000 such words held about 1 GB until the program ended. It now moves to the next pool.
  • mimic fmt (and formatting from an editor or the MCP server) leaves a file that does not parse untouched and reports the parse error; fmt --glyphs had rewritten ∧/ into ∧÷.
  • A primitive's word is refused wherever a name is declared (E0214): a parameter or a typed let named shape (the word of ⍴) was accepted, then every use of it was the primitive, with a misleading parse error (expected an expression, found '=='). Functions, parameters, lets and lambda parameters now all give E0214 naming the primitive. Reported by a user.
  • A function named like a built-in primitive is refused (E0214): fn matmul(…) compiled, but every matmul(…) elsewhere is the primitive ⊠, so the function could never be called and nothing said so. It is now an error where the function is declared, naming the primitive. (C functions keep such names under c::.)
  • Stopping mimic run stops the program: on macOS and Linux the program now takes the place of mimic run instead of running as its child, so kill, a time limit or a closed terminal ends the program itself (before, only mimic stopped and the program kept running, holding its memory), and the shell sees the program's own exit status.

Install

Download the file for your system below and unpack it. Then run the install script inside: it copies mimic to ~/.mimic/bin, puts that folder on your PATH, and checks what your system needs.

System File Install Also needed
Windows x86_64 (10, 11) mimic-0.4.3-windows-x86_64.zip Double-click install.cmd Nothing: Windows programs are linked with the MinGW-w64 inside mimic.
macOS Apple silicon mimic-0.4.3-macos-arm64.tar.gz ./install.sh The Xcode Command Line Tools (xcode-select --install), for the macOS SDK.
Linux x86_64 mimic-0.4.3-linux-x86_64.tar.gz ./install.sh glibc 2.28 or newer (Ubuntu 20.04, Debian 10, RHEL 8 or later) and a C compiler (cc) to link native programs. --target x86_64-linux needs nothing else.

On Windows, if double-clicking install.cmd does nothing (some company PCs block scripts), open PowerShell in the unpacked folder and run:

powershell -NoProfile -ExecutionPolicy Bypass -File ".\install.ps1"

The script says if something is missing and how to install it. Open a new terminal afterwards so the new PATH applies.

Upgrading from an earlier Mimic: run the new install script the same way. It replaces the old mimic, clears the build cache the old version left in ~/.mimic/cache (it is rebuilt on first use), and keeps your projects and installed libraries. If another mimic comes first on your PATH, it says where, and offers to remove it.

  • macOS: the script clears the download quarantine. If you copy mimic by hand instead, run xattr -d com.apple.quarantine mimic once.
  • Windows: the script unblocks the downloaded files. SmartScreen may still ask the first time: "More info" → "Run anyway".
  • Uninstall: ./install.sh --uninstall, or install.cmd -Uninstall on Windows. Your projects are not touched.

First program

mimic create hello
cd hello
mimic run

mimic --help lists every command. SHA256SUMS has the checksum of each file.