perf: (Codex) 优化 RuntimeService.getScriptsForTab#1404
Draft
cyfung1031 wants to merge 18 commits into
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 优化
RuntimeService.getScriptsForTab的页面加载路径,并调整 Popup 对 disabled 脚本的匹配方式,减少 service worker 初始化和页面加载时的重复计算。同时修复了命名@resource使用file:///本地资源时未按解析后的路径刷新资源的问题。背景
原来的
getScriptsForTab在每次页面加载时都会重新读取或解析脚本运行所需的多项静态资料,例如 compiled resource、resource、脚本代码、metadata、userConfig 和 URL match 规则。实际页面加载时经常只有脚本 value 需要保持最新,脚本代码、metadata、match 规则和 resource 等静态资料通常不会变化。另外,service worker 初始化阶段原本会处理全部普通脚本,包括 disabled 脚本。对于安装脚本较多、但实际启用脚本较少的场景,这会带来不必要的启动成本。
修改内容
1. 为页面加载增加 service worker 内存缓存
新增 page-load cache,用于缓存单个脚本在页面加载时需要复用的静态资料:
scriptUrlPatterns/originalUrlPatternsresourcecodemetadataStruserConfigStruserConfigfile:///resource 的 hash 快照这些缓存只存在于当前 service worker 生命周期内,不写入持久化存储,也不改变
getScriptsForTab的输入输出结构。2.
getScriptsForTab复用静态资料,只刷新动态 value新的页面加载流程会先通过 matcher 找到当前 URL 匹配的 enabled 脚本,再按脚本缓存 key 判断 page-load cache 是否可复用。
GM_setValue等 value 更新可以及时反映;cache key 覆盖会影响运行静态资料的字段,包括
metadata、originalMetadata、selfMetadata、status、type和updatetime。3. 保持脚本执行顺序稳定
getScriptsForTab现在会先按 matcher 返回顺序建立结果槽位,再将 cache hit 和 cache miss 的脚本回填到对应位置,避免异步处理 cache miss 时改变脚本执行顺序。4. 更完整的 runtime cache 清理
新增统一的 runtime cache 清理逻辑,在脚本安装、更新、删除、启用或停用等场景中清理相关缓存,避免复用旧的 page-load cache、code cache 或 matcher pattern cache。
5. 保留
file:///本地资源热更新能力page-load cache 会记录本地
file:///resource 的 hash。页面加载时如果发现本地资源内容变化,会更新:同时修复
ResourceService.getScriptResources中命名@resource的本地资源判断逻辑:现在使用解析后的path判断file:///,而不是原始uri,保证@resource name file:///...能正确触发本地文件刷新。6. service worker 初始化只预热 enabled 普通脚本
waitInit()改为:compiledResourceDAO.all()读取全部 compiled resource;CompiledResourceNamespace标记判断是否需要清理旧注册;SCRIPT_TYPE_NORMAL && SCRIPT_STATUS_ENABLE的脚本建立 matcher / compiled resource;这样可以减少“安装大量脚本但只启用少量脚本”时的 service worker 启动开销。
7. Popup disabled 脚本匹配改为按需构建
Popup 获取当前页面脚本匹配结果时,改为调用专用入口:
该入口会在 Popup 首次请求时按需构建 disabled matcher,后续 Popup 请求复用该 matcher。普通页面加载路径只维护 enabled matcher,不再被 disabled 脚本拖慢。
当脚本安装、更新、删除、启用、停用或排序变化时,会同步清理或更新相关 matcher / sorter 缓存,保证 Popup 结果仍能按 sort 顺序返回,并正确保留 effective / non-effective 状态。
8. 类型收紧
新增
RequireField<T, K>工具类型,并将部分 userScripts 注册结构收紧为必须包含js字段,避免内部构造注册脚本时出现类型约束不明确的问题。测试覆盖
本 PR 补充和调整了 runtime / resource 单元测试,覆盖:
null;run-in、noframes、inject-into等过滤和拆分逻辑;file:///resource 变化后更新缓存、返回结果和已注册 userScript;@resource name file:///...会立即刷新本地文件;兼容性
getScriptsForTab的输入输出结构;Screenshots / 截图