diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000000..1648cdedbd0 --- /dev/null +++ b/.jules/bolt.md @@ -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()`. diff --git a/fastdeploy/engine/request.py b/fastdeploy/engine/request.py index 1e2a53ed205..21a67e72fb2 100644 --- a/fastdeploy/engine/request.py +++ b/fastdeploy/engine/request.py @@ -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()