events.py: enhance efficiency of clear() by eliminating full dictionary rebuild
The current clear() method rebuilds the entire dictionary from the keys in EVENTS on each call, resulting in inefficient performance. For example, in selfdrived, clear() is among the top five most time-consuming operations, with its execution time just behind SelfdriveD.publish_selfdriveState(self, CS), which is unreasonable:
5000 0.192 0.000 0.467 0.000 selfdrived.py:415(publish_selfdriveState)
5000 0.042 0.000 0.324 0.000 events.py:68(clear)
After this PR, the execution time of clear() has been reduced from 0.324 to 0.0311, making it approximately 10 times faster. This improvement should enhance the runtime efficiency of both selfdrived and card.
Changes:
- Added self.prev_event_set to track previously active events.
- Refactored clear() to use set operations for efficient counter reset instead of rebuilding the entire dictionary.
- Updated add() method to increment event counters directly.
trigger-jenkins
Using defaultdict in the Events class is more efficient, especially since Events() may be instantiated frequently, such as in functions like create_common_events(). For example, the Events instance is created on every loop in card:
ncalls tottime percall cumtime percall filename:lineno(function)
4999 0.017 0.000 0.077 0.000 events.py:51(__init__)
Initializing counters from EVENTS in the __init__ method provides no real benefit and results in unnecessary CPU usage. Switching to defaultdict streamlines the process, reducing overhead by eliminating unnecessary initialization steps.
This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.
This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.
This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.
This PR has been automatically closed due to inactivity. Feel free to re-open once activity resumes.
This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.
trigger-jenkins
This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.
This PR has been automatically closed due to inactivity. Feel free to re-open once activity resumes.