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

Speed up validate_entity_id #32137

Merged
merged 6 commits into from
Feb 24, 2020
Merged

Speed up validate_entity_id #32137

merged 6 commits into from
Feb 24, 2020

Conversation

balloob
Copy link
Member

@balloob balloob commented Feb 24, 2020

Breaking change

Proposed change

In #32128 @KapJI included a call tree that showed that validate entity ID has horrible performance. And we use that a lot 😱

So I did a quick attempt to rewrite it to use regex. Included a benchmark. On my 2.9 GHz Core i7 machine it goes from 12s to 0.6s.

However, regex is not 100% correct yet. So going to fix that soonish. Just wanted to open PR to get feedback if I am missing entity IDs that need to be valid.

<domain>.<object_id> can have underscores, but neither can start or end with it.

To run benchmark: hass --script benchmark valid_entity_id

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)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

# Example configuration.yaml

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.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

@balloob balloob requested a review from a team as a code owner February 24, 2020 08:01
@KapJI
Copy link
Member

KapJI commented Feb 24, 2020

Yes, slugify was very expensive call. Thanks for optimising this!

@KapJI
Copy link
Member

KapJI commented Feb 24, 2020

@frenck although your version looks nicer I'd like to point out that it's 11-12% slower than explicit original.

@frenck
Copy link
Member

frenck commented Feb 24, 2020

The original that was provided wasn't complete and didn't pass the tests...

Benchmarked with the provided benchmark in this PR, it drops from the original slug solution (10~ seconds on my machine) to a steady 0.48 sec.

While passing all tests (which has been extended with more invalid cases).

Things that the previous regex didn't do but this one does:

  • Make sure both domain & entity don't start or end with a _.
  • Make sure both domain & entity part don't start with a number.
  • Making a single character domain or entity valid (e.g., a.a)

@frenck
Copy link
Member

frenck commented Feb 24, 2020

So starting with a number 0-9 is valid apparently, as it is being used in our tests as well.

Furthermore, there is a test case needed with a double underscore.

Reference, the 0.86 release notes: https://www.home-assistant.io/blog/2019/01/23/release-86/

@codecov
Copy link

codecov bot commented Feb 24, 2020

Codecov Report

Merging #32137 into dev will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev   #32137      +/-   ##
==========================================
+ Coverage   94.74%   94.74%   +<.01%     
==========================================
  Files         767      767              
  Lines       55498    55504       +6     
==========================================
+ Hits        52579    52587       +8     
+ Misses       2919     2917       -2
Impacted Files Coverage Δ
...istant/components/homekit_controller/connection.py 92.73% <0%> (-0.09%) ⬇️
...stant/components/homekit_controller/config_flow.py 100% <0%> (ø) ⬆️
homeassistant/helpers/config_validation.py 96.01% <0%> (+0.09%) ⬆️
homeassistant/helpers/entity.py 97.61% <0%> (+0.34%) ⬆️
homeassistant/components/uk_transport/sensor.py 94.2% <0%> (+0.72%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e6625e...695be47. Read the comment docs.

@balloob balloob merged commit 07fa844 into dev Feb 24, 2020
@balloob balloob deleted the speed-up-validate-entity-id branch February 24, 2020 16:35
@KapJI KapJI mentioned this pull request Feb 24, 2020
20 tasks
@lock lock bot locked and limited conversation to collaborators Feb 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants