Skip to content

Small speed up to frequently called datetime functions#85399

Merged
bdraco merged 8 commits into
home-assistant:devfrom
bdraco:dt_speedups
Jan 8, 2023
Merged

Small speed up to frequently called datetime functions#85399
bdraco merged 8 commits into
home-assistant:devfrom
bdraco:dt_speedups

Conversation

@bdraco
Copy link
Copy Markdown
Member

@bdraco bdraco commented Jan 7, 2023

Proposed change

We call time functions as much as 10000-20000 times per minute so any savings here makes a difference with latency (these are mostly in the bluetooth advertisement path).

We can reduce some python overhead and make functions with static arguments ~30% faster by wrapping them as a partial which avoids the overhead of constructing the underlying call every time since it will only be done once

https://github.com/python/cpython/blob/4b4e6da7b53f9eb2d3654d321f6acfbd599bd990/Modules/_functoolsmodule.c#L286

% cat timer.py
import timeit

print(timeit.timeit("utcnow()", setup="from tester import utcnow"))
print(timeit.timeit("utcnow_partial()", setup="from tester import utcnow_partial"))
% python3 timer.py
0.207963207969442
0.14283533301204443
% cat tester.py
from functools import partial
import datetime as dt

UTC = dt.timezone.utc

def utcnow() -> dt.datetime:
    """Get now in UTC time."""
    return dt.datetime.now(UTC)

utcnow_partial = partial(dt.datetime.now, UTC)

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

We can reduce some python overhead and make functions with
static arguments ~30% faster by wrapping them as a partial
which avoids the overhead of constructing the underlying
call every time since it will only be done once

https://github.com/python/cpython/blob/4b4e6da7b53f9eb2d3654d321f6acfbd599bd990/Modules/_functoolsmodule.c#L286

```
% cat timer.py
import timeit

print(timeit.timeit("utcnow()", setup="from tester import utcnow"))
print(timeit.timeit("utcnow_partial()", setup="from tester import utcnow_partial"))
% python3 timer.py
0.207963207969442
0.14283533301204443
% cat tester.py
from functools import partial
import datetime as dt

UTC = dt.timezone.utc

def utcnow() -> dt.datetime:
    """Get now in UTC time."""
    return dt.datetime.now(UTC)

utcnow_partial = partial(dt.datetime.now, UTC)
```
@home-assistant home-assistant Bot added cla-signed code-quality core small-pr PRs with less than 30 lines. labels Jan 7, 2023
@bdraco bdraco closed this Jan 7, 2023
@bdraco bdraco reopened this Jan 7, 2023
@bdraco
Copy link
Copy Markdown
Member Author

bdraco commented Jan 8, 2023

Its hitting the event rearm
DEBUG:homeassistant.helpers.event:Called 161.467664 seconds too early, rearming

@bdraco bdraco marked this pull request as ready for review January 8, 2023 03:08
@bdraco bdraco requested review from a team and epenet as code owners January 8, 2023 03:08
Comment thread tests/conftest.py
@bdraco
Copy link
Copy Markdown
Member Author

bdraco commented Jan 8, 2023

Thanks

@bdraco bdraco merged commit d81febd into home-assistant:dev Jan 8, 2023
@bdraco bdraco deleted the dt_speedups branch January 8, 2023 19:42
shbatm pushed a commit to shbatm/home-assistant-core that referenced this pull request Jan 9, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants