Skip to content

降低 Worker CPU:日期格式化器按时区缓存 - #295

Merged
Moshuiwang merged 1 commit into
mainfrom
claude/cf-cpu-formatter-cache
Nov 7, 2026
Merged

Moshuiwang merged 1 commit into
mainfrom
claude/cf-cpu-formatter-cache

Conversation

@Moshuiwang

Copy link
Copy Markdown
Owner

为什么

回源(等级 7):Workers Free 单次 CPU 上限 10ms,线上 P50 14.5ms、P90 66ms、P99 135ms,0 报错(暂未被掐断)。wrangler tail 逐请求:/api/mobile/summary 63–84ms,/ingest 36–42ms。

根因与改动

本地基准每个时刻约 86µs,几乎全在每次调用都 new Intl.DateTimeFormat(3.0 前后相当,不是 3.0 引入的退化)。改为按统计时区缓存、同一时刻只拆解一次:86µs → 7µs/时刻(约 12×)。

验证(等级 3)

  • 新增守卫:1000 个时刻的转换最多构造 1 个格式化器(修复前 3000 次,先红后绿);切换时区不串缓存。
  • Worker 424 通过;Python 815(test_signed_release 本机缺 cryptography,与本改动无关)。

部署后用 wrangler tail 复测各接口 CPU。

🤖 Generated with Claude Code

线上 wrangler tail 实测:/api/mobile/summary 每次 63–84ms CPU、/ingest 36–42ms(Workers Free
单次 10ms,P99 达 135ms,目前未被掐断)。本地基准:每个时刻约 86µs,几乎全部花在每次调用都
new Intl.DateTimeFormat 上(toOffsetIso 一次还构造两遍)。改为按统计时区缓存一个实例、同一
时刻只拆解一次:每个时刻 86µs → 7µs。新增构造次数守卫(1000 个时刻 ≤ 1 次,修复前 3000 次)。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Moshuiwang
Moshuiwang merged commit 562cc40 into main Nov 7, 2026
5 checks passed
@Moshuiwang
Moshuiwang deleted the claude/cf-cpu-formatter-cache branch November 7, 2026 19:50
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.

2 participants