Improve performance, add internal Clock
, reuse clock in same tick
#457
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 PR improves the HTTP server performance by adding an internal
Clock
instance that reuses the system clock within the same loop tick. This helps avoiding unneededgettimeofday()
syscalls that show a noticeable performance impact especially during benchmark runs (see also #455).The new internal
Clock
class is mostly used as an internal optimization to avoid unneeded syscalls to get the current system time multiple times within the same loop tick. For the purpose of the HTTP server, the clock is assumed to not change to a significant degree within the same loop tick. If you need a high precision clock source, you may want to use\hrtime()
instead (PHP 7.3+).The API is modelled to resemble the PSR-20
ClockInterface
(in draft at the time of writing this), but uses afloat
return value for performance reasons instead.For a single HTTP request, this means the following syscalls can be eliminated:
This is especially noticeable during a benchmark run. In particular, the HTTP server may handle multiple concurrent connections in a single loop tick, so even the first
gettimeofday()
call above will only be invoked once per tick. Runningdocker run -it --rm --net=host jordi/ab -n1000000 -c50 -k http://localhost:8080/
against the example HTTP server suggests results improved from 19188 req/s to 20318 req/s on my machine (best of 5 each).This changeset only refactors some internal logic and does not affect the public API, so it should be safe to apply. The test suite confirms this has 100% code coverage.
Refs #455 and reactphp/event-loop#246