-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cache task.to_str_params() to speedup Task initialization #421
base: master
Are you sure you want to change the base?
Conversation
Nice contribution! |
b289714
to
a6c8ade
Compare
@hiro-o918 Thanks! I've fixed the CI errors and also added some tests! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put just a nits comment
Co-authored-by: hirosassa <hiro.sassa@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
gokart/task.py
Outdated
@@ -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 and (not only_public): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why this condition is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kitagry This intend to cache only when called from init.
There're two solutions
- only cache default parameters: simple impl. , but specific use
- cache all patterns of parameters: general use, but a little bit comprecated impl.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to name the condition for your intention. For example
init_condition = only_significant and (not only_public)
if !init_condition:
return super().to_str_params(only_significant, only_public)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kitagry Thanks! I've introduced _called_with_default_args
:)
gokart/task.py
Outdated
@@ -373,6 +374,17 @@ 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) -> dict[str, str]: | |||
_called_with_default_args: bool = only_public and (not only_significant) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ask] default only_public is False. Is it correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe?
_called_with_default_args: bool = only_public and (not only_significant) | |
_called_with_default_args: bool = (not only_public) and (not only_significant) |
@kitagry @hiro-o918 @hirosassa After discussing with @kitagry, we concluded that the code's intent was somewhat unclear. Therefore, we simplified it by switching to simply using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks clear and simple. I agree with this change.
Summary:
During the initialization process of gokart.TaskOnKart, especially with deeply nested TaskInstanceParameter, the repeated calls to to_str_params() caused significant performance degradation. To address this issue, we introduced a caching mechanism for function calls, which has significantly accelerated the initialization process.
Detailed Behavior:
The following actions occur during initialization:
Changes:
Implemented a caching mechanism for the to_str_params() function.
Cached the string representations of parameters once computed to prevent recalculations for the same parameters.
This change substantially reduces the time complexity from O(n^2), thereby shortening the initialization time.
Impact:
This modification affects all tasks inheriting from gokart.TaskOnKart, particularly those with complex dependencies, improving the initialization performance significantly.
How to Reproduce
Prior to this change, this sample code took 21.9 seconds to execute. With the new caching mechanism, it now takes only 1.15 seconds, marking a significant improvement in performance.
TBA
need some tests