-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
events.py: enhance efficiency of clear() by eliminating full dictionary rebuild #33521
base: master
Are you sure you want to change the base?
Conversation
trigger-jenkins |
85f67ee
to
abffd51
Compare
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:
Initializing counters from EVENTS in the |
def clear(self) -> None: | ||
self.event_counters = {k: (v + 1 if k in self.events else 0) for k, v in self.event_counters.items()} | ||
# Get events no longer active |
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 looks immediately more confusing to read, anything else we can try? Why not one loop and an if?
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.
A more common and readable approach is to loop through event_counters and check if event_name is not in the events list. The code snippet would look like this:
for event_name in self.event_counters:
if event_name not in self.events:
self.event_counters[event_name] = 0
However, this approach is significantly less efficient than the version in this PR and might not offer much improvement over the master version. I'll consider if there's a better solution.
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.
I can't think of a better solution for now. I've improved the variable name and comments to make the code easier to understand. The logic is straightforward: maintain a list of previously active events, compare it with the current active events, and remove those that are no longer active.
This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity. |
abffd51
to
e1bcd22
Compare
This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity. |
e1bcd22
to
7506f17
Compare
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. |
7506f17
to
85a6b37
Compare
This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity. |
59f9e47
to
33d0e79
Compare
trigger-jenkins |
The current
clear()
method rebuilds the entire dictionary from the keys inEVENTS
on each call, resulting in inefficient performance. For example, inselfdrived,
clear() is among the top five most time-consuming operations, with its execution time just behindSelfdriveD.publish_selfdriveState(self, CS)
, which is unreasonable: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: