Menai continues to get faster and more capable!
Changing the nature of complex number support
complex numbers have been a part of Menai since the start. I'm an Electrical Engineer, so I sort-of know they're essential. With that said, however, it's been bugging me for a while that they have held a special place in the VM as they added quite a lot of complexity and there wasn't a full set of operations available.
With the introduction of struct several months ago, it seemed like these could be handled better in stdlib. Yes, the VM versions would be a little faster, but there are exactly zero uses of complex to this point so they really didn't justify their existence.
JPEG encode/decode
Added JPEG encode and decode libraries to give a more comprehensive set of image codecs. There are still a few image codecs to add, but the set is getting more comprehensive.
What was most interesting about these was to set DeepSeek off optimizing the implementations. The initial versions were quite elegant, but the new versions are more than 2x faster.
Compiler tweaks
It was apparent that we could improve the inlining support associated with higher-order prelude functions, which in turn generated code that showed a missed loop optimization.
bytes opcodes
The bytes opcodes used to use read and write as operation names, but we're a pure functional language and these sort of don't make sense. Renamed the operations to nth and with, in keeping with all other container objects.
Menai v0.13
Breaking changes:
- Complex numbers are no longer a language type. There is no complex value, no dedicated arithmetic and no
3+4jliteral — a trailingjnow raises a compile-time error that points at the standard library as the place to construct one. The numeric types areintegerandfloat:number->stringandstring->numberhandle those two only, andstring->numberreturns#nonefor"1+2j". See ADR-0074.
New features:
jpeg-encodewrites a pixel dict as a baseline sequential JPEG file, sojpeg-decodedecodes baseline sequential JPEG files to the pixel-dict shape the other image codecs use.jpeg-decodeandjpeg-encodeform a codec pair. JPEG is lossy, so the round-trip suite asserts a tolerance rather than equality.bmp-decodewalks each row by byte offset and takes the bit depth out of the pixel loop; 20% faster, 28% fewer instructions.- Reintroduce some Python reference examples in the benchmark suite.
- A callback passed to a higher-order prelude function is inlined into that function's loop, whether it is written as a literal argument or bound first. A loop parameter the loop cannot rebind now resolves to the lambda it was seeded with, so
(fold-list integer+ 0 lst)and(map-list (lambda (x) ...) lst)no longer pay an indirect call, a frame and an argument move per element. - An accessor applied to a container literal folds, so a call to a variadic prelude function no longer allocates its rest list and its dispatch collapses to the arm the call's arity selects.
(list-length (list a b))folds too, which is the shape every hand-rolled variadic builtin emits. - The loop parameter and closure that inlining makes dead are dropped, so inlining a callback no longer leaves an uncalled function in the module.
- Added an
--annotatecommand line argument tomenai-test. - Renamed the
byteselement-access operations to the-nth/-withverbs, sobytesmatches every other type.bytes-read-{width}-{endian}reads a field and becomesbytes-nth-{width}-{endian}.bytes-write-{width}-{endian}replaces a field and becomesbytes-with-{width}-{endian}.bytes-read-u8is removed, sincebytes-nthalready reads a single byte. - Added
string-withandlist-with, so that every container with a positional element read has a matching element update:(string-with s i ch)replaces the codepoint at an index and(list-with lst i x)replaces the element. complexis a standard library module: complex arithmetic over a struct of two floats, with the"3+4j"text form into-stringandfrom-string. It replaces the language type removed above, so a caller imports the capability and constructs values by calling the type; see ADR-0074.
Internal structure changes:
- Added
jpeg-decodeandjpeg-encodebenchmark suites. - The 58 opcodes taking an index, offset or slice bound reject a bignum outright instead of decoding it first; a bignum operand now reports a range error.
- Benchmark suite names are hyphenated throughout. Eight suites used an underscore, so
menai-benchmark --suite sort-listnow selects what was--suite sort_list.
Bug fixes:
- A call whose function position was a loop parameter could be inlined as an outer binding of the same name, ignoring the value the parameter held, so a loop that rebound its function argument computed the wrong answer.
- A nested loop whose initial value is the enclosing loop's parameter — the shape inlining an inner loop into an outer loop's body leaves behind — could lose the definition of the enclosing loop's variable, so modules that inlined such a function failed bytecode validation instead of running.
menai-formatdropped a binding's value and its closing parenthesis when a comment sat between the binding's name and its value.- A program whose code object tree could not be converted to native code objects, because a host-supplied value was of a type the bridge cannot convert, crashed the interpreter instead of raising the conversion error.
- A float constant's identity was its value, so
0.0and-0.0shared one constant pool entry and one literal silently took the other's sign. Floats are now keyed by bit pattern in the constant pool, in VCode constant coalescing, and in CSE, so a signed zero keeps its sign whatever else the program contains.