Skip to content

Stack safety overhaul and effect handlers debloating MK1 #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 19 commits into
base: hkmc2
Choose a base branch
from

Conversation

CAG2Mark
Copy link
Contributor

@CAG2Mark CAG2Mark commented May 15, 2025

Changes

  • Stack safety instrumentation now happens after the handler lowering
  • The stack depth is incremented at the start of each function rather than before each call, which reduces the file size and allows for different functions to increment the stack depth by different amounts (not implement)
  • No more virtual stack depth tracking -- only the real stack depth is tracked
    • This also gives a better estimation of the stack depth, because previously, it did not account for other effects being raised and decreasing the stack depth
  • Stack depth is not tracked in continuation classes, nor can they raise a stack delay effect
  • All manipulations of the continuation linked during unwinding list now happen in a doUnwind function, which is unique for each function
  • The lifter removes the curried function definition when that function does not appear in a naked reference (i.e. it's used as a first class function)
    • In general, this could be moved to a "de-currying" stage later on to optimize all such functions

Results

  • Marginal decrease in the compiled size of NofibPrelude.mls with only effect handlers and lifting enabled
  • A 21.8% decrease in file size (530KB to 435KB) of NoFibPrelude.mls when stack safety and effect handlers are enabled

TODO

  • The runtime functions should also update the stack depth
  • Each function should increment the stack depth by different amounts
  • Sanity checks breaks the compilation, as they are inserted before the handler lowering and break some assumptions.

@CAG2Mark CAG2Mark changed the base branch from mlscript to hkmc2 May 15, 2025 11:13
@CAG2Mark CAG2Mark marked this pull request as draft May 15, 2025 11:18
@CAG2Mark CAG2Mark requested a review from Copilot May 15, 2025 16:01
Copilot

This comment was marked as off-topic.

@CAG2Mark

This comment was marked as off-topic.

@CAG2Mark CAG2Mark mentioned this pull request May 15, 2025
29 tasks
@CAG2Mark
Copy link
Contributor Author

CAG2Mark commented May 19, 2025

Performance changes:
Graph showing the performance changes with this PR
y-axis: Performance relative to baseline

CSV data:

ansi,78.305%,78.572%,45.180%,48.735%
atom,64.709%,61.997%,38.102%,43.725%
awards,78.522%,78.463%,47.855%,50.185%
banner,80.986%,81.064%,50.575%,51.395%
boyer,79.368%,84.953%,53.873%,54.335%
boyer2,80.787%,82.025%,53.624%,58.412%
calendar,76.608%,78.913%,35.017%,36.746%
cichelli,75.770%,79.192%,42.470%,43.530%
circsim,79.495%,81.833%,44.099%,45.953%
clausify,75.126%,76.282%,42.295%,48.461%
constraints,84.604%,84.100%,59.034%,59.930%
cryptarithm2,73.042%,72.861%,36.957%,38.145%
cse,83.834%,83.137%,56.859%,60.370%
eliza,77.885%,79.358%,44.876%,50.093%
fish,86.550%,84.613%,41.852%,49.446%
gcd,89.088%,92.064%,34.430%,38.215%
integer,74.860%,76.152%,47.172%,50.363%
knights,83.976%,82.673%,39.919%,42.936%
lambda,81.313%,83.163%,41.116%,43.634%
lastpiece,82.387%,80.275%,47.155%,49.383%
lcss,82.506%,82.066%,49.759%,51.948%
life,78.349%,77.587%,49.700%,52.061%
mandel,88.684%,72.480%,44.944%,46.229%
mandel2,87.548%,91.400%,51.361%,55.956%
mate,76.593%,80.295%,40.363%,41.765%
minimax,80.468%,79.293%,50.415%,54.149%
para,71.413%,74.906%,33.990%,37.404%
power,93.648%,85.929%,74.931%,74.316%
pretty,75.643%,78.346%,43.287%,41.117%
primetest,80.769%,79.593%,24.422%,23.966%
rsa,97.894%,98.085%,91.702%,91.384%
scc,84.305%,81.658%,46.160%,48.809%
secretary,61.378%,67.018%,37.491%,37.576%
sorting,89.380%,88.693%,43.986%,47.003%
sphere,78.865%,84.152%,38.884%,42.691%
Average,80.42%,80.66%,46.40%,48.87%

@CAG2Mark
Copy link
Contributor Author

CAG2Mark commented May 19, 2025

Compile size changes:
Graph showing the changes in compile sizes due to this PR

y-axis: File size relative to baseline
Baseline: No lifting or instrumentation
Effect only: Lifting and function instrumentation

CSV:

Predef.mjs,6.351729213,6.244297277,14.72700515,11.97939662
NofibPrelude.mjs,6.253761392,6.114735999,11.73907302,9.631524257
BenchmarkPrelude.mjs,5.595646438,5.666226913,13.10158311,10.7328496
fish.mjs,4.725480769,4.388548951,10.20030594,8.51402972
constraints.mjs,6.941625547,6.618129907,12.25391801,9.755236943
integer.mjs,6.078100264,5.530474934,12.39327177,10.02031662
rsa.mjs,8.532602311,8.019385158,14.90973174,11.54121794
cryptarithm1.mjs,5.659736106,5.222710916,10.79354925,8.49873384
ansi.mjs,10.15923817,9.639112507,16.30149224,12.62850972
atom.mjs,7.189570643,6.683702273,12.62204724,9.929728124
cse.mjs,7.291286527,7.019106175,12.49676054,10.03821235
circsim.mjs,6.506663006,6.121490024,11.07830862,9.035969248
calendar.mjs,10.44584988,9.916535869,17.16729757,13.35435325
knights.mjs,8.960993913,8.246122298,13.74310085,10.92779075
awards.mjs,6.84441892,6.46507257,12.48407643,9.900699593
boyer.mjs,12.99216737,11.66013105,17.12702343,13.09936497
mandel.mjs,6.261008031,5.835364165,12.49736915,10.16657436
minimax.mjs,5.171566909,4.79397073,8.781263693,7.057838927
scc.mjs,8.407297538,7.602491842,12.81637496,9.915455354
mandel2.mjs,5.80392773,5.400785546,8.278083268,6.756009427
lastpiece.mjs,4.902262525,4.696119911,8.689141427,7.44279191
treejoin.mjs,6.066440881,5.616196832,9.515888037,7.716406017
life.mjs,6.244507442,6.050061207,11.19837639,9.25958379
cichelli.mjs,6.496831536,6.19525142,11.03464735,9.010904452
clausify.mjs,8.211840253,7.310909653,12.46667353,9.693176745
pretty.mjs,7.222479722,6.731846273,11.62355156,9.385525299
banner.mjs,3.869155104,3.735656388,6.767358006,6.284160729
secretary.mjs,9.551166966,8.772890485,15.83482944,12.01406344
gcd.mjs,5.238574352,4.974249659,10.43417462,8.484822647
power.mjs,9.789845712,8.803803373,15.17822031,11.54894151
eliza.mjs,5.462865497,5.173279352,9.045299145,7.60845704
boyer2.mjs,4.75916492,4.430581976,7.627261003,6.335950347
puzzle.mjs,7.051832722,6.508359386,11.2395451,8.885234017
lcss.mjs,9.260149726,8.457529513,14.20097898,11.08364526
primetest.mjs,7.898645876,7.421146218,12.84687457,10.30912324
lambda.mjs,7.221995569,6.680969296,12.03580347,9.727956951
sorting.mjs,7.465911643,6.852770381,12.13539093,9.365074273
sphere.mjs,7.426542909,6.933831022,11.21131097,9.185370608
para.mjs,8.450345529,7.8450556,14.07485881,11.0743677
mate.mjs,7.721036956,7.165820859,11.84122256,9.405092741
cryptarithm2.mjs,7.961723092,7.617104598,14.05728903,11.1993431
Average,7.181609502,6.71126411,12.16025198,9.719604961

@CAG2Mark
Copy link
Contributor Author

CAG2Mark commented May 19, 2025

Did another optimization and managed to bring down the compiled stack safety size by a bit.
image

Predef.mjs,6.351729213,6.244297277,14.72700515,11.80647535
NofibPrelude.mjs,6.253761392,6.114735999,11.73907302,8.387850973
BenchmarkPrelude.mjs,5.595646438,5.666226913,13.10158311,10.4762533
fish.mjs,4.725480769,4.388548951,10.20030594,7.820498252
constraints.mjs,6.941625547,6.618129907,12.25391801,8.50876703
integer.mjs,6.078100264,5.530474934,12.39327177,9.450263852
rsa.mjs,8.532602311,8.019385158,14.90973174,11.23947523
cryptarithm1.mjs,5.659736106,5.222710916,10.79354925,6.258963081
ansi.mjs,10.15923817,9.639112507,16.30149224,12.19840958
atom.mjs,7.189570643,6.683702273,12.62204724,8.682365176
cse.mjs,7.291286527,7.019106175,12.49676054,9.279584821
circsim.mjs,6.506663006,6.121490024,11.07830862,7.607340289
calendar.mjs,10.44584988,9.916535869,17.16729757,13.25288524
knights.mjs,8.960993913,8.246122298,13.74310085,9.833242435
awards.mjs,6.84441892,6.46507257,12.48407643,9.007309178
boyer.mjs,12.99216737,11.66013105,17.12702343,13.0789159
mandel.mjs,6.261008031,5.835364165,12.49736915,8.635558017
minimax.mjs,5.171566909,4.79397073,8.781263693,6.151279467
scc.mjs,8.407297538,7.602491842,12.81637496,8.88742213
mandel2.mjs,5.80392773,5.400785546,8.278083268,6.045561665
lastpiece.mjs,4.902262525,4.696119911,8.689141427,6.161872452
treejoin.mjs,6.066440881,5.616196832,9.515888037,6.555035362
life.mjs,6.244507442,6.050061207,11.19837639,8.227884801
cichelli.mjs,6.496831536,6.19525142,11.03464735,8.178133487
clausify.mjs,8.211840253,7.310909653,12.46667353,8.324576766
pretty.mjs,7.222479722,6.731846273,11.62355156,9.090623793
banner.mjs,3.869155104,3.735656388,6.767358006,6.212680647
secretary.mjs,9.551166966,8.772890485,15.83482944,11.43776182
gcd.mjs,5.238574352,4.974249659,10.43417462,6.556957708
power.mjs,9.789845712,8.803803373,15.17822031,11.35518479
eliza.mjs,5.462865497,5.173279352,9.045299145,7.209581646
boyer2.mjs,4.75916492,4.430581976,7.627261003,5.488989199
puzzle.mjs,7.051832722,6.508359386,11.2395451,7.644239047
lcss.mjs,9.260149726,8.457529513,14.20097898,10.0272099
primetest.mjs,7.898645876,7.421146218,12.84687457,9.47154972
lambda.mjs,7.221995569,6.680969296,12.03580347,8.262828404
sorting.mjs,7.465911643,6.852770381,12.13539093,8.286983862
sphere.mjs,7.426542909,6.933831022,11.21131097,8.189933986
para.mjs,8.450345529,7.8450556,14.07485881,10.3607535
mate.mjs,7.721036956,7.165820859,11.84122256,8.390226131
cryptarithm2.mjs,7.961723092,7.617104598,14.05728903,10.49621021
Average,7.181609502,6.71126411,12.16025198,8.842381419

It seems this change also increased the performance by a measurable amount (full benchmark coming later)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant