edit: the final syntax ended up a bit different, see http://chdk.wikia.com/wiki/Script_commands#set_yieldAfter some thinking about this, I've concluded that yield behavior should be adjustable from script.
Rational:
- Current behavior is acceptable for most scripts.
- Impact of running lots of cycles in kbd_task is unclear, will vary depending what camera is doing.
- 10ms precision of get_tick_count means that even the lowest setting is a drastic change in kbd_task behavior
- The fact lua can run for excessive times is a bug. Better control of scheduling should be added to lua, rather than re-creating this in ubasic.
Conclusion:
Users who actually need extra speed should have direct control over scheduling, so they can determine the best balance of performance and potential side effects.
#3 unfortunately means that one, tick based setting is insufficient. Setting it to 0 could reproduce the current behavior, but you have nothing between 1 line per cycle (which should generally be a fraction of a ms) and noticeably slowing down kbd_task.
Solution:
Allow the user to set maximum lines before yield, and maximum execution time. Whichever one is hit first wins. Setting max lines to 1 reproduces current behavior. Setting either to an arbitrarily high number effectively disables the limit. This feels slightly overcomplicated, but I didn't see a clean way to do everything I wanted with less.
Patch in attached zip implements the above. The defaults reproduce the existing trunk behavior. The line and tick limits are treated as unsigned, so -1 can be used to set either or both "unlimited"
The test script allows you to try different values. The "dummy" test script allows testing the equivalent script on builds that don't have the new commands (see note #1 below for why this is needed).
Test results for D10, 1000 iterations, playback mode, times in ms
old trunk (dummy script): 30020
tsvstar patch (dummy script): 3530
defaults: 30020
no yield (-1,-1), play: 3260
Notes
- ubasic is brain damaged in many ways. In testing, I noticed that adding more @ directives and some simple assignments *outside* of the main loop of my test script increased the run time by a factor of ~2. This is because things that implicitly refer to a line number (like a while loop) get there by running the whole script through the tokenizer from the start (see jump_line)
- runtime is different in play and rec mode
- All of the above times are terrible compared to lua, where a similarly trivial loop is ~100x as fast with yields, and ~1000x without http://chdk.setepontos.com/index.php?topic=6729.msg71456#msg71456
I noticed tsvstars patch avoided calling get_tick_count each line. I haven't done this, because the call is quite cheap. Basically, it just disables interrupts, gets a variable, and restores interrupts. If you *really* want more performance out of ubasic, fixing things like the brain damaged mentioned above will be far more productive. Or just use lua...
tsvstars patch also didn't set the yield flag for MD, which would have broken MD scripts. I've added that in mine.