Identify and eliminate redundant or duplicate computation paths in your code, especially for expensive operations like schema generation, type resolution, or recursive traversals. This improves performance and reduces resource usage.
Identify and eliminate redundant or duplicate computation paths in your code, especially for expensive operations like schema generation, type resolution, or recursive traversals. This improves performance and reduces resource usage.
Key practices to follow:
Example of problematic code:
def _display_complex_type(obj: Any) -> str:
# Expensive recursive operation called frequently
if get_origin(obj):
args = get_args(obj)
# Recursive processing of each argument...
# ...
# Called in multiple places
repr_args = _display_complex_type(type_) # Called repeatedly for same types
Improved approach:
@cached_property # Or use functools.lru_cache
def _validator(self) -> SchemaValidator:
"""Cache validator to avoid rebuilding for each validation"""
if not self._core_schema:
self._build_schema() # Build only once
return SchemaValidator(self._core_schema)
For frequently used utility functions, consider specialized implementations that avoid repeated work or use more efficient algorithms for common cases.
Enter the URL of a public GitHub repository