Repository navigation
Mimic 0.4.3-preview.1
Pre-releaseA preview of the next version: not a stable release. Install it with mimic upgrade --preview.
Step 0.4.3, loops and values in registers, so far (0.4.3-preview.1): 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 each night; and the held-out set (40 programs written plainly, never used for tuning) traced and made faster without a change to them: Mimic's time over C's, as a geometric mean, from 4.65× to 3.67× on Linux, 4.18× to 3.24× on macOS and 4.33× to 3.44× on Windows. A tuple of a list and a number is returned as its parts, ¨ over lists of strings and lists of lists is one compiled loop, v[i] = x changes a let mut list in place, join writes its string once, lists of truth values keep their type, and settle/evolve over a record or a list go through the Mimic IR: histogram 15×, bfsgrid 4.4×, unionfind 2×, sudokucheck 2× faster; markdown, soundex and template 17–29%. qoi's encoder and decoder steps no longer allocate.
Added
- 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 lldbstops right there, with the stack. Release builds are unchanged. - Random programs, nightly (
tools/fuzz.py, thefuzzworkflow): 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(andto_bits32/from_bits32forf32),bytes::of_f64sandbytes::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 (allandanywritten as glyphs, as in APL):same ▷ ∧/,∨/ flags.math::tanh,math::sinh,math::cosh,math::log1p,math::expm1andmath::sigmoid(libm calls that fuse into array kernels likemath::exp;log1pandexpm1exact near zero;sigmoidwithout overflow for large negative inputs), andunwrap_orfor anOption:parse_int(s) ▷ unwrap_or(300). Reported by a user.
Changed
- 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-outunionfind6.2 s → 3.2 s,bfsgrid1.3 s → 0.45 s,textstats18% 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-outcaesar12% 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-outbfsgridread its walls through the generic path at every step: 2.0 s → 1.3 s. joinwrites 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-outtextstats24% faster.v[i] = xon alet mutlist 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; andlet mut v = xs, changed and then handed on (fill(n - 1, y, v)), moves the list there instead of keeping a reference. The held-outhistogramabout 15 times as fast.xs ¨ fover a list of lists is one compiled loop (split over the cores when long): each row is lent tofas 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-outsudokucheckabout twice as fast.xs ¨ fover 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,soundexandtemplate17–29% faster; the held-outcaesarfrom 50× C to 35×.settleandevolveover 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 thencopies 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), andconcat(s.out, flush)with an emptyflushleaves 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, …) }inlineschunkand makes the update in each of its branches, wherebytesis a list of known length kept in registers and appended to the record's own list in place;concat(s.out, concat(flush, bytes))growss.outinstead of building the inner list.qoion one core: 0.87 s → 0.47 s. - A record's list grows in place when the new list is chosen by an
iffirst: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'ssettleover a 512 KiB output: 1.71 s → 0.03 s. push,putandconcatof 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 (asputon 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 ofxs'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:
charsandchar_ofshare one string per ASCII character instead of making a new one each time, andchar_codeof 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])changesxsin 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 ↑ kandxs ↓ kshrink 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 anifafter 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, soparse_float("{x}")is alwaysx.0.1 + 0.2now shows as0.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(…)movespat 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)growsxsin place: the outerpushis 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 = hthens[i] = h[j]s[j] = h[i](a swap), andf(push(h, v), ≢ h), now changehin 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 = xsand 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 (sudokucheckno longer crashes but is slow). - A list of any element type grown by
push(or changed byputorconcat) 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 ofi64were, 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 withputis 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 offlatten, 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).bigfactorialin 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
settleorevolveloop made through the Mimic IR put its exit between the test and the step, andsieve's crossing-out loop ran 8–9% slower on Linux than in 0.4.2; the exit comes after the step again. evolvewith 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 wheresettle,evolve,gather,trailor¨needs a function is an error too.fs::temp_dir()was/tmpon Windows, which does not exist there; it is the user's temporary directory (TMP,TEMP).k ↑ xsandk ↓ xson 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.optwhere 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 testreports a test that died from a signal as crashed. - A value read in a
matchguard 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 --glyphshad rewritten∧/into∧÷.- A primitive's word is refused wherever a name is declared (E0214): a parameter or a typed
letnamedshape(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 everymatmul(…)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 underc::.) - Stopping
mimic runstops the program: on macOS and Linux the program now takes the place ofmimic runinstead of running as its child, sokill, a time limit or a closed terminal ends the program itself (before, onlymimicstopped 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-preview.1-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-preview.1-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-preview.1-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
mimicby hand instead, runxattr -d com.apple.quarantine mimiconce. - Windows: the script unblocks the downloaded files. SmartScreen may still ask the first time: "More info" → "Run anyway".
- Uninstall:
./install.sh --uninstall, orinstall.cmd -Uninstallon Windows. Your projects are not touched.
First program
mimic create hello
cd hello
mimic runmimic --help lists every command. SHA256SUMS has the checksum of each file.