Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Improve async loop time #1460

Closed
laurensvalk opened this issue Feb 19, 2024 · 9 comments · Fixed by pybricks/pybricks-micropython#263
Closed

[Bug] Improve async loop time #1460

laurensvalk opened this issue Feb 19, 2024 · 9 comments · Fixed by pybricks/pybricks-micropython#263
Labels
bug Something isn't working software: pybricks-blocks Issues with blocks and code generation software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime)

Comments

@laurensvalk
Copy link
Member

The loop time doesn't seem to work as intended which is a separate issue.

Originally posted by @laurensvalk in #1429 (comment)

@laurensvalk
Copy link
Member Author

The following works as "intended", but this is somewhat questionable. The async loop time is set to 10 ms.

from pybricks.tools import run_task, wait, StopWatch

watch = StopWatch()

async def main1():
    while True:
        await wait(1)
        print(watch.time())

run_task(main1())
10
20
30
40
50
60
70
80
90

@laurensvalk
Copy link
Member Author

laurensvalk commented Feb 19, 2024

On second thought, the issue I thought I was originally seeing wasn't due to async, so this is not as critical as I thought.

It's probably still worth considering the 10 ms loop though, as it would slow down plain-math loops in block code, since they always have one yield or (await 0) auto-inserted.

@laurensvalk
Copy link
Member Author

Another possibility is to leave it at 10 ms, and provide an option to avoid auto-inserting loop trap yields for more advanced users.

@laurensvalk laurensvalk added the software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) label Feb 19, 2024
@laurensvalk
Copy link
Member Author

laurensvalk commented May 31, 2024

Also, it would be nice to avoid inserting it if there is already a known, nonzero constant wait in the loop at the same nesting level. We should be able to detect this quite easily since we know the inner stack of block when we generate the code for the loop. EDIT: We might only be able to do this if that wait is guaranteed to run (e.g. sits at the top).

Right now, if you have a 1 ms loop and you then switch to multitask (which adds another 1ms wait), the loop actually takes 20 ms, since the async loop time is 10ms right now.

@dlech
Copy link
Member

dlech commented May 31, 2024

Could we expose the loop time in the "set up" hat block? I.e. If there is only one "program" hat block, nothing changes. If there is more than one "program" hat block then "set up" grows an extra parameter for the loop time. This would make it somewhat obvious that there is an implicit loop time shared by all parallel tasks. And that way, people could tweak the default 10ms loop time if needed.

@laurensvalk
Copy link
Member Author

That's a good idea.

I was thinking about having some kind of extra (and thereby optional) block to configure that sort of thing. Besides the async loop time, we could allow users to configure if (and/or how much) waits are injected in loops and functions.

Power users could opt to skip inserting waits. Right now, iterating through a list to do some operation also takes 10ms each iteration for example, which is probably undesired in (finite) data processing cases.

@laurensvalk
Copy link
Member Author

In #1780 it was suggested to drop the loop_time arg. This is done in pybricks/pybricks-micropython#263. Combined with the fix for wait(0), this issue is fixed.

What remains then is to change the block code generator to insert wait(0) instead of wait(1).

@laurensvalk
Copy link
Member Author

What remains then is to change the block code generator to insert wait(0) instead of wait(1).

This isn't done yet, so re-opening.

@laurensvalk laurensvalk reopened this Aug 28, 2024
@laurensvalk laurensvalk added the software: pybricks-blocks Issues with blocks and code generation label Aug 28, 2024
@laurensvalk
Copy link
Member Author

What remains then is to change the block code generator to insert wait(0) instead of wait(1).

This isn't done yet, so re-opening.

Fixed and released in latest beta.

@laurensvalk laurensvalk added the bug Something isn't working label Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working software: pybricks-blocks Issues with blocks and code generation software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants