Conversation
Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
|
|
Thanks for your contribution! |
There was a problem hiding this comment.
Pull request overview
该 PR 针对 fastdeploy/engine/request.py 中请求级指标 RequestMetrics 的序列化热点路径做性能优化,避免 dataclasses.asdict() 触发的递归 deepcopy 带来的开销,从而降低高 QPS 场景下的序列化延迟。
Changes:
- 将
RequestMetrics.to_dict()从asdict(self)改为手写字段遍历与类型分支序列化。 - 引入
is_dataclass用于识别嵌套 dataclass,并在存在.to_dict()时优先调用。 - 新增
.jules/bolt.md记录此次优化的经验与行动项。
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| fastdeploy/engine/request.py | 重写 RequestMetrics.to_dict(),减少 asdict/deepcopy 在热路径上的开销 |
| .jules/bolt.md | 记录 dataclass 序列化优化的学习点与后续行动建议 |
| import time | ||
| import traceback | ||
| from dataclasses import asdict, dataclass, fields | ||
| from dataclasses import asdict, dataclass, fields, is_dataclass |
There was a problem hiding this comment.
PR 标题目前为“⚡ Bolt: [performance improvement] ...”,不符合项目要求的“[CLASS]Title”格式。建议改为类似“[Perf] Optimize RequestMetrics.to_dict serialization”这样的格式,便于后续按类别检索与生成变更日志。
| return {k: v for k, v in asdict(self).items()} | ||
|
|
||
| result = {} | ||
| for k in self.__dataclass_fields__: |
There was a problem hiding this comment.
to_dict() 里直接遍历 self.dataclass_fields 属于依赖 dataclasses 的内部实现细节,可读性和稳定性不如 dataclasses.fields(self/cls)。同一个类的 from_dict() 已经使用了 fields(cls);建议这里也用 fields(self) 并取 field.name 来迭代字段,保持一致性并减少对内部属性的依赖。
| for k in self.__dataclass_fields__: | |
| for field in fields(self): | |
| k = field.name |
| result = {} | ||
| for k in self.__dataclass_fields__: | ||
| v = getattr(self, k) | ||
| if type(v) in (int, float, str, bool, type(None)): | ||
| result[k] = v |
There was a problem hiding this comment.
RequestMetrics.to_dict() 的序列化逻辑已从 asdict() 改为自定义分支。当前 tests/engine 下虽覆盖了 Request/RequestOutput,但没有直接断言 RequestMetrics.to_dict() 的输出结构(尤其是 speculate_metrics 这类嵌套 dataclass)。建议补充单测覆盖:基础字段 + speculate_metrics=SpeculateMetrics(...) 时应输出纯 dict/list/primitive,避免序列化格式回归。
Motivation
The
RequestMetricsclass previously useddataclasses.asdict(self)within itsto_dict()implementation.asdict()utilizescopy.deepcopy()internally, which inherently iterates recursively over all fields and applies expensive cloning logic even for primitive types. SinceRequestMetricsobjects are instantiated and serialized per-request, this deepcopy mechanism created a noticeable serialization overhead on the hot-path, particularly at high QPS.Modifications
Replaced the
dataclasses.asdict(self)call inRequestMetrics.to_dict()with a manually optimized field iteration.getattr().int,float,str,bool,NoneType) without any copy overhead.listanddictcomponents..to_dict()methods for internal nested dataclasses (likeSpeculateMetrics) when available, reducing further dependency on generalizedasdict()deepcopy loops.Usage or Command
Standard deployment. The metrics API functions exactly the same but yields lower latency during serialization steps.
Accuracy Tests
Backward compatibility is maintained. Ran
PYTHONPATH=. pytest tests/engine/test_request.pyand full suitePYTHONPATH=. pytest tests/engine/to verify tests pass and regressions are avoided.Checklist
pnpm lintandpnpm testequivalent formatting & unit tests.PR created automatically by Jules for task 10413296569687171589 started by @ZeyuChen