mirror of
https://github.com/Alishahryar1/free-claude-code.git
synced 2026-05-20 00:56:31 +00:00
⚡ Bolt: Optimize get_block_attr by removing hasattr overhead
Refactors `get_block_attr` to remove the use of `hasattr()`, replacing it with `isinstance(dict)` check first and using `getattr(block, attr, default)` to avoid double-evaluation overhead and prevent edge cases where dictionaries might return built-in methods instead of intended keys. Co-authored-by: shreyashjagtap157 <38904253+shreyashjagtap157@users.noreply.github.com>
This commit is contained in:
parent
06e6adb5bb
commit
45de088e38
2 changed files with 5 additions and 3 deletions
|
|
@ -14,3 +14,7 @@
|
|||
1. Increased default HTTP_READ_TIMEOUT to 600s across all providers.
|
||||
2. Modified GlobalRateLimiter to treat httpx.TimeoutException and openai.APITimeoutError as retryable events (Status 408).
|
||||
3. Increased SDK max_retries to 2 in OpenAIChatTransport to handle transient connection resets.
|
||||
|
||||
## 2024-05-25 - Avoid hasattr() Overheads in High-Frequency Python Loops
|
||||
**Learning:** In high-frequency content block parsing logic for APIs, `hasattr()` is computationally expensive because it catches and hides internal `AttributeError` exceptions inside the CPython interpreter runtime. Also, if a dictionary has a key that matches a built-in dict method name (like `get` or `keys`), `hasattr()` evaluates to `True` leading to method object extraction instead of key retrieval.
|
||||
**Action:** When working with mixed dict/object types in payload schemas, explicitly isolate dict behaviors using `isinstance(obj, dict)` initially, then fall back to direct `getattr(obj, attr, default)` calls to avoid double-lookups and exception silencing overheads.
|
||||
|
|
|
|||
|
|
@ -5,11 +5,9 @@ from typing import Any
|
|||
|
||||
def get_block_attr(block: Any, attr: str, default: Any = None) -> Any:
|
||||
"""Get an attribute from a Pydantic model, lightweight object, or dict."""
|
||||
if hasattr(block, attr):
|
||||
return getattr(block, attr)
|
||||
if isinstance(block, dict):
|
||||
return block.get(attr, default)
|
||||
return default
|
||||
return getattr(block, attr, default)
|
||||
|
||||
|
||||
def get_block_type(block: Any) -> str | None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue