From 54dbdd14f96e13fce76d34883be46a9adff5c74f Mon Sep 17 00:00:00 2001 From: dvdvgt <40773635+dvdvgt@users.noreply.github.com> Date: Fri, 20 Mar 2026 16:43:03 +0100 Subject: [PATCH 1/2] make thunking dynamically depended on call-depth --- .../effekt/generator/js/TransformerCps.scala | 3 +-- libraries/js/effekt_runtime.js | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/effekt/shared/src/main/scala/effekt/generator/js/TransformerCps.scala b/effekt/shared/src/main/scala/effekt/generator/js/TransformerCps.scala index 1d7ea09207..38049f3650 100644 --- a/effekt/shared/src/main/scala/effekt/generator/js/TransformerCps.scala +++ b/effekt/shared/src/main/scala/effekt/generator/js/TransformerCps.scala @@ -300,8 +300,7 @@ object TransformerCps extends Transformer { } case cps.Stmt.Jump(k, vargs, ks) => - pure(js.Return(maybeThunking(js.Call(nameRef(k), - vargs.map(toJS) ++ List(toJS(ks))))) :: Nil) + pure(List(js.Return(js.Call(js.Variable(js.JSName("THUNK_K")), nameRef(k) :: vargs.map(toJS) ++ List(toJS(ks)))))) case cps.Stmt.App(Recursive(id, label, vparams, bparams, ks1, k1, used), vargs, bargs, MetaCont(ks), k) => diff --git a/libraries/js/effekt_runtime.js b/libraries/js/effekt_runtime.js index bb149178db..65f98cd02f 100644 --- a/libraries/js/effekt_runtime.js +++ b/libraries/js/effekt_runtime.js @@ -135,6 +135,27 @@ function RESUME(cont, c, ks, k) { return () => c(meta, k1) } +let _depth = 0 + +function THUNK_K(k, v, ks) { + const threshold = 1000 + if (_depth >= threshold) { + return () => k(v, ks) + } else { + _depth += 1 + return k(v, ks) + } +} + +function THUNK(prog) { + if (_depth >= threshold) { + return () => prog() + } else { + _depth += 1 + return prog() + } +} + function RUN_TOPLEVEL(comp) { try { let a = comp(TOPLEVEL_KS, TOPLEVEL_K) From a28d75664b39bd5f53e9bca3b25f79b7d12f49d1 Mon Sep 17 00:00:00 2001 From: dvdvgt <40773635+dvdvgt@users.noreply.github.com> Date: Mon, 23 Mar 2026 16:32:14 +0100 Subject: [PATCH 2/2] tune threshold value --- libraries/js/effekt_runtime.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/js/effekt_runtime.js b/libraries/js/effekt_runtime.js index 65f98cd02f..ce9d965f72 100644 --- a/libraries/js/effekt_runtime.js +++ b/libraries/js/effekt_runtime.js @@ -138,7 +138,7 @@ function RESUME(cont, c, ks, k) { let _depth = 0 function THUNK_K(k, v, ks) { - const threshold = 1000 + const threshold = 512 if (_depth >= threshold) { return () => k(v, ks) } else {