Improve performance of StreamSelectLoop
when no timers are scheduled
#246
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.
This simple changeset improves performance of the
StreamSelectLoop
when no timers are scheduled. This can be reproduced by running the included benchmarks (#100):In a very busy loop constantly using
futureTick()
like this example, this shows a significant improvement. For many "normal" workloads that have any reasonable stream activity, this improvement is expected to be negligible. At the very least, this changeset is expected to offset any negative impact #245 may show, but in some cases you may even be able to see a significant improvement.The idea behind this PR is to avoid any unnecessary system calls as these incur a significant overhead. In the previous version, you would constantly see a bunch of system calls here:
In the updated version, these system calls are no longer issued. Once any timers are scheduled, this system call is issued as usual (which is why this has little effect on many real-world workloads). The test suite confirms this has full code coverage.
This benchmark is interesting because it actually skips any system calls entirely (as of #93). If the same benchmark is updated to add an idle stream, the loop has to invoke a
select()
system call on each tick at least. In this case, the benchmark improved from2.6s
to1.3s
on my machine.As a worst-case scenario, you can also execute
time php examples/94-benchmark-timers-delay.php
which constantly schedules a single timer for the next loop iteration. With or without my changes, this shows7.1s
on my machine, so this confirms my changeset does not negatively impact performance.Builds on top of #245, #183, #182, #93, and others