perf(render): MFA 倒计时仅在展示 TOTP 时运行,消除空闲状态的每秒整页重渲染 - #2112
Open
HUF457 wants to merge 1 commit into
Open
Conversation
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.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
问题
两个账号页(
CodexAccountsPage、AccountsPage)的 MFA 倒计时setInterval无条件每秒执行一次setState,每次都触发整页重渲染——CodexAccountsPage 约 2.1 万行、464 个 hooks。空闲状态 CPU profile 显示:Codex 页空闲占用 1.47%,对照仪表盘只有 0.08%,其中仅 i18n 查找就占了浪费量的约五分之一。而这个倒计时只在账号备注弹层的 TOTP 动态码旁边才会渲染——绝大多数时间它在为不存在的界面走表。
改法
给定时器加门控:只在 2FA 密钥非空(即倒计时真的在展示)时才启动;重新激活时先立即同步一次剩余秒数。剩余时间用墙钟计算(
getMfaTimeRemaining()),暂停后恢复不会停留在旧秒数。验证
tsc --noEmit通过。