Skip to content

perf: Refine the Model Manager code#3093

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

perf: Refine the Model Manager code#3093
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 8cf66b9 into main May 15, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@perf_model_manage branch May 15, 2025 14:38
ModelManage.cache.set(_id, model_instance, timeout=60 * 60 * 8)
else:
if model_instance.is_cache_model():
ModelManage.cache.touch(_id, timeout=60 * 60 * 8)
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 appears to be intended to optimize the retrieval of models from a cache while providing fallback functionality if the model is not found or invalid. However, there are a few potential improvements:

  1. Avoid Unnecessary Checks: The second if statement checks both whether the model instance should be considered and its validity. It’s usually more efficient to first retrieve and verify the existence before touching the TTL.

  2. Lock Management: Ensure that _lock is properly initialized and managed throughout the function to avoid concurrency issues.

  3. Cache Key Naming Consistency: Use consistent naming conventions for caching keys.

Here's an optimized version of the code:

from concurrent.futures import ThreadPoolExecutor

class ModelManage:
    # Assuming this is where lock initialization might happen
    _lock = threading.Lock()

def get_model(_id, get_model):
    with ThreadPoolExecutor() as executor:
        result = executor.submit(get_model, _id)

        try:
            response = result.result(timeout=60)  # Timeout after 60 seconds
        except futures.TimeoutError:
            raise Exception(f"Model retrieval took longer than expected for {_id}")

    model_instance = ModelManage.cache.get(_id)
    
    if model_instance is None:
        model_instance = response
        ModelManage.cache.set(_id, model_instance, timeout=60 * 60 * 8)
        
        if not model_instance.should_be_cached():
            print("Warning: Retrieved model does not seem cacheable.")
            
    elif model_instance.is_valid():
        ModelManage.cache.touch(_id, timeout=60 * 60 * 8)
        print(f"Model {model_id} renewed.")

    return model_instance

Key Improvements:

  • ThreadPoolExecutor: Used for async calls to get_model, which can improve performance by allowing other operations to proceed without waiting.
  • Timeout Handling: Added a timeout to prevent hanging indefinitely on result.
  • Improved Condition Check Logic: First tries to update the cache only when necessary, reducing overhead and ensuring consistency.

Note:

  • This assumes that _lock is correctly defined elsewhere in your implementation.
  • Consider adding error handling for actual exceptions thrown during model retrieval.
  • Adjust should_be_cached() method according to how your validation logic determines eligibility for caching.

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