Interpreter on MCU: is script-side preemption worth its cost, or is cooperative + host watchdog the sane design?
i make a small embeddable scripting language (zym) with mcu use as a target, runtime only builds, statically provisionable heap, that kind of stuffs. its at 0.3.x and im at a design fork i want opinions on from people who put interpreters on constrained hardware
the vm currently has instruction count preemption that is deterministic and exact. you say fire every N instructions and it fires at instruction N, and both the host and the script can arm it. it also has continuations as a primitive so script can build its own schedulers
exactness costs a counter check on every instruction, measured at about 21% of throughput on dispatch heavy code. checking only at loop back edges and calls recovers 15-17% but the watchdog then overshoots by up to one straight line block instead of firing on the exact instruction
what im considering:
- script side concurrency becomes cooperative fibers/coroutines with explicit yield and native scheduling, no counter involved
- preemption becomes host only, a watchdog and hard stop the embedding code arms to bound runaway or untrusted script. script cannot see or touch it, bounded rather than exact, and it is a compile flag so a fully trusted build drops the check entirely
questions for this crowd specifically:
- for a watchdog on script code, do you need it to fire on an exact instruction, or do you need it to be impossible to outrun and fire within a bounded window? my instinct is bounded is fine for nearly everything but timing determinism people are exactly who i would expect to disagree
- is script side preemption, the script scheduling itself preemptively, something you would actually want on an mcu, or is cooperative plus a host guard the sane shape? i built the script side version and i am not sure anyone needed it
- anyone shipping an interpreter on hardware with a guard on / guard off build split, and did it cause problems later?
these questions have been bugging me for a while so mostly here for the answers, happy to explain any of the reasoning if it helps, have been trying to come to a decision for a few weeks so am reaching out for people that use scripting on their systems and what their expectations are as well as what they actually care about in these regards
website: https://zym-lang.org/
github: https://github.com/zym-lang/