Repository navigation
Conversation
|
Code size report: |
ffc3964 to
bf7cab6
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #18571 +/- ##
==========================================
- Coverage 98.45% 98.44% -0.02%
==========================================
Files 175 176 +1
Lines 22635 22387 -248
==========================================
- Hits 22286 22038 -248
Misses 349 349 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The webassembly CI failure looks like it's caused by the "webassembly/api: Fix CLI" commit. On master, the stdin-as-source check I've fixed it by guarding the stdin read with |
db849c1 to
bf7cab6
Compare
Signed-off-by: Damien George <[email protected]>
For testing. Signed-off-by: Damien George <[email protected]>
bf7cab6 to
f28bd9d
Compare
It can be `undefined` as well. Signed-off-by: Damien George <[email protected]>
Signed-off-by: Damien George <[email protected]>
Enabling `MICROPY_PY_SYS_SETTRACE` increases the output wasm file by +41k. Enabling `MICROPY_PY_SYS_SETTRACE_DUAL_VM` on top of that adds a further +17k, but performance is regained. In particular, `perf_bench/misc_aes.py` runs 200 times faster with dual-VM enabled compared to having it disabled with `sys.settrace` enabled. Signed-off-by: Damien George <[email protected]>
Signed-off-by: Damien George <[email protected]>
Signed-off-by: Damien George <[email protected]>
So that, after enabling a settrace callback, the outer-most function call will call into the callback when dual-VM is enabled. Signed-off-by: Damien George <[email protected]>
f28bd9d to
9510dfa
Compare
Summary
MicroPython supports the standard
sys.settrace()facility, to trace bytecode execution. That feature is enabled throughMICROPY_PY_SYS_SETTRACEand is disabled by default because it really slows the VM down, and increases memory use in many places due to the overhead of tracking the state of functions.This PR adds a new option
MICROPY_PY_SYS_SETTRACE_DUAL_VMthat, when enabled, duplicates the VM, themp_execute_bytecode()function:mp_execute_bytecode_standard()and has settrace disabled.mp_execute_bytecode_tracing()and has settrace enabled.Then a wrapper function
mp_execute_bytecode()calls into the appropriate VM depending on whether the user has enabled a settrace callback or not.The result is:
This is useful for, eg, webassembly where PyScript wants to have
sys.settraceenabled, and can afford to have a dual-VM in order not to lose performance with settrace enabled.This PR also includes some minor fixes for the webassembly port to be able to run the performance benchmarks on that port.
Testing
Tested by running the standard test suite on the unix and webassembly ports, which also have dual-VM enabled in CI.
On the webassembly port, the
perf_bench/misc_aes.pyperformance test runs 200 times faster (!) with dual-VM enabled, compared to just enabling settrace without the dual-VM. So it's a very big win there.On PYBV10 the metrics are:
MICROPY_PY_SYS_SETTRACEenabledMICROPY_PY_SYS_SETTRACE_DUAL_VMenabled as wellThe performance measurements on PYBV10 in detail
Enabling
MICROPY_PY_SYS_SETTRACE:Tests that are heavy on the VM (
misc_aes.pyandmisc_pystone.py) run at about half the speed with settrace enabled.Enabling both
MICROPY_PY_SYS_SETTRACEandMICROPY_PY_SYS_SETTRACE_DUAL_VM(comparing to settrace completely disabled):Performance is almost all regained with dual-VM enabled.
Trade-offs and Alternatives
This obviously duplicates the VM, but I tried to make it as simple as possible in the implementation so that it's easy to maintain. In the future this mechanism could potentially be extended to optimise for other things, although I can't think of any obvious candidates for that.
An alternative to having a dual-VM could be to try and optimise the internal VM macros
FRAME_UPDATEandTRACE_TICK. Although that could save code size, no matter how well these macros are optimised it won't be able to reach the performance of a dual-VM architecture.I'm not sure exactly how CPython implements efficient
sys.settracethese days (they've changed a lot recently with their VM), but could look there for alternative ideas as well.