Skip to content

Commit

Permalink
Fix memory leak from shared_description cache, fixes #163
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMakesGames committed Dec 15, 2024
1 parent 974665a commit 7a5e99e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions stellarisdashboard/parsing/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
def dump_name(name: dict):
return json.dumps(name, sort_keys=True)

@cache
def _get_or_add_shared_description(session, text: str) -> datamodel.SharedDescription:
matching_description = (
session.query(datamodel.SharedDescription)
.filter_by(text=text)
.one_or_none()
)
if matching_description is None:
matching_description = datamodel.SharedDescription(text=text)
session.add(matching_description)
return matching_description

@dataclasses.dataclass
class BasicGameInfo:
Expand Down Expand Up @@ -72,6 +83,7 @@ def process_gamestate(self, game_id: str, gamestate_dict: Dict[str, Any]):
)
if config.CONFIG.debug_mode or isinstance(e, KeyboardInterrupt):
raise e
_get_or_add_shared_description.cache_clear()

def _check_if_gamestate_exists(self, db_game):
existing_dates = {gs.date for gs in db_game.game_states}
Expand Down Expand Up @@ -251,17 +263,8 @@ def data(self) -> Any:
def extract_data_from_gamestate(self, dependencies: Dict[str, Any]):
pass

@cache
def _get_or_add_shared_description(self, text: str) -> datamodel.SharedDescription:
matching_description = (
self._session.query(datamodel.SharedDescription)
.filter_by(text=text)
.one_or_none()
)
if matching_description is None:
matching_description = datamodel.SharedDescription(text=text)
self._session.add(matching_description)
return matching_description
_get_or_add_shared_description(self._session, text)


class SystemProcessor(AbstractGamestateDataProcessor):
Expand Down

0 comments on commit 7a5e99e

Please sign in to comment.