2026-11-25: Continuing Menai Performance Work

Published: 2026-11-25

There are always options to make compiled code faster, so in the last few days I've taken a few.

Improved algorithms

The Sudoku solver was quite naiive, so now there's a new implementation that's much faster. To make things more directly relevant I've also started to reintroduce some of the Python reference benchmarks.

The Python versions are intended to use the same algorithmic approach but can use any idiomatic approach to the implementation. The only thing they're not doing is using hand-tuned C implementations so we keep the Menai and Python VM comparisons meaningful (Menai can use hand-tuned C too but that's not the point).

The bmp-decode library was also not as good as it should have been, so has now been improved algorithmically.

VM improvements

By only having 61-bit fixnum implementations and ignoring the possibility of having 64-bit fixed integers the code in the VM could be simplified and sped up. This isn't visible to the Menai language surface or the compiler and is purely a VM tradeoff.

Compiler improvements

A few small improvements in the compiler became apparent during analysis and this opened up an opportunity to slightly increase the inlining thresholds (from 50 to 60 IR nodes). This offered a very nice performance improvement.

Performance

Comparing the 192 benchmark cases that appear in both this run and the 2026-11-23 run, the mean improvement is 9.2% and the median is 3.2%. 161 cases got faster, 13 were unchanged and 18 measured slower. The gains are heavily skewed: the interquartile range runs from 1.1% to 11.2%, and the best case (the list-mean-n=500 stats benchmark) is 45.8% faster.

The 18 apparent regressions are all measurement noise. The largest, multiline-1000 in csv-decode at 31.2% and runs-32k in deflate-compress at 17.0%, are both cases whose timings move by that much between runs without any code change, and the remaining 16 are all under 4%. So the changes in this note did not make anything slower.

The stats benchmarks dominate the top of the table, with the mean cases improving by 30–46%. Outside stats, the clearest win is bmp-decode, where all five cases improve by 20–24%, and the bmp-encode cases move by less than 4% either way.

BASE64-DECODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
standard-1k               0.109         0.100
standard-64k              7.414         7.300
url-1k                    0.108         0.107
url-64k                   7.410         7.322
──────────────────────────────────────────────────────


BASE64-ENCODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
standard-1k               0.051         0.050
standard-64k              3.086         2.967
unpadded-1k               0.051         0.050
url-64k                   3.119         2.972
──────────────────────────────────────────────────────


BMP-DECODE
────────────────────────────────────────────────────────
Case                        mean (ms)     min (ms)
────────────────────────────────────────────────────────
truecolour-64x64            0.213         0.194
truecolour-128x128          0.864         0.845
truecolour-topdown-128x128  0.843         0.839
truecolour-alpha-128x128    0.989         0.982
padded-65x64                0.200         0.199
────────────────────────────────────────────────────────


BMP-ENCODE
────────────────────────────────────────────────────────
Case                        mean (ms)     min (ms)
────────────────────────────────────────────────────────
truecolour-64x64            0.294         0.289
truecolour-128x128          1.203         1.188
truecolour-1024x16          1.104         1.085
truecolour-16x1024          2.033         2.005
truecolour-256x256          5.162         5.121
truecolour-topdown-128x128  1.207         1.195
truecolour-alpha-128x128    1.478         1.467
padded-65x64                0.300         0.292
────────────────────────────────────────────────────────


CALENDAR
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
short_5d                  0.022         0.017
month_20d                 0.069         0.066
quarter_60d               0.198         0.196
year_250d                 0.819         0.811
months_600d               1.433         1.431
──────────────────────────────────────────────────────


CSV-DECODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
plain-1000x10             1.879         1.842
quoted-1000x10            2.075         2.065
escaped-1000x10           4.281         4.213
multiline-1000            0.623         0.484
wide-100x100              1.698         1.673
──────────────────────────────────────────────────────


CSV-ENCODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
plain-1000x10             2.353         2.194
quoted-1000x10            2.630         2.460
escaped-1000x10           2.637         2.627
wide-100x100              1.494         1.484
──────────────────────────────────────────────────────


DEFLATE-COMPRESS
────────────────────────────────────────────────────────────────────────────────────────────
Case                          mean menai    min menai   mean python   min python  ref/menai
────────────────────────────────────────────────────────────────────────────────────────────
text-4k                       0.559         0.540       0.586         0.566       1.05x
text-32k                      2.265         2.248       2.514         2.474       1.10x
incremental-16k               2.181         2.118       3.620         3.593       1.70x
runs-32k                      7.210         6.170       8.366         8.093       1.31x
────────────────────────────────────────────────────────────────────────────────────────────


DEFLATE-DECOMPRESS
────────────────────────────────────────────────────────────────────────────────────────────
Case                          mean menai    min menai   mean python   min python  ref/menai
────────────────────────────────────────────────────────────────────────────────────────────
text-4k                       0.142         0.133       0.116         0.105       0.79x
text-32k                      0.306         0.297       0.291         0.283       0.95x
incremental-16k               0.370         0.363       0.486         0.478       1.32x
stored-8k                     0.050         0.045       0.004         0.002       0.04x
runs-32k                      1.242         1.207       1.625         1.604       1.33x
────────────────────────────────────────────────────────────────────────────────────────────


DOCX-DECODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
minimal                   0.282         0.266
document-100              1.078         1.046
document-500              4.589         4.481
document-2000             18.317        18.203
styles-100                1.776         1.701
numbering-9               0.971         0.926
media-16                  3.299         3.256
full-500                  7.713         7.583
──────────────────────────────────────────────────────


DOCX-ENCODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
minimal                   0.033         0.025
document-100              0.128         0.119
document-500              0.514         0.499
styles-100                0.163         0.156
numbering-9               0.058         0.052
media-16                  0.118         0.109
full-500                  0.631         0.615
──────────────────────────────────────────────────────


HTML-DECODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
wide-1000                 1.847         1.814
deep-200                  0.174         0.170
attributes-500            1.726         1.710
mixed-500                 1.736         1.721
entities-2000             5.012         4.987
void-1000                 4.685         4.664
omitted-end-tags-1000     1.031         1.012
raw-text-500              1.387         1.371
realistic-500             3.350         3.303
──────────────────────────────────────────────────────


HTML-ENCODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
wide-1000                 1.005         0.946
deep-200                  0.137         0.110
attributes-500            0.582         0.569
mixed-500                 0.829         0.823
void-1000                 2.150         2.134
escaping-2000             2.517         2.483
declarations-1000         0.493         0.485
──────────────────────────────────────────────────────


JSON-DECODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
object                    0.012         0.010
flat_array                0.073         0.072
flat_object               0.063         0.061
mixed_nested              0.141         0.138
string_heavy              0.050         0.049
numbers_array             0.016         0.015
unicode_strings           0.007         0.007
long_string               0.060         0.059
deep_array                0.128         0.127
──────────────────────────────────────────────────────


JSON-ENCODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
object                    0.006         0.004
flat_array                0.012         0.011
flat_object               0.017         0.016
mixed_nested              0.035         0.034
string_heavy              0.040         0.039
numbers_array             0.007         0.005
unicode_strings           0.002         0.002
long_string               0.049         0.048
──────────────────────────────────────────────────────


PNG-DECODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
greyscale-64x64           1.295         1.268
greyscale-alpha-64x64     6.019         6.007
palette-128x128           3.958         3.935
truecolour-128x128        34.565        34.417
truecolour-alpha-128x128  46.268        46.062
truecolour-192x192        79.887        79.726
──────────────────────────────────────────────────────


PNG-ENCODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
truecolour-48x48          7.513         7.496
truecolour-96x96          40.617        40.315
truecolour-alpha-96x96    78.641        77.023
──────────────────────────────────────────────────────


