Skip to content

⚡ Bolt: Optimize RequestMetrics.to_dict() serialization#7022

Open
ZeyuChen wants to merge 1 commit intodevelopfrom
bolt-optimize-requestmetrics-todict-10518415069077200055
Open

⚡ Bolt: Optimize RequestMetrics.to_dict() serialization#7022
ZeyuChen wants to merge 1 commit intodevelopfrom
bolt-optimize-requestmetrics-todict-10518415069077200055

Conversation

@ZeyuChen
Copy link
Member

Motivation

The RequestMetrics object is frequently serialized using to_dict() during request handling and completion. It previously relied on dataclasses.asdict(), which utilizes a recursive deepcopy that is notoriously slow. This created a performance bottleneck on the API hot path, unnecessarily increasing latency and overhead.

Modifications

  1. fastdeploy/engine/request.py: Refactored RequestMetrics.to_dict() to iterate over __dataclass_fields__ directly. It performs type checks to assign primitives by value, perform fast shallow copies for standard collections (list, dict), and recursively calls to_dict() for nested dataclasses if available.
  2. fastdeploy/worker/output.py: Added a matching, optimized to_dict() method to SpeculateMetrics to ensure nested objects do not fall back to the slow asdict() implementation.

Usage or Command

No changes required for end users. The optimization is transparent and reduces the overhead of API request logging and metric processing.

Accuracy Tests

  • Local performance tests demonstrate an approximate 44% reduction in execution time for RequestMetrics.to_dict().
  • Successfully ran pytest tests/engine/test_request.py (30 passed).
  • Successfully ran black, isort, and flake8 to ensure no linting regressions.

Checklist

  • I have tested the code locally and it works as expected.
  • I have added unit tests for my changes (if applicable).
  • I have updated the documentation (if applicable).
  • I have reviewed my code and ensured it follows the project's coding standards.

PR created automatically by Jules for task 10518415069077200055 started by @ZeyuChen

Replaces the slow `dataclasses.asdict()` call in `RequestMetrics.to_dict()` and `SpeculateMetrics.to_dict()` with a direct property assignment loop. `asdict()` internally performs a deep copy, which adds significant overhead. This optimization uses shallow assignments (and shallow list/dict copies) to dramatically reduce serialization time, which is critical since these metrics are repeatedly dumped on the hot path during request completions.

Performance tests showed ~44% latency reduction in `to_dict` serialization.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings March 25, 2026 14:28
@paddle-bot
Copy link

paddle-bot bot commented Mar 25, 2026

Thanks for your contribution!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

本 PR 针对请求处理热路径上的指标序列化开销进行优化,通过避免 dataclasses.asdict() 的递归 deepcopy 来降低 to_dict() 的 CPU 开销与延迟,属于引擎/worker 指标链路的性能优化。

Changes:

  • 重写 RequestMetrics.to_dict():遍历 __dataclass_fields__ 做浅序列化,并对嵌套 dataclass 做递归处理。
  • SpeculateMetrics 增加匹配的 to_dict(),避免嵌套对象回退到慢路径序列化。

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() 的序列化实现,引入 is_dataclass 分支处理嵌套 dataclass
fastdeploy/worker/output.py SpeculateMetrics 增加快速 to_dict(),用于与 RequestMetrics 的嵌套序列化配合

Comment on lines +899 to +901
res = {}
for k in self.__dataclass_fields__:
v = getattr(self, k)
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR 标题目前未按仓库 PR 模板要求包含至少一个标签(形如 [Optimization]...)。建议将标题改为例如 [Optimization] Optimize RequestMetrics.to_dict() serialization(可按需要叠加 [Engine] 等标签),以满足模板检查项并便于后续检索。

Copilot uses AI. Check for mistakes.
Comment on lines +899 to +915
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 type(v) is list:
res[k] = list(v)
elif type(v) is dict:
res[k] = dict(v)
elif is_dataclass(v):
if hasattr(v, "to_dict"):
res[k] = v.to_dict()
else:
res[k] = asdict(v)
else:
res[k] = v
return res
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestMetrics.to_dict() 的序列化逻辑与 fastdeploy/worker/output.py::SpeculateMetrics.to_dict() 基本重复;建议抽一个共享的轻量 helper(例如放到 metrics/utils 模块)或复用同一实现,避免未来一处修改导致两处行为不一致。

Copilot uses AI. Check for mistakes.
Comment on lines +167 to +179
def to_dict(self):
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 type(v) is list:
res[k] = list(v)
elif type(v) is dict:
res[k] = dict(v)
else:
res[k] = v
return res
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpeculateMetrics.to_dict()fastdeploy/engine/request.py::RequestMetrics.to_dict() 存在重复的“快速 dataclass 序列化”逻辑;建议抽取共享 helper 或基类复用,减少重复代码并避免后续序列化规则分叉。

Copilot uses AI. Check for mistakes.
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants