Skip to content

Commit

Permalink
cache task.to_str_params() to speedup Task initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Hi-king committed Jan 1, 2025
1 parent ff42bf4 commit c6317dc
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions gokart/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit c6317dc

Please sign in to comment.