openpilot icon indicating copy to clipboard operation
openpilot copied to clipboard

events.py: enhance efficiency of clear() by eliminating full dictionary rebuild

Open deanlee opened this issue 1 year ago • 8 comments

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:

  1. Added self.prev_event_set to track previously active events.
  2. Refactored clear() to use set operations for efficient counter reset instead of rebuilding the entire dictionary.
  3. Updated add() method to increment event counters directly.

deanlee avatar Sep 09 '24 17:09 deanlee

trigger-jenkins

sshane avatar Sep 09 '24 19:09 sshane

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.

deanlee avatar Sep 10 '24 19:09 deanlee

This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.

github-actions[bot] avatar Sep 20 '24 01:09 github-actions[bot]

This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.

github-actions[bot] avatar Sep 30 '24 02:09 github-actions[bot]

This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.

github-actions[bot] avatar Oct 11 '24 01:10 github-actions[bot]

This PR has been automatically closed due to inactivity. Feel free to re-open once activity resumes.

github-actions[bot] avatar Oct 13 '24 02:10 github-actions[bot]

This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.

github-actions[bot] avatar Oct 24 '24 02:10 github-actions[bot]

trigger-jenkins

adeebshihadeh avatar Oct 26 '24 21:10 adeebshihadeh

This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.

github-actions[bot] avatar Nov 06 '24 01:11 github-actions[bot]

This PR has been automatically closed due to inactivity. Feel free to re-open once activity resumes.

github-actions[bot] avatar Nov 08 '24 01:11 github-actions[bot]