-
Notifications
You must be signed in to change notification settings - Fork 735
⚡ Bolt: Optimize RequestMetrics serialization #7137
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-request-metrics-serialization-15896084233915345816
+44
−1
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-04-01 - Fast Serialization for Metrics | ||
| **Learning:** `dataclasses.asdict()` relies on recursive deep cloning internally, making it extremely slow for high-frequency operations like serializing metrics per request/token. Shallow iterating over `__dataclass_fields__` directly avoids this overhead. | ||
| **Action:** Replace `asdict()` with a custom field iteration method (falling back appropriately) in hot paths like metrics classes (`RequestMetrics`, `SpeculateMetrics`). |
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
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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| # limitations under the License. | ||
| """ | ||
|
|
||
| import dataclasses | ||
| from dataclasses import dataclass, field | ||
| from typing import NamedTuple, Optional | ||
|
|
||
|
|
@@ -164,6 +165,28 @@ class SpeculateMetrics: | |
| """ | ||
| accept_ratio_per_head: list[float] | ||
|
|
||
| def to_dict(self): | ||
| """ | ||
| Convert the SpeculateMetrics object to a dictionary. | ||
| """ | ||
| res = {} | ||
| for k in self.__dataclass_fields__: | ||
| v = getattr(self, k) | ||
| if type(v) in (int, float, str, bool, type(None)): | ||
| res[k] = v | ||
| elif isinstance(v, list): | ||
| res[k] = list(v) | ||
| elif isinstance(v, dict): | ||
| res[k] = dict(v) | ||
| elif dataclasses.is_dataclass(v): | ||
| if hasattr(v, "to_dict"): | ||
| res[k] = v.to_dict() | ||
| else: | ||
| res[k] = dataclasses.asdict(v) | ||
| else: | ||
|
Comment on lines
+168
to
+186
|
||
| res[k] = v | ||
| return res | ||
|
|
||
|
|
||
| @dataclass | ||
| class SamplerOutput: | ||
|
|
||
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 与 SpeculateMetrics.to_dict 里包含几乎相同的字段遍历/浅拷贝逻辑,后续若需要支持更多类型(例如 list/dict 内嵌 dataclass 的递归转换)时容易出现两处实现不一致。建议抽取一个共享的序列化 helper(例如放在 utils/metrics 里)并在两处复用,以降低维护成本。