Skip to content

perf(render): MFA 倒计时仅在展示 TOTP 时运行,消除空闲状态的每秒整页重渲染 - #2112

Open
HUF457 wants to merge 1 commit into
jlcodes99:mainfrom
HUF457:perf/mfa-countdown-idle-tick
Open

perf(render): MFA 倒计时仅在展示 TOTP 时运行,消除空闲状态的每秒整页重渲染#2112
HUF457 wants to merge 1 commit into
jlcodes99:mainfrom
HUF457:perf/mfa-countdown-idle-tick

Conversation

@HUF457

@HUF457 HUF457 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

问题

两个账号页(CodexAccountsPageAccountsPage)的 MFA 倒计时 setInterval 无条件每秒执行一次 setState,每次都触发整页重渲染——CodexAccountsPage 约 2.1 万行、464 个 hooks。

空闲状态 CPU profile 显示:Codex 页空闲占用 1.47%,对照仪表盘只有 0.08%,其中仅 i18n 查找就占了浪费量的约五分之一。而这个倒计时只在账号备注弹层的 TOTP 动态码旁边才会渲染——绝大多数时间它在为不存在的界面走表。

改法

给定时器加门控:只在 2FA 密钥非空(即倒计时真的在展示)时才启动;重新激活时先立即同步一次剩余秒数。剩余时间用墙钟计算(getMfaTimeRemaining()),暂停后恢复不会停留在旧秒数。

验证

  • 同环境 CPU profile:Codex 页空闲占用 1.47% → 0.30%
  • tsc --noEmit 通过。

Both account pages ran a 1s interval unconditionally, and each tick
re-rendered the entire page component - 21k lines and 464 hooks for
CodexAccountsPage. A CPU profile of the idle app showed 1.47% CPU on the
Codex page against 0.08% on the dashboard, with i18n lookups alone
accounting for a fifth of the wasted work.

The countdown only ever renders next to a live TOTP token, so gate the
interval on a non-empty 2FA secret.
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.

1 participant