Continued to improve the Menai VM.
Improving behaviour with clang
It turns out that -O3 optimizations work better for clang. At the same time it also worked out that branch prediction hints don't win in general. Previously there were hit and miss gains by using the MENAI_LIKELY and MENAI_UNLIKELY so I've left them in the codebase, but disabled them for clang.
Reference counting improvements
One of the costs in the Menai VM is that it has to track the reference counts on all registers, so retaining a reference and then releasing it can be quite expensive. In particular we end up doing 2 writes to memory.
Our "None" object is not unboxed so it doesn't need a reference count, so it has become possible to move a register's contents to another register or into a data structure and simply overwrite the original register with "None". Doing this doesn't require a reference count change and is thus less expensive.
What this also allows is us to have a MOVE opcode and a COPY opcode. COPY has the original semantics of MOVE, but now MOVE no longer retains a new reference on the destination.
A bunch of internall plumbing changes allow the same logic to be applied in quite a few other instructions, all without changing any user-visible aspect of the language, and only affecting the very last bytecode emitter logic.
Performance
The cumulative results of the changes are interesting! Against the previous run, the geometric mean improvement across the 245 benchmark cases that appear in both is 2.95%, with 169 cases faster and 50 slower.
BASE64-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
standard-1k 0.109 0.101
standard-64k 7.162 7.065
url-1k 0.103 0.101
url-64k 7.346 7.219
──────────────────────────────────────────────────────
BASE64-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
standard-1k 0.047 0.044
standard-64k 2.837 2.798
unpadded-1k 0.047 0.044
url-64k 2.849 2.811
──────────────────────────────────────────────────────
BMP-DECODE
────────────────────────────────────────────────────────
Case mean (ms) min (ms)
────────────────────────────────────────────────────────
truecolour-64x64 0.200 0.178
truecolour-128x128 0.836 0.801
truecolour-topdown-128x128 0.809 0.806
truecolour-alpha-128x128 0.943 0.931
padded-65x64 0.191 0.178
────────────────────────────────────────────────────────
BMP-ENCODE
────────────────────────────────────────────────────────
Case mean (ms) min (ms)
────────────────────────────────────────────────────────
truecolour-64x64 0.274 0.256
truecolour-128x128 1.071 1.046
truecolour-1024x16 1.042 1.031
truecolour-16x1024 1.140 1.120
truecolour-256x256 4.117 4.048
truecolour-topdown-128x128 1.088 1.082
truecolour-alpha-128x128 1.272 1.268
padded-65x64 0.277 0.273
────────────────────────────────────────────────────────
CALENDAR
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
short_5d 0.020 0.017
month_20d 0.065 0.062
quarter_60d 0.189 0.187
year_250d 0.777 0.773
months_600d 1.356 1.345
──────────────────────────────────────────────────────
CSV-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
plain-1000x10 1.779 1.757
quoted-1000x10 2.005 1.990
escaped-1000x10 4.126 4.100
multiline-1000 0.451 0.446
wide-100x100 1.636 1.633
──────────────────────────────────────────────────────
CSV-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
plain-1000x10 2.398 2.355
quoted-1000x10 2.640 2.585
escaped-1000x10 2.579 2.562
wide-100x100 1.468 1.459
──────────────────────────────────────────────────────
DEFLATE-COMPRESS
───────────────────────────────────────────────────────────────────────────────────────────────────
Case mean menai min menai mean python min python ref/menai
───────────────────────────────────────────────────────────────────────────────────────────────────
text-4k 0.532 0.519 0.593 0.573 1.10x
text-32k 2.297 2.268 2.510 2.471 1.09x
incremental-16k 1.960 1.883 3.612 3.485 1.85x
runs-32k 4.769 4.717 7.831 7.600 1.61x
───────────────────────────────────────────────────────────────────────────────────────────────────
DEFLATE-DECOMPRESS
───────────────────────────────────────────────────────────────────────────────────────────────────
Case mean menai min menai mean python min python ref/menai
───────────────────────────────────────────────────────────────────────────────────────────────────
text-4k 0.143 0.132 0.119 0.108 0.82x
text-32k 0.304 0.293 0.296 0.288 0.98x
incremental-16k 0.365 0.356 0.489 0.481 1.35x
stored-8k 0.047 0.040 0.004 0.002 0.04x
runs-32k 1.187 1.148 1.677 1.580 1.38x
───────────────────────────────────────────────────────────────────────────────────────────────────
DOCX-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
minimal 0.273 0.257
document-100 1.011 0.914
document-500 4.375 4.295
document-2000 16.880 16.703
styles-100 1.647 1.583
numbering-9 0.918 0.877
media-16 3.222 3.185
full-500 7.329 7.242
──────────────────────────────────────────────────────
DOCX-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
minimal 0.030 0.023
document-100 0.127 0.118
document-500 0.530 0.506
styles-100 0.165 0.158
numbering-9 0.058 0.053
media-16 0.119 0.110
full-500 0.642 0.631
──────────────────────────────────────────────────────
HTML-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
wide-1000 1.785 1.760
deep-200 0.167 0.163
attributes-500 1.668 1.650
mixed-500 1.669 1.653
entities-2000 4.924 4.896
void-1000 4.587 4.533
omitted-end-tags-1000 0.999 0.968
raw-text-500 1.340 1.328
realistic-500 3.257 3.217
──────────────────────────────────────────────────────
HTML-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
wide-1000 0.958 0.900
deep-200 0.151 0.116
attributes-500 0.555 0.548
mixed-500 0.788 0.785
void-1000 2.103 2.090
escaping-2000 2.377 2.356
declarations-1000 0.483 0.479
──────────────────────────────────────────────────────
JPEG-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
greyscale-64x64 2.572 2.535
greyscale-detail-64x64 4.047 4.030
colour-444-64x64 7.698 7.668
colour-420-64x64 4.795 4.777
colour-422-64x64 5.758 5.736
colour-440-64x64 5.773 5.747
colour-444-128x128 30.964 30.901
colour-420-128x128 19.293 19.231
colour-444-97x61 12.109 12.085
──────────────────────────────────────────────────────
JPEG-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
colour-64x64 7.505 7.490
colour-128x128 32.545 32.372
colour-97x61 11.940 11.922
──────────────────────────────────────────────────────
JSON-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
object 0.011 0.010
flat_array 0.068 0.066
flat_object 0.060 0.058
mixed_nested 0.131 0.128
string_heavy 0.047 0.046
numbers_array 0.015 0.013
unicode_strings 0.007 0.006
long_string 0.056 0.055
deep_array 0.120 0.118
──────────────────────────────────────────────────────
JSON-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
object 0.006 0.005
flat_array 0.012 0.011
flat_object 0.017 0.016
mixed_nested 0.037 0.035
string_heavy 0.040 0.039
numbers_array 0.007 0.005
unicode_strings 0.002 0.001
long_string 0.049 0.049
──────────────────────────────────────────────────────
PNG-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
greyscale-64x64 1.273 1.252
greyscale-alpha-64x64 5.960 5.942
palette-128x128 3.964 3.953
truecolour-128x128 34.296 34.088
truecolour-alpha-128x128 45.746 45.640
truecolour-192x192 79.230 79.017
──────────────────────────────────────────────────────
PNG-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
truecolour-48x48 7.279 7.267
truecolour-96x96 36.030 35.888
truecolour-alpha-96x96 54.092 54.068
──────────────────────────────────────────────────────
PPTX-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
minimal 0.618 0.599
slides-10 2.612 2.598
slides-50 11.413 11.390
slides-200 44.580 44.428
shapes-50 6.746 6.655
masters-8 5.723 5.660
media-16 5.828 5.810
theme-and-masters 4.352 4.333
full-50 54.082 53.955
──────────────────────────────────────────────────────
PPTX-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
minimal 0.041 0.031
slides-10 0.096 0.087
slides-50 0.357 0.346
shapes-50 0.705 0.688
masters-8 0.162 0.154
media-16 0.196 0.187
theme-and-masters 0.133 0.125
full-50 3.395 3.322
──────────────────────────────────────────────────────
REGEXP
───────────────────────────────────────────────────────
Case mean (ms) min (ms)
───────────────────────────────────────────────────────
compile-per-line 0.060 0.057
search-prefix-log 0.031 0.029
search-class-numbers 0.034 0.032
search-alternation 0.053 0.051
search-all-class 4.235 4.225
search-all-prefix 0.121 0.120
search-groups-log 0.052 0.049
search-all-groups-numbers 31.925 31.795
split-whitespace 5.308 5.264
replace-whitespace 64.394 61.510
replace-groups-dates 15.606 15.495
chained-per-line 0.042 0.040
───────────────────────────────────────────────────────
RUBIKS-LIST
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
1-move 0.058 0.053
2-move 0.056 0.053
3-move 0.126 0.122
4-move 1.281 1.277
5-move 4.316 4.312
6-move 27.996 27.978
7-move 61.652 61.614
7-move with B 7.891 7.880
──────────────────────────────────────────────────────
RUBIKS-VECTOR
───────────────────────────────────────────────────────────────────────────────────────────────────
Case mean menai min menai mean python min python ref/menai
───────────────────────────────────────────────────────────────────────────────────────────────────
1-move 0.046 0.042 0.053 0.051 1.22x
2-move 0.045 0.041 0.051 0.050 1.22x
3-move 0.100 0.096 0.121 0.117 1.22x
4-move 1.018 1.011 1.273 1.272 1.26x
5-move 3.465 3.455 4.334 4.326 1.25x
6-move 22.356 22.328 28.166 28.115 1.26x
7-move 49.626 49.603 62.227 62.131 1.25x
7-move with B 6.310 6.304 8.127 8.116 1.29x
───────────────────────────────────────────────────────────────────────────────────────────────────
SORT-LIST
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
n=10 0.004 0.003
n=50 0.024 0.023
n=100 0.053 0.053
n=250 0.151 0.149
n=500 0.339 0.335
n=1000 0.747 0.745
n=2500 2.174 2.167
n=5000 4.739 4.713
n=10000 10.218 10.209
──────────────────────────────────────────────────────
SORT-VECTOR
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
n=10 0.004 0.003
n=50 0.023 0.022
n=100 0.052 0.051
n=250 0.151 0.148
n=500 0.337 0.332
n=1000 0.737 0.736
n=2500 2.168 2.161
n=5000 4.707 4.700
n=10000 10.186 10.160
──────────────────────────────────────────────────────
STATS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
vector-mean-n=100 0.006 0.005
vector-mean-n=500 0.023 0.023
vector-mean-n=1000 0.046 0.046
vector-mean-n=2500 0.112 0.111
vector-mean-n=5000 0.226 0.224
vector-mean-n=10000 0.453 0.452
vector-mean-n=25000 1.121 1.109
vector-summary-n=100 0.046 0.044
vector-summary-n=500 0.259 0.257
vector-summary-n=1000 0.560 0.557
vector-summary-n=2500 1.588 1.585
vector-summary-n=5000 3.385 3.378
vector-summary-n=10000 7.229 7.195
vector-summary-n=25000 19.804 19.399
list-mean-n=100 0.006 0.005
list-mean-n=500 0.028 0.025
list-mean-n=1000 0.053 0.051
list-mean-n=2500 0.154 0.152
list-mean-n=5000 0.349 0.345
list-mean-n=10000 0.741 0.694
list-mean-n=25000 1.912 1.875
list-summary-n=100 0.046 0.044
list-summary-n=500 0.261 0.259
list-summary-n=1000 0.562 0.559
list-summary-n=2500 1.630 1.627
list-summary-n=5000 3.514 3.498
list-summary-n=10000 7.620 7.506
list-summary-n=25000 20.486 20.277
──────────────────────────────────────────────────────
STRING-OPS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
search long/absent 0.017 0.014
search long/present 0.020 0.020
search short needle 0.022 0.020
search needle=haystack 0.151 0.150
hash 2000x8 0.033 0.030
hash 2000x64 0.093 0.092
hash 500x512 0.198 0.197
──────────────────────────────────────────────────────
SUDOKU-LIST
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
Easy (36 givens) 8.453 8.417
Medium (30 givens) 0.125 0.118
Hard (25 givens) 1774.280 1774.280
Expert (23 givens) 99.405 99.405
──────────────────────────────────────────────────────
SUDOKU-MRV
───────────────────────────────────────────────────────────────────────────────────────────────────
Case mean menai min menai mean python min python ref/menai
───────────────────────────────────────────────────────────────────────────────────────────────────
Easy (36 givens) 0.103 0.096 0.067 0.066 0.68x
Medium (30 givens) 0.065 0.062 0.029 0.028 0.45x
Hard (25 givens) 9.597 9.597 9.876 9.876 1.03x
Expert (23 givens) 23.888 23.888 25.445 25.445 1.07x
───────────────────────────────────────────────────────────────────────────────────────────────────
SUDOKU-VECTOR
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
Easy (36 givens) 6.497 6.483
Medium (30 givens) 0.091 0.088
Hard (25 givens) 1360.128 1360.128
Expert (23 givens) 76.850 76.850
──────────────────────────────────────────────────────
XML-DECODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
wide-1000 0.990 0.972
deep-200 0.122 0.121
attributes-500 0.725 0.719
mixed-500 1.232 1.221
entities-2000 3.130 3.123
ooxml-500 2.395 2.374
──────────────────────────────────────────────────────
XML-ENCODE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
wide-1000 0.521 0.505
deep-200 0.099 0.089
attributes-500 0.531 0.523
mixed-500 0.797 0.791
escaping-2000 2.370 2.354
──────────────────────────────────────────────────────
ZIP-CREATE
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
single-deflate 0.377 0.357
single-stored 0.021 0.015
mixed-16 2.666 2.643
many-128 38.547 38.512
large-256k 16.622 16.343
──────────────────────────────────────────────────────
ZIP-ENTRIES
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
single-deflate 0.003 0.001
single-stored 0.002 0.001
mixed-16 0.010 0.008
many-128 0.069 0.064
large-256k 0.002 0.001
──────────────────────────────────────────────────────
ZIP-EXTRACT
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
single-deflate 0.089 0.082
single-stored 0.048 0.044
mixed-16 0.353 0.347
many-128 4.224 4.206
large-256k 2.007 1.984
──────────────────────────────────────────────────────
ZLIB-COMPRESS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
text-4k 0.639 0.627
text-32k 3.092 3.053
incremental-16k 2.401 2.377
runs-32k 5.610 5.553
──────────────────────────────────────────────────────
ZLIB-DECOMPRESS
──────────────────────────────────────────────────────
Case mean (ms) min (ms)
──────────────────────────────────────────────────────
text-4k 0.245 0.236
text-32k 1.151 1.144
incremental-16k 0.787 0.780
runs-32k 2.027 2.006
──────────────────────────────────────────────────────