-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Decrease event loop latency by binding time.monotonic to loop.time directly #98288
Conversation
…rectly This is a small improvment to decrease event loop latency. While the goal is is to reduce Bluetooth connection time latency, everything using asyncio is a bit more responsive as a result.
It looks like async_fire_time_changed might be off by a microsecond. Will investigate more tomorrow |
Looks like it's always been a bit off but it worked before because the run time of the python code was long enough that time moved forward enough between when the time call was made and when the system call/vdso call was made. Now it's too fast and it can fail |
We need to add clock_resolution to fix async_fire_time_changed |
freezegun is also a problem since it patches time.monotonic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's give this a try. I would expect any issues to be mainly related to testing as you've experienced in this PR.
Do you think there is an opportunity to upstream this? Or would the JIT work in Py3.12 and beyond make this obsolete?
I've been trying to upstream things that have near 0% risk like the selector changes (all of them are in now). For us changing the test suite a bit usually doesn't cause too many downstream issues, but for cpython itself, it could be a lot bigger issue. This change likely has a much larger benefit for our use-cases vs the general asyncio user since we tend to read a lot (30000 is just what I see on my systems, I've seen users with 100000+ but I think they are outliers) of small packets (esphome, bluetooth, shelly, zeroconf, ssdp, etc). Also, I think the work in Py3.13+ (maybe Py3.14+ actually depending on how things shake out) will make it less impactful. |
thanks! |
I've been running this for a while with good results. Not expecting an side effects since its calling the exact same path under the hood without the python method call wrapper.
Proposed change
This is a small improvement to decrease event loop latency. While the original goal was to reduce Bluetooth connection time latency, everything using asyncio is a bit more responsive as a result.
base_events.py
is almost always the top file in profiles since_run_once
is there. This change eliminated at least 10% of the run time in that file, ~13.8% in when profiling as the time call is made upwards of ~30000 times per minute on systems with 4 bluetooth remotes/local adapters. The pythontime
method call that wrappedtime.monotonic
, is the most frequently called nonbuilt-in
method on all my production systems, and it was more expensive than the actual call totime.monotonic
sincetime.monotonic
is implemented as abuilt-in
/cext.Type of change
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: