Skip to content

perf: Refine the Model Manager code #3089

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

Merged
merged 1 commit into from
May 15, 2025
Merged

Conversation

shaohuzhang1
Copy link
Contributor

perf: Refine the Model Manager code

@shaohuzhang1 shaohuzhang1 merged commit 949e4de into main May 15, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@perf_model_manage branch May 15, 2025 13:22
ModelManage.cache.clear_timeout_data()
if time.time() - ModelManage.up_clear_time > 60 * 60:
threading.Thread(target=lambda: ModelManage.cache.clear_timeout_data()).start()
ModelManage.up_clear_time = time.time()

@staticmethod
def delete_key(_id):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here are some optimizations and improvements:

  1. Use try-with-resources style context managers to automatically release locks:
 with _lock:
  1. Simplify cache set logic by directly using the lock within it:

    def get_model(self, _id, get_model):
        model_instance = self.cache.get(_id)
        if not model_instance or not model_instance.is_cache_model():
            with _lock:
                model_instance = get_model(_id)
                self.cache.set(
                    _id,
                    model_instance,
                    timeout=60 * 3600,  # Increased from 30 minutes to an hour
                )
                self.clear_timeout_cache()
                return model_instance
        elif model_instance.is_cache_model():
            self.cache.touch(_id, timeout=60 * 3600)  # Increased from 30 minutes to an hour
            return model_instance
        else:
            self.delete_key(_id)
            return get_model(_id)
  2. Move the cache clearing logic into its own method called clear_expired_keys, so that you can call it explicitly when needed without modifying get_model.

  3. Ensure that ModelManage.up_clear_time is properly initialized before being used.

  4. The time.sleep(3) in your original code was removed because there's no reason for sleeping between each request. If sleep is required somewhere else, ensure that it doesn't interfere with performance or functionality of this critical service.

This optimized version maintains the necessary logic while improving readability and efficiency. Always test thoroughly after making changes to ensure stability!

Copy link

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.

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.

Copy link

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.

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

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