From eb39bdd9592eaf13b5fc0afdaccc17cc5f28238c Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 4 Nov 2025 04:54:48 +0000 Subject: [PATCH] Optimize create_exception_from_response The optimized code achieves a **35% speedup** by eliminating the repeated creation of the `suggestions` dictionary on every function call. **Key optimization:** - **Moved the suggestions dictionary outside the function** as a module-level constant `_SUGGESTIONS` - **Eliminated dictionary recreation overhead** - the original code created a 13-entry dictionary with string values on every function call, which involved repeated memory allocation and object creation **Performance impact:** The line profiler shows the original code spent ~29% of its time (lines with dictionary entries) just creating the suggestions dictionary. The optimized version eliminates this entirely, reducing total runtime from 26.8ms to 15.8ms. **Why this works:** - Dictionary creation in Python involves memory allocation and hash table setup - String literals in dictionaries still need to be processed and stored as objects - Moving to a module-level constant means the dictionary is created once at import time rather than on every function call - The suggestion lookup `_SUGGESTIONS.get(status_code, "Please try again later")` is now just a single hash table lookup **Test case performance:** All test cases show consistent 17-40% speedups, with particularly strong gains for: - Bulk operations (36-40% faster) - where the dictionary recreation overhead compounds - Edge cases with unknown status codes (35-40% faster) - still benefit from eliminating dictionary creation - Basic single calls (17-35% faster) - benefit from removing the creation overhead This optimization is especially effective for applications that handle many HTTP errors or exceptions in tight loops. --- mem0/exceptions.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/mem0/exceptions.py b/mem0/exceptions.py index 56c2b54c32..8192bb3d12 100644 --- a/mem0/exceptions.py +++ b/mem0/exceptions.py @@ -30,6 +30,22 @@ from typing import Any, Dict, Optional +_SUGGESTIONS = { + 400: "Please check your request parameters and try again", + 401: "Please check your API key and authentication credentials", + 403: "You don't have permission to perform this operation", + 404: "The requested resource was not found", + 408: "Request timed out. Please try again", + 409: "Resource conflict. Please check your request", + 413: "Request too large. Please reduce the size of your request", + 422: "Invalid request data. Please check your input", + 429: "Rate limit exceeded. Please wait before making more requests", + 500: "Internal server error. Please try again later", + 502: "Service temporarily unavailable. Please try again later", + 503: "Service unavailable. Please try again later", + 504: "Gateway timeout. Please try again later", +} + class MemoryError(Exception): """Base exception for all memory-related errors. @@ -475,24 +491,7 @@ def create_exception_from_response( if not error_code: error_code = f"HTTP_{status_code}" - # Create appropriate suggestion based on status code - suggestions = { - 400: "Please check your request parameters and try again", - 401: "Please check your API key and authentication credentials", - 403: "You don't have permission to perform this operation", - 404: "The requested resource was not found", - 408: "Request timed out. Please try again", - 409: "Resource conflict. Please check your request", - 413: "Request too large. Please reduce the size of your request", - 422: "Invalid request data. Please check your input", - 429: "Rate limit exceeded. Please wait before making more requests", - 500: "Internal server error. Please try again later", - 502: "Service temporarily unavailable. Please try again later", - 503: "Service unavailable. Please try again later", - 504: "Gateway timeout. Please try again later", - } - - suggestion = suggestions.get(status_code, "Please try again later") + suggestion = _SUGGESTIONS.get(status_code, "Please try again later") return exception_class( message=response_text or f"HTTP {status_code} error",