-
Notifications
You must be signed in to change notification settings - Fork 740
⚡ Bolt: Optimize RequestMetrics.to_dict() serialization #7036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ZeyuChen
wants to merge
1
commit into
develop
from
bolt/optimize-metrics-serialization-9575716354012606787
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 2025-03-02 - Optimize RequestMetrics to_dict() | ||
| **Learning:** dataclasses.asdict uses copy.deepcopy recursively. For high-frequency serialization paths (like RequestMetrics.to_dict, which is called on every request output), writing a custom loop over __dataclass_fields__ that directly assigns primitives, calls custom .to_dict() methods on nested dataclasses, and shallow-copies collections can dramatically improve performance (observed over 2x speedup in isolated micro-benchmarks). | ||
| **Action:** Always profile and consider manual mapping over __dataclass_fields__ when serializing deeply nested or frequently created dataclasses in high-throughput engines. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -897,7 +897,28 @@ def to_dict(self): | |
| """ | ||
| Convert the RequestMetrics object to a dictionary. | ||
| """ | ||
| return {k: v for k, v in asdict(self).items()} | ||
| import dataclasses | ||
| import copy | ||
|
|
||
| result = {} | ||
| for k in self.__dataclass_fields__: | ||
| v = getattr(self, k) | ||
| if type(v) in (int, float, str, bool, type(None)): | ||
| result[k] = v | ||
| elif dataclasses.is_dataclass(v): | ||
| if hasattr(v, "to_dict"): | ||
| result[k] = v.to_dict() | ||
| else: | ||
| result[k] = dataclasses.asdict(v) | ||
| elif isinstance(v, list): | ||
| # NOTE: this assumes lists do not contain nested dataclasses | ||
| result[k] = list(v) | ||
| elif isinstance(v, dict): | ||
| # NOTE: this assumes dicts do not contain nested dataclasses | ||
| result[k] = dict(v) | ||
| else: | ||
| result[k] = copy.deepcopy(v) | ||
| return result | ||
|
Comment on lines
+903
to
+921
|
||
|
|
||
| def record_recv_first_token(self): | ||
| cur_time = time.time() | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RequestMetrics.to_dict() 看起来是高频热路径,但这里在函数内部执行
import dataclasses/import copy(每次调用都会走一次 import 语句,虽然有缓存但仍有额外开销),与“优化序列化开销”的目标相冲突。建议把依赖提升到模块级(文件顶部)或至少缓存is_dataclass/asdict/deepcopy的局部引用,避免在每次序列化时触发 import 逻辑。