Optimize hot path functions and remove dead code
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 inscore_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 forclean_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 redundantnp.linalg.norm()calls inget_angular_distance(4→2 calls, ~37% faster) -
app_chat.py: Remove 64 lines of unreachable code after early return inchat_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.