PPTX-DECODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
minimal                   0.635         0.616
slides-10                 2.706         2.684
slides-50                 11.873        11.848
slides-200                46.302        46.073
shapes-50                 7.041         6.990
masters-8                 5.930         5.897
media-16                  5.964         5.939
theme-and-masters         4.460         4.427
full-50                   55.869        55.681
──────────────────────────────────────────────────────


PPTX-ENCODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
minimal                   0.040         0.032
slides-10                 0.099         0.089
slides-50                 0.361         0.349
shapes-50                 0.708         0.685
masters-8                 0.165         0.156
media-16                  0.197         0.188
theme-and-masters         0.135         0.126
full-50                   3.467         3.402
──────────────────────────────────────────────────────


REGEXP
───────────────────────────────────────────────────────
Case                       mean (ms)     min (ms)
───────────────────────────────────────────────────────
compile-per-line           0.062         0.058
search-prefix-log          0.032         0.030
search-class-numbers       0.034         0.032
search-alternation         0.054         0.051
search-all-class           4.224         4.204
search-all-prefix          0.124         0.121
search-groups-log          0.054         0.051
search-all-groups-numbers  32.398        32.164
split-whitespace           5.379         5.347
replace-whitespace         64.676        61.210
replace-groups-dates       15.955        15.853
chained-per-line           0.043         0.041
───────────────────────────────────────────────────────


RUBIKS-LIST
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
1-move                    0.056         0.051
2-move                    0.054         0.050
3-move                    0.121         0.116
4-move                    1.239         1.232
5-move                    4.148         4.141
6-move                    27.121        27.089
7-move                    59.393        59.324
7-move with B             7.604         7.589
──────────────────────────────────────────────────────


RUBIKS-VECTOR
────────────────────────────────────────────────────────────────────────────────────────────
Case                          mean menai    min menai   mean python   min python  ref/menai
────────────────────────────────────────────────────────────────────────────────────────────
1-move                        0.044         0.040       0.052         0.050       1.25x
2-move                        0.045         0.041       0.051         0.050       1.21x
3-move                        0.096         0.091       0.119         0.118       1.29x
4-move                        0.992         0.986       1.264         1.259       1.28x
5-move                        3.344         3.334       4.378         4.368       1.31x
6-move                        21.466        21.443      28.007        27.927      1.30x
7-move                        47.765        47.740      62.926        62.845      1.32x
7-move with B                 6.100         6.087       8.067         8.025       1.32x
────────────────────────────────────────────────────────────────────────────────────────────


SORT-LIST
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
n=10                      0.004         0.003
n=50                      0.023         0.022
n=100                     0.054         0.053
n=250                     0.150         0.148
n=500                     0.338         0.335
n=1000                    0.753         0.746
n=2500                    2.208         2.199
n=5000                    4.812         4.785
n=10000                   10.272        10.213
──────────────────────────────────────────────────────


SORT-VECTOR
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
n=10                      0.003         0.003
n=50                      0.023         0.022
n=100                     0.052         0.051
n=250                     0.148         0.147
n=500                     0.332         0.330
n=1000                    0.762         0.751
n=2500                    2.177         2.169
n=5000                    4.717         4.668
n=10000                   10.064        10.047
──────────────────────────────────────────────────────


STATS
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
vector-mean-n=100         0.006         0.005
vector-mean-n=500         0.023         0.022
vector-mean-n=1000        0.046         0.046
vector-mean-n=2500        0.114         0.113
vector-mean-n=5000        0.227         0.224
vector-mean-n=10000       0.447         0.444
vector-mean-n=25000       1.122         1.107
vector-summary-n=100      0.046         0.044
vector-summary-n=500      0.266         0.264
vector-summary-n=1000     0.573         0.572
vector-summary-n=2500     1.633         1.622
vector-summary-n=5000     3.488         3.464
vector-summary-n=10000    7.479         7.450
vector-summary-n=25000    19.841        19.765
list-mean-n=100           0.007         0.006
list-mean-n=500           0.026         0.024
list-mean-n=1000          0.052         0.050
list-mean-n=2500          0.147         0.145
list-mean-n=5000          0.334         0.332
list-mean-n=10000         0.683         0.681
list-mean-n=25000         1.802         1.777
list-summary-n=100        0.047         0.045
list-summary-n=500        0.268         0.266
list-summary-n=1000       0.578         0.576
list-summary-n=2500       1.673         1.652
list-summary-n=5000       3.630         3.588
list-summary-n=10000      7.637         7.607
list-summary-n=25000      20.459        20.371
──────────────────────────────────────────────────────


STRING-OPS
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
search long/absent        0.016         0.015
search long/present       0.020         0.020
search short needle       0.020         0.019
search needle=haystack    0.148         0.146
hash 2000x8               0.035         0.033
hash 2000x64              0.095         0.093
hash 500x512              0.198         0.197
──────────────────────────────────────────────────────


SUDOKU-LIST
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
Easy (36 givens)          9.021         8.936
Medium (30 givens)        0.130         0.122
Hard (25 givens)          1846.256      1846.256
Expert (23 givens)        104.714       104.714
──────────────────────────────────────────────────────


SUDOKU-MRV
────────────────────────────────────────────────────────────────────────────────────────────
Case                          mean menai    min menai   mean python   min python  ref/menai
────────────────────────────────────────────────────────────────────────────────────────────
Easy (36 givens)              0.114         0.103       0.070         0.067       0.65x
Medium (30 givens)            0.070         0.067       0.028         0.028       0.42x
Hard (25 givens)              9.871         9.871       9.930         9.930       1.01x
Expert (23 givens)            24.201        24.201      25.718        25.718      1.06x
────────────────────────────────────────────────────────────────────────────────────────────


SUDOKU-VECTOR
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
Easy (36 givens)          7.130         7.096
Medium (30 givens)        0.099         0.095
Hard (25 givens)          1452.323      1452.323
Expert (23 givens)        81.902        81.902
──────────────────────────────────────────────────────


XML-DECODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
wide-1000                 1.043         1.022
deep-200                  0.132         0.129
attributes-500            0.761         0.750
mixed-500                 1.330         1.316
entities-2000             3.265         3.239
ooxml-500                 2.560         2.532
──────────────────────────────────────────────────────


XML-ENCODE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
wide-1000                 0.518         0.503
deep-200                  0.105         0.092
attributes-500            0.554         0.546
mixed-500                 0.828         0.821
escaping-2000             2.532         2.522
──────────────────────────────────────────────────────


ZIP-CREATE
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
single-deflate            0.386         0.371
single-stored             0.020         0.015
mixed-16                  2.770         2.744
many-128                  39.996        39.946
large-256k                16.327        16.018
──────────────────────────────────────────────────────


ZIP-ENTRIES
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
single-deflate            0.003         0.001
single-stored             0.002         0.001
mixed-16                  0.010         0.009
many-128                  0.076         0.067
large-256k                0.002         0.001
──────────────────────────────────────────────────────


ZIP-EXTRACT
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
single-deflate            0.093         0.085
single-stored             0.048         0.045
mixed-16                  0.364         0.353
many-128                  4.322         4.295
large-256k                1.996         1.973
──────────────────────────────────────────────────────


ZLIB-COMPRESS
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
text-4k                   0.679         0.665
text-32k                  3.165         3.144
incremental-16k           2.600         2.559
runs-32k                  7.057         6.999
──────────────────────────────────────────────────────


ZLIB-DECOMPRESS
──────────────────────────────────────────────────────
Case                      mean (ms)     min (ms)
──────────────────────────────────────────────────────
text-4k                   0.254         0.243
text-32k                  1.198         1.184
incremental-16k           0.815         0.808
runs-32k                  2.109         2.083
──────────────────────────────────────────────────────