unix,win: fix busy loop with zero timeout timers#4250
Merged
bnoordhuis merged 3 commits intolibuv:v1.xfrom Dec 22, 2023
Merged
Conversation
vtjnash
reviewed
Dec 15, 2023
Member
vtjnash
left a comment
There was a problem hiding this comment.
Without fully reviewing the implementation, this approach SGTM
bnoordhuis
reviewed
Dec 15, 2023
Member
bnoordhuis
left a comment
There was a problem hiding this comment.
Can you check how this affects the million_timers benchmark?
As a possible improvement: instead of a flag, always uv__queue_init() when the handle is stopped. That simplifies the uv_timer_stop() logic to:
if (uv__is_active(handle)) {
heap_remove(timer_heap(handle->loop), &handle->u.heap_node, timer_less_than);
uv__handle_stop(handle);
} else {
uv__queue_remove(&handle->u.queue_node);
}
uv__queue_init(&handle->u.queue_node);With complementary logic in uv_timer_start().
Calling `uv_timer_start(h, cb, 0, 0)` from a timer callback resulted in the timer running immediately because it was inserted at the front of the timer heap. If the callback did that every time, libuv would effectively busy-loop in `uv__run_timers()` and never make forward progress. Work around that by collecting all expired timers into a queue and only running their callback afterwards. Fixes: libuv#4245
7336e87 to
3a7cb0d
Compare
Contributor
Author
For that benchmark, there is essentially no difference. Follows sample output for one run, and average of 10 runs computed with hyperfine: Old: New: Tests run on Windows 11 system with |
Member
|
Thanks Matheus, merged. |
Closed
santigimeno
added a commit
to santigimeno/libuv
that referenced
this pull request
Feb 6, 2024
This reverts commit 51a22f6.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Calling
uv_timer_start(h, cb, 0, 0)from a timer callback resulted inthe timer running immediately because it was inserted at the front of
the timer heap.
If the callback did that every time, libuv would effectively busy-loop
in
uv__run_timers()and never make forward progress.Work around that by collecting all expired timers into a queue and only
running their callback afterwards.
Fixes: #4245