(Codex) 优化 RuntimeService.getScriptsForTab#1404
Draft
cyfung1031 wants to merge 2 commits intomainfrom
Draft
Conversation
Member
|
越来越激进了,我觉得要做好自动化测试 |
Collaborator
Author
这个改动大。先放在这里做 draft PR 会加测试 |
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.
Checklist / 检查清单
Description / 描述
PR 描述
本 PR 优化
RuntimeService.getScriptsForTab及相关初始化流程,目标是减少页面加载时和 service worker 首次启动时对脚本运行资料的重复计算,尤其针对“安装脚本很多,但实际启用脚本很少”的场景。背景与问题
原本
getScriptsForTab每次页面加载都会重新执行较多固定成本操作,包括读取脚本、读取 compiled resource、加载 resource、读取并解析 script code、解析 metadata/userConfig 等。但这些资料大部分并不会因为
GM_setValue/ value 更新而改变。常见情况下,脚本代码、metadata、resource、match 规则都没有变化,只有 value 变化。此时重复计算静态资料没有必要。另外,原本 service worker 初始化时会处理全部普通脚本,包括 disabled 脚本。如果用户安装了大量脚本,例如 300 个脚本但只启用 30 个,初始化仍可能为大量 disabled 脚本建立 compiled resource / matcher cache,启动成本过高。
修改内容
在
runtime.ts中新增 page-load cache,用于缓存单个脚本在页面加载时需要的静态资料:scriptUrlPatternsoriginalUrlPatternsresourcemetadataStruserConfigStruserConfigcode缓存只存在于当前 service worker 生命周期内,不改变持久化数据结构,也不改变
getScriptsForTab的输入输出。getScriptsForTab只刷新动态 value重写后的
getScriptsForTab会先根据 URL 匹配 enabled 脚本,然后检查脚本静态资料 cache 是否可用。如果 cache 命中:
如果 cache 未命中:
这样 value-only 更新不会导致整套静态资料重新计算。
cache key 基于会影响运行静态资料的脚本因素生成,包括:
脚本安装、更新、删除时会清理相关 cache。启用/停用时会清理 page-load cache,避免状态变化后复用旧资料。
getScriptsForTab中 cache hit 和 cache miss 混合出现时,仍按 URL matcher 返回的顺序回填脚本列表,避免因为 cache miss 后追加而改变脚本执行顺序。file:///resource 仍保持热更新对于
file:///resource,页面加载时仍会检查资源内容是否变化。如果本地资源变化:
因此优化不会破坏本地资源开发场景。
调整
waitInit():compiledResourceDAO.all()读取全部 compiled resourcecompiledResourceNamespaceflag 判断是否需要清理旧注册SCRIPT_TYPE_NORMAL && SCRIPT_STATUS_ENABLE的脚本建立 matcher / compiled resource这样安装大量 disabled 脚本时,不会在 service worker 启动阶段为它们建立运行 cache。
Popup 需要显示“当前页面匹配但 disabled 的脚本”时,不再依赖初始化时预先建立的
scriptMatchDisablecache。新增按需查询流程:
兼容性
本 PR 不修改
getScriptsForTab的输入输出结构,也不改变 content / inject 接收到的数据格式。缓存仅存在于 service worker 内存中;service worker 重启后会按现有流程重新建立必要资料。
Screenshots / 截图