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

Improve performance of StreamSelectLoop when no timers are scheduled #246

Merged
merged 1 commit into from
Feb 23, 2022

Conversation

clue
Copy link
Member

@clue clue commented Feb 23, 2022

This simple changeset improves performance of the StreamSelectLoop when no timers are scheduled. This can be reproduced by running the included benchmarks (#100):

me@me-mate:~/workspace/reactphp-event-loop$ time php examples/93-benchmark-ticks-delay.php 1000000
// old: 1.8s
// new: 0.5s

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:

me@me-mate:~/workspace/reactphp-event-loop$ strace php examples/93-benchmark-ticks-delay.php 1000000
[…]
clock_gettime(CLOCK_MONOTONIC, {tv_sec=1834904, tv_nsec=318373419}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=1834904, tv_nsec=318397235}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=1834904, tv_nsec=318421121}) = 0
// repeated 1 Mio times for old version
[…]

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 from 2.6s to 1.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 shows 7.1s on my machine, so this confirms my changeset does not negatively impact performance.

Builds on top of #245, #183, #182, #93, and others

@clue clue added this to the v1.3.0 milestone Feb 23, 2022
@WyriHaximus WyriHaximus merged commit cd3f42b into reactphp:master Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants