AudioMuse-AI icon indicating copy to clipboard operation
AudioMuse-AI copied to clipboard

Optimize hot path functions and remove dead code

Open Copilot opened this issue 2 months ago • 0 comments

Identified and fixed performance bottlenecks in frequently-called functions, plus removed unreachable code.

Changes

  • tasks/commons.py: Replace O(n) list.index() with O(1) dict lookup in score_vector

    # Before: O(n) per lookup
    mood_scores_for_vector[mood_labels_list.index(label)] = float(score_str)
    
    # After: O(1) per lookup
    mood_label_to_index = {label: idx for idx, label in enumerate(mood_labels_list)}
    mood_scores_for_vector[mood_label_to_index.get(label)] = float(score_str)
    
  • ai.py: Pre-compile regex patterns for clean_playlist_name (~37% faster)

    _REGEX_INVALID_CHARS = re.compile(r'[^a-zA-Z0-9\s\-\&\'!\.\,\?\(\)\[\]]')
    _REGEX_TRAILING_NUMBER = re.compile(r'\s\(\d+\)$')
    _REGEX_MULTIPLE_SPACES = re.compile(r'\s+')
    
  • tasks/path_manager.py: Eliminate redundant np.linalg.norm() calls in get_angular_distance (4→2 calls, ~37% faster)

  • app_chat.py: Remove 64 lines of unreachable code after early return in chat_playlist_api()

Original prompt

Identify and suggest improvements to slow or inefficient code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Nov 30 '25 18:11 Copilot