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
Make unit converter use a factory to avoid looking up the ratios each conversion #93706
Conversation
With statistics we are doing the same conversion over and over and we have do do lookups every time to do the convert. Since its the same to/from unit over and over we can use a factory pattern to avoid all the lookups
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
LRU stats after 1 minute 2023-05-28 15:55:08.674 CRITICAL (SyncWorker_16) [homeassistant.components.profiler] Cache stats for lru_cache <function BaseUnitConverter.converter_factory at 0x7fc6cf1b7e20> at /usr/src/homeassistant/homeassistant/util/unit_conversion.py: CacheInfo(hits=105, misses=7, maxsize=128, currsize=7) |
LRU stats from another production system after 2 minutes
|
Tests for gdacs are failing because they aren't using pytest.approx for the float conversion We could do two operations to reduce the floating point conversion loss but that might be a bit overkill to preserve decimal places beyond the actual precision |
refactored to retain the exact same floating point conversion behavior |
stats profile looks great as well. also gone from there. |
lru stats look excellent
|
Test failure is unrelated |
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.
LGTM, thanks @bdraco 👍
../Frenck
Thanks. This one will make quite a difference for some use cases where the display and stored units differ |
Proposed change
With statistics we are doing the same conversion for each data point, and we currently have to do lookups every time to do the convert. Since its the same to/from unit over and over we can use a factory pattern to avoid all but the first set of lookups (unless its already in the LRU).
I added LRUs to avoid building the converters over and over since we also use this in sensor to convert units. It most cases we end up with ~20-30 tiny cached converter functions which uses less memory than most of our python modules.
After this change, all the unit conversion no longer show in all the statistics profiling because they are too small to be included. The flame on 1939 no longer has a sub-flame for the conversion:
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: