Skip to content

perf: Refine the Model Manager code#3091

Merged
shaohuzhang1 merged 1 commit intomainfrom
pr@main@perf_model_manage
May 15, 2025
Merged

perf: Refine the Model Manager code#3091
shaohuzhang1 merged 1 commit intomainfrom
pr@main@perf_model_manage

Conversation

@shaohuzhang1
Copy link
Copy Markdown
Contributor

perf: Refine the Model Manager code

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented May 15, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented May 15, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@shaohuzhang1 shaohuzhang1 merged commit 1db8577 into main May 15, 2025
4 checks passed
return model_instance

@staticmethod
def clear_timeout_cache():
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code seems correct and efficient. Here are a few minor improvements and notes:

  1. Return Value: The function get_model should have only one return point without unnecessary nested conditions after the caching logic.

  2. Consistent Code Blocks: Ensure consistent indentation throughout the code block to improve readability.

  3. Variable Naming: Variable names like _lock, _id, model_instance, and others could be more descriptive for better understanding of their purpose within the context of the function.

  4. Performance Considerations: Since you're setting timeouts for cachable instances but not retrieving them again before they expire, this pattern is efficient for ensuring hot data remains cached until it's no longer needed due to updates or expiration.

Here’s an optimized version of the code with these considerations:

@@ -25,16 +25,13 @@ def get_model(_id, get_model):
             with _lock:
                 model_instance = get_model(_id)
                 ModelManage.cache.set(_id, model_instance, timeout=8 * 60 * 60)  # Set a fixed duration rather than using another method call
-                ModelManage.clear_timeout_cache()  # Only clear cache when a new instance is fetched
         else:
             if model_instance.is_cache_model():
                 ModelManage.cache.touch(_id, timeout=8 * 60 * 60)  # Extend the cache lifetime of existing cached models
             else:
                 model_instance = get_model(_id)
                 ModelManage.cache.set(_id, model_instance, timeout=8 * 60 * 60)  # Always set new instances with a fixed timeout

     @staticmethod
     def clear_timeout_cache(*args, **kwargs):  # Adjusting parameters based on expected usage
     ```

These changes simplify the logic and ensure that resources are used efficiently while maintaining clarity and simplicity in the codebase.

@shaohuzhang1 shaohuzhang1 deleted the pr@main@perf_model_manage branch May 15, 2025 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant