-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Optimize calculate_data and redact_data() #361
base: main
Are you sure you want to change the base?
Conversation
# Conflicts: # custom_components/bermuda/bermuda_device_scanner.py
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.
This is great stuff :D
I'm also thinking that we should:
-
in
redaction_list_update()
:- store a (errr... static local?) stamp of MONOTONIC_TIME(), and if the last one was more than... maybe several hours? Then re-initialise list so we don't end up iterating over fixes for addresses (and associated matches) that have long since been purged from the system (ie, when a user does two diags but quite some time apart). There's probably a trade-off between cache miss/size cost and regeneration-time.
- (maybe/or) schedule a job in hass to purge the redactions[] after a certain time, to free up the memory I assume they'd take up. Maybe it's not big enough to matter.
- never apply
.upper()
toaddress
(since we always normalise the address to lowercase) and alwayslower()
any non-known-lowered address when adding/searching keys in redactions.
-
Then in
redact_data()
we can remove the case-checking variants, and always look forredactions[thing.lower()]
/if thing.lower() in redactions
orif find in data.lower()
.
Also, maybe we can ditch the re.sub altogether and use python's string.replace() since it might be a lot faster and we're not using the power of regex at all, really. Notes should be added to the update_redactions info to explain that the keys are not regex, but lower() forms of literal strings to match.
Also...
This comment is an interesting idea that might apply to our use-case:
If the replacements do not change the string size, you can speed up things a lot by using bytearray / buffers or even mmap and modify the data in-place. (re.sub() and string.replace and endstring += line cause that a lot of memory is copyied around.)
@@ -384,12 +397,16 @@ def calculate_data(self): | |||
) | |||
# Discard the bogus reading by duplicating the last. | |||
self.hist_distance_by_interval.insert(0, self.hist_distance_by_interval[0]) |
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.
This has a bug, just reported in #362 - might be worth including in this PR as a quickie? Suggestion is un-tested.
self.hist_distance_by_interval.insert(0, self.hist_distance_by_interval[0]) | |
if len(self.hist_distance_by_interval) == 0: | |
self.hist_distance_by_interval = [ self.rssi_distance_raw ] | |
else: | |
self.hist_distance_by_interval.insert(0, self.hist_distance_by_interval[0]) |
Co-authored-by: Ashley Gittins <[email protected]>
|
Ready for another lookover @agittins Redactions are deleted every 8 hours. |
Just a heads-up that I've had to make some changes to get some bugfixes out, so I've rebased your PR against main, I hope I've done that right :-/ There might be some areas (particularly in coordinator) where I've made changes to main that might need careful merging - I'm not 100% sure, I'm still getting to grips with handling merge conflicts (only been mucking around with various VCSs since the early 2000's, can't go too hard on myself! 😅 ) Re the |
Your merge looks fine! Yeah there is probably a better way to handle it, but honestly this seems to be fine imo. I think the tests were just being annoying |
redact data likely needs more work. Gonna let my instance run for a while and then try it. With limited data it does show some pretty large improvements.
There were some merge conflicts for calculate_data and update_advertisement. So if you could just give those a extra lookover that would probably be good!