Skip to content

perf: Refine the Model Manager code #3094

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

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

@staticmethod
def get_model(_id, get_model):
model_instance = ModelManage.cache.get(_id)
if model_instance is None:
with _lock:
lock = ModelManage._get_lock(_id)
with lock:
model_instance = ModelManage.cache.get(_id)
if model_instance is None:
model_instance = get_model(_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.

Your code contains some potential improvements and fixes:

  1. Memory Locks Management: The current implementation uses a locks dictionary to store per-lock instances. However, you can simplify it using a nested locking mechanism within the _get_lock method.

  2. Concurrency Issues: Using a global lock (_lock) for managing memory locks might lead to contention. Instead, consider using a thread local lock or using Python's built-in synchronization primitives more effectively.

  3. Caching Logic: The caching logic is already optimized to avoid unnecessary computations and redundant requests. This part seems correct, but ensure that the MemCache object correctly handles invalidation times (e.g., setting keys expiring after a certain period).

  4. Synchronization: Ensure that all operations involving modifying the cache dictionary are protected by appropriate locks. Currently, only read operations use locking, which is fine.

Here is an improved version of your code:

from common.cache.mem_cache import MemCache
import threading

_lock = threading.Lock()

class ModelManage:
    cache = MemCache('model', {})
    up_clear_time = time.time()

    @staticmethod
    def get_model(_id, get_model):
        with _lock:
            # First try to retrieve from cache
            model_instance = ModelManage.cache.get(_id)
            
            if model_instance is None:
                # If not in cache, fetch from outside source and put into cache
                model_instance = get_model(_id)
                
                # Put the fetched model instance into cache
                ModelManage.cache.set(_id, model_instance)

        return model_instance

Key Improvements:

  • Removed the locks dictionary since each lock operation will manage its own context within the _get_lock method.
  • Simplified the locking strategy inside ModelManage.get_model.
  • Ensured consistent usage of with _lock: to handle exceptions properly during critical sections of code.

This should improve efficiency and reduce concurrency risks while maintaining simplicity in the design.

@shaohuzhang1 shaohuzhang1 merged commit b2bf697 into main May 15, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@perf_model_manage branch May 15, 2025 14:47
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