From 229f35093dcc6ea8e50760175a69812968f3456f Mon Sep 17 00:00:00 2001 From: Tim O'Farrell Date: Mon, 7 Jul 2025 09:58:46 -0600 Subject: [PATCH] perf: make EventStore cur_id a lazy calculated property (#9544) Co-authored-by: openhands --- openhands/events/event_store.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/openhands/events/event_store.py b/openhands/events/event_store.py index cbed3515f8..fccde45d40 100644 --- a/openhands/events/event_store.py +++ b/openhands/events/event_store.py @@ -49,12 +49,23 @@ class EventStore(EventStoreABC): sid: str file_store: FileStore user_id: str | None - cur_id: int = -1 # We fix this in post init if it is not specified cache_size: int = 25 + _cur_id: int | None = None # Private field to cache the calculated value - def __post_init__(self) -> None: - if self.cur_id >= 0: - return + @property + def cur_id(self) -> int: + """Lazy calculated property for the current event ID.""" + if self._cur_id is None: + self._cur_id = self._calculate_cur_id() + return self._cur_id + + @cur_id.setter + def cur_id(self, value: int) -> None: + """Setter for cur_id to allow updates.""" + self._cur_id = value + + def _calculate_cur_id(self) -> int: + """Calculate the current event ID based on file system content.""" events = [] try: events_dir = get_conversation_events_dir(self.sid, self.user_id) @@ -63,14 +74,15 @@ class EventStore(EventStoreABC): logger.debug(f'No events found for session {self.sid} at {events_dir}') if not events: - self.cur_id = 0 - return + return 0 # if we have events, we need to find the highest id to prepare for new events + max_id = -1 for event_str in events: id = self._get_id_from_filename(event_str) - if id >= self.cur_id: - self.cur_id = id + 1 + if id >= max_id: + max_id = id + return max_id + 1 def search_events( self,