Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2025-03-11 - Optimize dataclass serialization with __slots__
**Learning:** `dataclasses.asdict` can be a significant performance bottleneck when dealing with high-frequency serialization of dataclasses, especially nested ones. For dataclasses that use `slots=True` (like `RequestMetrics`), iterating over `__slots__` and using `getattr` to manually construct the dictionary is significantly faster than using `asdict`. Nested dataclasses can still be supported efficiently by dynamically checking for `hasattr(v, '__dataclass_fields__')` and falling back to `asdict(v)` only for the nested components.
**Action:** When optimizing frequent dataclass serialization in Python, check if the dataclass uses `slots=True` and prefer a custom `to_dict` method using `__slots__` iteration over `dataclasses.asdict()`.
5 changes: 4 additions & 1 deletion fastdeploy/engine/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,10 @@ def to_dict(self):
"""
Convert the RequestMetrics object to a dictionary.
"""
return {k: v for k, v in asdict(self).items()}
# Optimized: Avoiding dataclasses.asdict() because its recursive deepcopy
# and validation overhead is a significant bottleneck for high-frequency calls.
# Iterating over __slots__ is significantly faster for flat structs.
return {k: getattr(self, k) for k in self.__slots__}

def record_recv_first_token(self):
cur_time = time.time()
Expand Down
Loading