Menai v0.12 takes advantage of the VM single-use mutation strategy I noted a few days ago. Added to that is a new VM opcode for fast scanning of characters in strings and the ability to slice strings much more cheaply.
This unlocked a lot of potential to make algorithmic improvements across the stdlib and benchmark test cases.
Menai v0.12
New features:
- The VM now implements in-place modification of containers (vectors, sets, dicts, structs and bytes) where a source goes dead during the operation.
- Appending to a
bytesvalue now updates it in place, carrying spare capacity and growing geometrically, so building a byte sequence in steps is linear. - Appending to a
stringvalue now updates it in place on the same conditions, so building a string in steps is linear. string-slice,string-trim,string-trim-leftandstring-trim-rightnow return a view into their source rather than copying the codepoints, so trimming no longer moves the text it keeps and slicing a document into tokens no longer copies each token.map-vector,map-setandfilter-setnow build their result in place rather than converting it through a list.sort-vectoris a native bottom-up merge sort, andsort-listsorts through it.- The DEFLATE LZ77 candidate table is now one dictionary updated in place rather than sixteen shards, which existed only because every update copied. 30–50% faster compression on run-heavy inputs.
bmp-encodeappends to its output in the pixel loop rather than through a helper, which had made it quadratic in the image size.png-encodenow builds each scanline in place.csv-encodenow decides quoting and doubles quotes with native scans and replacement; 87% fewer instructions on the quoted-table benchmark.xml-encodeandhtml-encodeescape with native replacement behind a native presence check, so text with nothing to escape costs no allocation.csv-decodenow finds fields with native searches and takes them as slices of the input; up to 59% fewer instructions.html-decodenow finds markup and character references with native searches and takes text runs and attribute values as slices; 13–46% faster.xml-decodenow finds markup and entities natively and takes text runs and attribute values as slices; 37–43% faster, which the OOXML decoders inherit.json-encodeexamines a string's body as bytes and copies it whole when it holds nothing to escape; the long-string benchmark is 72% faster.base64-encodeandbase64-decoderead the alphabet and the input as bytes and look values up in a table built once, so neither costs a string per character.- Loop rotation now also rotates a loop whose test loops out of both arms, removing a jump per merged element in the sort benchmarks.
menai-formatnow formats files in place by default and accepts several files at once;-o -writes the result to stdout and-still reads stdin. The--in-placeoption is gone because it is now the default.- The Sudoku benchmarks were improved to avoid doing unnecessary work, and thus making them more interesting as comparisons with other languages.
string-index-anyis a new builtin: it returns the index of the first character in a set of inclusive codepoint ranges, or#none, so finding any of several terminators is one native scan rather than one scan per character or one test per position.regexpcompiles a pattern's first-character set and jumps to it withstring-index-any, including the union of an alternation's branches and the complement of a negated class. Class-first patterns execute 44–81% fewer instructions; replacing\s+is 11% faster because allocation in the matcher now dominates it.csv-decode,html-decodeandxml-decodefind the end of a field, a name and an unquoted attribute value with that one set search, replacing three searches or a per-character loop.
Bug fixes:
- Loop rotation now sees a terminator's operands when deciding whether the loop body consumes a header-test result.
- An
errorused as an operand rather than in tail position failed to compile with a slot-allocation error, and a later operand could raise in its place. html-decoderaised a slice error on a quoted attribute value that is never closed.
Performance
BASE64-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
standard-1k 0.124 0.115
standard-64k 8.489 8.379
url-1k 0.120 0.116
url-64k 8.494 8.439
──────────────────────────────────────────────────────
BASE64-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
standard-1k 0.052 0.051
standard-64k 3.167 3.020
unpadded-1k 0.052 0.052
url-64k 3.190 3.090
──────────────────────────────────────────────────────
BMP-DECODE
────────────────────────────────────────────────────────
Case mean (ms) min (ms)
────────────────────────────────────────────────────────
truecolour-64x64 0.267 0.238
truecolour-128x128 1.131 1.082
truecolour-topdown-128x128 1.095 1.078
truecolour-alpha-128x128 1.238 1.208
padded-65x64 0.263 0.257
────────────────────────────────────────────────────────
BMP-ENCODE
────────────────────────────────────────────────────────
Case mean (ms) min (ms)
────────────────────────────────────────────────────────
truecolour-64x64 0.284 0.264
truecolour-128x128 1.194 1.157
truecolour-1024x16 1.091 1.084
truecolour-16x1024 2.084 2.066
truecolour-256x256 5.101 5.046
truecolour-topdown-128x128 1.210 1.198
truecolour-alpha-128x128 1.455 1.446
padded-65x64 0.291 0.289
────────────────────────────────────────────────────────
CALENDAR
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
short_5d 0.023 0.018
month_20d 0.072 0.070
quarter_60d 0.206 0.200
year_250d 0.840 0.838
months_600d 1.496 1.485
──────────────────────────────────────────────────────
CSV-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
plain-1000x10 1.888 1.851
quoted-1000x10 2.114 2.104
escaped-1000x10 4.356 4.321
multiline-1000 0.475 0.474
wide-100x100 1.706 1.698
──────────────────────────────────────────────────────
CSV-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
plain-1000x10 2.675 2.603
quoted-1000x10 2.962 2.939
escaped-1000x10 2.951 2.919
wide-100x100 1.565 1.545
──────────────────────────────────────────────────────
DEFLATE-COMPRESS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
text-4k 0.601 0.591
text-32k 2.311 2.293
incremental-16k 2.200 2.145
runs-32k 6.161 6.093
──────────────────────────────────────────────────────
DEFLATE-DECOMPRESS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
text-4k 0.146 0.135
text-32k 0.315 0.307
incremental-16k 0.379 0.369
stored-8k 0.052 0.049
runs-32k 1.236 1.198
──────────────────────────────────────────────────────
DOCX-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
minimal 0.288 0.272
document-100 1.098 1.067
document-500 4.655 4.581
document-2000 18.434 18.300
styles-100 1.802 1.702
numbering-9 0.983 0.943
media-16 3.365 3.336
full-500 7.784 7.707
──────────────────────────────────────────────────────
DOCX-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
minimal 0.035 0.028
document-100 0.134 0.127
document-500 0.545 0.530
styles-100 0.175 0.169
numbering-9 0.064 0.059
media-16 0.125 0.118
full-500 0.662 0.647
──────────────────────────────────────────────────────
HTML-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
wide-1000 1.912 1.888
deep-200 0.179 0.175
attributes-500 1.765 1.748
mixed-500 1.791 1.770
entities-2000 5.209 5.180
void-1000 4.817 4.793
omitted-end-tags-1000 1.039 1.025
raw-text-500 1.419 1.399
realistic-500 3.466 3.428
──────────────────────────────────────────────────────
HTML-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
wide-1000 1.052 1.012
deep-200 0.216 0.157
attributes-500 0.643 0.636
mixed-500 0.900 0.891
void-1000 2.440 2.399
escaping-2000 2.699 2.673
declarations-1000 0.534 0.527
──────────────────────────────────────────────────────
JSON-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
object 0.012 0.010
flat_array 0.074 0.072
flat_object 0.063 0.061
mixed_nested 0.139 0.138
string_heavy 0.049 0.048
numbers_array 0.016 0.015
unicode_strings 0.007 0.006
long_string 0.060 0.059
deep_array 0.127 0.125
──────────────────────────────────────────────────────
JSON-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
object 0.007 0.005
flat_array 0.013 0.012
flat_object 0.019 0.017
mixed_nested 0.037 0.036
string_heavy 0.041 0.040
numbers_array 0.007 0.005
unicode_strings 0.002 0.002
long_string 0.052 0.050
──────────────────────────────────────────────────────
PNG-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
greyscale-64x64 1.433 1.410
greyscale-alpha-64x64 6.192 6.184
palette-128x128 4.671 4.618
truecolour-128x128 35.432 35.325
truecolour-alpha-128x128 47.439 47.292
truecolour-192x192 82.069 81.869
──────────────────────────────────────────────────────
PNG-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
truecolour-48x48 7.476 7.439
truecolour-96x96 41.729 41.705
truecolour-alpha-96x96 79.258 78.629
──────────────────────────────────────────────────────
PPTX-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
minimal 0.640 0.618
slides-10 2.737 2.715
slides-50 11.820 11.796
slides-200 46.081 45.956
shapes-50 7.173 7.106
masters-8 5.877 5.838
media-16 6.023 5.993
theme-and-masters 4.561 4.508
full-50 56.435 56.252
──────────────────────────────────────────────────────
PPTX-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
minimal 0.044 0.035
slides-10 0.107 0.099
slides-50 0.383 0.372
shapes-50 0.739 0.713
masters-8 0.176 0.169
media-16 0.209 0.200
theme-and-masters 0.147 0.139
full-50 3.543 3.469
──────────────────────────────────────────────────────
REGEXP
───────────────────────────────────────────────────────
Case mean (ms) min (ms)
───────────────────────────────────────────────────────
compile-per-line 0.062 0.059
search-prefix-log 0.032 0.030
search-class-numbers 0.034 0.032
search-alternation 0.053 0.051
search-all-class 4.242 4.207
search-all-prefix 0.126 0.123
search-groups-log 0.054 0.051
search-all-groups-numbers 32.769 32.666
split-whitespace 5.442 5.369
replace-whitespace 65.599 62.463
replace-groups-dates 16.126 16.066
chained-per-line 0.043 0.042
───────────────────────────────────────────────────────
RUBIKS_LIST
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
1-move 0.057 0.051
2-move 0.056 0.052
3-move 0.120 0.115
4-move 1.255 1.246
5-move 4.225 4.217
6-move 27.230 27.151
7-move 60.515 60.383
──────────────────────────────────────────────────────
RUBIKS_VECTOR
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
1-move 0.045 0.041
2-move 0.048 0.042
3-move 0.100 0.094
4-move 1.016 1.012
5-move 3.414 3.408
6-move 21.974 21.942
7-move 48.940 48.877
──────────────────────────────────────────────────────
SORT_LIST
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
n=10 0.005 0.003
n=50 0.024 0.023
n=100 0.055 0.054
n=250 0.156 0.153
n=500 0.345 0.338
n=1000 0.771 0.766
n=2500 2.213 2.202
n=5000 4.804 4.786
n=10000 10.384 10.325
──────────────────────────────────────────────────────
SORT_VECTOR
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
n=10 0.004 0.003
n=50 0.023 0.022
n=100 0.053 0.052
n=250 0.152 0.151
n=500 0.339 0.338
n=1000 0.753 0.750
n=2500 2.202 2.194
n=5000 4.807 4.769
n=10000 10.277 10.245
──────────────────────────────────────────────────────
STATS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
vector-mean-n=100 0.009 0.008
vector-mean-n=500 0.041 0.040
vector-mean-n=1000 0.082 0.082
vector-mean-n=2500 0.208 0.205
vector-mean-n=5000 0.410 0.405
vector-mean-n=10000 0.821 0.816
vector-mean-n=25000 2.030 2.013
vector-summary-n=100 0.065 0.063
vector-summary-n=500 0.396 0.395
vector-summary-n=1000 0.859 0.857
vector-summary-n=2500 2.477 2.472
vector-summary-n=5000 5.487 5.440
vector-summary-n=10000 11.523 11.430
vector-summary-n=25000 30.717 30.686
list-mean-n=100 0.010 0.009
list-mean-n=500 0.048 0.046
list-mean-n=1000 0.095 0.094
list-mean-n=2500 0.258 0.256
list-mean-n=5000 0.544 0.538
list-mean-n=10000 1.110 1.106
list-mean-n=25000 2.841 2.823
list-summary-n=100 0.067 0.065
list-summary-n=500 0.402 0.401
list-summary-n=1000 0.883 0.877
list-summary-n=2500 2.532 2.522
list-summary-n=5000 5.463 5.457
list-summary-n=10000 11.721 11.673
list-summary-n=25000 31.468 31.427
──────────────────────────────────────────────────────
STRING_OPS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
search long/absent 0.016 0.015
search long/present 0.021 0.020
search short needle 0.020 0.020
search needle=haystack 0.155 0.151
hash 2000x8 0.035 0.033
hash 2000x64 0.095 0.094
hash 500x512 0.200 0.197
──────────────────────────────────────────────────────
SUDOKU_LIST
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
Easy (36 givens) 10.859 10.794
Medium (30 givens) 0.156 0.148
Hard (25 givens) 2272.701 2272.701
Expert (23 givens) 127.253 127.253
──────────────────────────────────────────────────────
SUDOKU_VECTOR
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
Easy (36 givens) 7.094 7.072
Medium (30 givens) 0.097 0.095
Hard (25 givens) 1467.573 1467.573
Expert (23 givens) 82.655 82.655
──────────────────────────────────────────────────────
XML-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
wide-1000 1.093 1.073
deep-200 0.142 0.138
attributes-500 0.818 0.804
mixed-500 1.359 1.350
entities-2000 3.339 3.320
ooxml-500 2.665 2.650
──────────────────────────────────────────────────────
XML-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
wide-1000 0.559 0.544
deep-200 0.103 0.090
attributes-500 0.598 0.587
mixed-500 0.840 0.835
escaping-2000 2.626 2.613
──────────────────────────────────────────────────────
ZIP-CREATE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
single-deflate 0.438 0.412
single-stored 0.025 0.019
mixed-16 3.037 3.000
many-128 42.802 42.728
large-256k 16.875 16.398
──────────────────────────────────────────────────────
ZIP-ENTRIES
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
single-deflate 0.004 0.002
single-stored 0.003 0.001
mixed-16 0.012 0.010
many-128 0.078 0.071
large-256k 0.002 0.001
──────────────────────────────────────────────────────
ZIP-EXTRACT
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
single-deflate 0.098 0.089
single-stored 0.055 0.050
mixed-16 0.367 0.361
many-128 4.347 4.335
large-256k 2.023 1.998
──────────────────────────────────────────────────────
ZLIB-COMPRESS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
text-4k 0.725 0.712
text-32k 3.219 3.199
incremental-16k 2.634 2.586
runs-32k 7.144 7.074
──────────────────────────────────────────────────────
ZLIB-DECOMPRESS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
text-4k 0.262 0.251
text-32k 1.210 1.201
incremental-16k 0.826 0.817
runs-32k 2.112 2.097
──────────────────────────────────────────────────────