From c6317dcca890cb55ed95234a0134837189ff1e3e Mon Sep 17 00:00:00 2001 From: Keisuke OGAKI Date: Fri, 27 Dec 2024 18:03:24 +0900 Subject: [PATCH] cache task.to_str_params() to speedup Task initialization --- gokart/task.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gokart/task.py b/gokart/task.py index a7f7a06d..43562f64 100644 --- a/gokart/task.py +++ b/gokart/task.py @@ -117,6 +117,7 @@ def __init__(self, *args, **kwargs): super(TaskOnKart, self).__init__(*args, **kwargs) self._rerun_state = self.rerun self._lock_at_dump = True + self._str_params_cache = None if self.complete_check_at_run: self.run = task_complete_check_wrapper(run_func=self.run, complete_check_func=self.complete) # type: ignore @@ -373,6 +374,18 @@ def make_unique_id(self) -> str: self.task_unique_id = unique_id return unique_id + def to_str_params(self, only_significant=False, only_public=False): + if only_significant == True and only_public == False: + # cache to_str_params to avoid too slow task creation of deep task tree + # e.g. gokart.build(RecursiveTask(dep=RecursiveTask(dep=RecursiveTask(dep=HelloWorldTask())))) takes O(n*n) to_str_params calls + if self._str_params_cache is not None: + return self._str_params_cache + else: + self._str_params_cache = super().to_str_params(only_significant, only_public) + return self._str_params_cache + else: + return super().to_str_params(only_significant, only_public) + def _make_hash_id(self) -> str: def _to_str_params(task): if isinstance(task, TaskOnKart):