summaryrefslogtreecommitdiff
path: root/wasm/setjmp.h
diff options
context:
space:
mode:
authorYuta Saito <[email protected]>2022-01-27 21:33:39 +0900
committerYuta Saito <[email protected]>2022-02-18 18:28:18 +0900
commitdff70b50d01930213e7799ee52969ff309cc3601 (patch)
treeb01dc7fbfb0875d6b513f4c141e0d3c05f060095 /wasm/setjmp.h
parentac32b7023a7743b1f0cdcfe11156c95c0edb7c54 (diff)
[wasm] vm.c: stop unwinding to main for every vm_exec call by setjmp
the original rb_wasm_setjmp implementation always unwinds to the root call frame to have setjmp compatible interface, and simulate sjlj's undefined behavior. Therefore, every vm_exec call unwinds to main, and a deep call stack makes setjmp call very expensive. The following snippet from optcarrot takes 5s even though it takes less than 0.3s on native. ``` [0x0, 0x4, 0x8, 0xc].map do |attr| (0..7).map do |j| (0...0x10000).map do |i| clr = i[15 - j] * 2 + i[7 - j] clr != 0 ? attr | clr : 0 end end end ``` This patch adds a WASI specialized vm_exec which uses lightweight try-catch API without unwinding to the root frame. After this patch, the above snippet takes only 0.5s.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5502
Diffstat (limited to 'wasm/setjmp.h')
-rw-r--r--wasm/setjmp.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/wasm/setjmp.h b/wasm/setjmp.h
index 30ea23ca12..65e35c03b3 100644
--- a/wasm/setjmp.h
+++ b/wasm/setjmp.h
@@ -58,4 +58,38 @@ typedef rb_wasm_jmp_buf jmp_buf;
#define setjmp(env) rb_wasm_setjmp(env)
#define longjmp(env, payload) rb_wasm_longjmp(env, payload)
+
+typedef void (*rb_wasm_try_catch_func_t)(void *ctx);
+
+struct rb_wasm_try_catch {
+ rb_wasm_try_catch_func_t try_f;
+ rb_wasm_try_catch_func_t catch_f;
+ void *context;
+ int state;
+};
+
+//
+// Lightweight try-catch API without unwinding to root frame.
+//
+
+void
+rb_wasm_try_catch_init(struct rb_wasm_try_catch *try_catch,
+ rb_wasm_try_catch_func_t try_f,
+ rb_wasm_try_catch_func_t catch_f,
+ void *context);
+
+// Run, catch longjmp thrown by run, and re-catch longjmp thrown by catch, ...
+//
+// 1. run try_f of try_catch struct
+// 2. catch longjmps with the given target jmp_buf or exit
+// 3. run catch_f if not NULL, otherwise exit
+// 4. catch longjmps with the given target jmp_buf or exit
+// 5. repeat from step 3
+//
+// NOTICE: This API assumes that all longjmp targeting the given jmp_buf are NOT called
+// after the function that called this function has exited.
+//
+void
+rb_wasm_try_catch_loop_run(struct rb_wasm_try_catch *try_catch, rb_wasm_jmp_buf *target);
+
#endif