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

Make ControlFlow::Wait the default #3106

Merged
merged 2 commits into from
Sep 22, 2023

Conversation

daxpedda
Copy link
Member

After some discussion in #3105 I was asking myself why Poll is the default. So I wanted to know what everybody thinks about switching the default to Wait.

I will make a follow-up to try and improve the documentation around our recommendations on which ControlFlow to choose, but wanted to keep it separate.

@daxpedda daxpedda force-pushed the control-flow-wait-default branch from 39b80f0 to 40e2f9c Compare September 20, 2023 11:07
Copy link
Member

@kchibisov kchibisov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue we should consider is that initial iteration, should still be with Poll, however anything further is with the default behavior. The reason behind is that you technically have events, and the event is the StartCause, but with Wait you'll wait for the actual events coming from the user or the windowing system, which might not be desirable for the first iteration.

What we should do, is to just switch the user default, but we shouldn't change the way kick start of the loop works, since we always have an event (StartCause::Init) and we simply need a wakeup, which Wait internally in the backends might not deliver it.

I'd also suggest to emphasize on the Poll internally, for the first loop iteration, like it's done in Wayland/X11, so you have more predictable startup, which should be fine across the backends.

@@ -230,7 +230,7 @@ impl<T: 'static> EventLoop<T> {
// Reset the internal state for the loop as we start running to
// ensure consistent behaviour in case the loop runs and exits more
// than once.
self.set_control_flow(ControlFlow::Poll);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep the Poll here, I think motivation is to do the first loop iteration non-blocking, because if you see the code, it won't get reset anymore? We just need to change the user default, but if the code really hardcodes the poll it's likely only a one time action.

@daxpedda
Copy link
Member Author

I think the issue we should consider is that initial iteration, should still be with Poll, however anything further is with the default behavior. The reason behind is that you technically have events, and the event is the StartCause, but with Wait you'll wait for the actual events coming from the user or the windowing system, which might not be desirable for the first iteration.

I'm not following. If StartCause is there, shouldn't that kickstart the initial event loop? Or are you saying that some backends just don't work correctly if you start with ControlFlow::Wait?

@kchibisov
Copy link
Member

@daxpedda yes, that's what I'm saying that they could go to sleep in such cases for the first iteration, because the event by its nature, is synthetic, so it can act like it won't wake up the event loop. I think Wayland/X11 backends will need more tuning than what you've done, but there's no issue to keeping the Poll for the first iteration internally, since it makes the most sense.

In practice, this won't change anything for the end users, since the default polling is still Wait, so after the first iteration.

@daxpedda daxpedda force-pushed the control-flow-wait-default branch from 40e2f9c to 684f617 Compare September 20, 2023 11:28
@daxpedda
Copy link
Member Author

In practice, this won't change anything for the end users, since the default polling is still Wait, so after the first iteration.

I assume I still have to put it back to ControlFlow::default() after the first iteration?
Another question would be if the user uses EventLoopWindowTarget::control_flow() would they not see that the ControlFlow is Poll?

@kchibisov
Copy link
Member

I assume I still have to put it back to ControlFlow::default() after the first iteration?
Another question would be if the user uses EventLoopWindowTarget::control_flow() would they not see that the ControlFlow is Poll?

Maybe it's not clear what I suggested, but I'll clarify:

  1. When first iteration happens, the internal behavior is if the Poll was used or it could be something else, if the backend can guarantee that it'll properly wakeup for the first iteration. The change I've suggested is a conservative approach to that, since I know that it won't break anything internally in the backends.
  2. After the first iteration, the behavior set by the user is what should be used, so in
    our case, the EventLoopWindowTarget::control_flow(). The default is Wait, but we shouldn't use the ControlFlow::default() in the backends code, we should use the value used by the users, and when the backends requires special polling for a short period of time to remain correctness it should switch to it. But we must not use the ::default() instead of the Poll.
  3. Users always see the default as a Wait or what they set, if the backend changes the way it polls internally for correctness reasons, is up to the backend, it was like that even before.

What you confuse is the policy with the behavior, users are setting the polling policy, how the backend follows that policy is up to the backend. It could use different polling internally, but it should look like the Wait behavior for the end user.

@kchibisov
Copy link
Member

So, how the change should look is that you simply change the default on the ControlFlow, and then ensure that the backends don't rely on that default being Poll, like ios. Instead they should simply specify the Poll when they need it.

@daxpedda
Copy link
Member Author

After some IRC conversations we figured out that this was already an issue before this PR.
See #3107.

@daxpedda daxpedda requested a review from kchibisov September 20, 2023 13:13
@daxpedda daxpedda requested a review from kchibisov September 22, 2023 19:17
@daxpedda daxpedda merged commit 878d832 into rust-windowing:master Sep 22, 2023
kchibisov pushed a commit to kchibisov/winit that referenced this pull request Oct 17, 2023
kchibisov pushed a commit that referenced this pull request Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants