Skip to content

perf(sessions): replace substring indexing with lazy metadata scans - #9823

Merged
allenxu09 merged 1 commit into
farion1231:mainfrom
YUZHEthefool:perf/session-metadata-scan
Nov 21, 2026
Merged

allenxu09 merged 1 commit into
farion1231:mainfrom
YUZHEthefool:perf/session-metadata-scan

Conversation

@YUZHEthefool

@YUZHEthefool YUZHEthefool commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Human Note / 本人说明

索引算法,数据结构优化

Summary / 概述

会话页使用 FlexSearch 的 full 分词为 UUID 和路径生成大量子串索引,切换应用后重新构建;空查询还会在 Hook 和页面各排序一次。

改为首次非空搜索时准备每条会话的小写元数据字符串,按空白拆分关键词并要求每项均为字面子串。缓存只依赖完整 sessions,应用选择和可见应用筛选复用缓存;页面在元数据与正文结果合并后统一排序。移除唯一使用点及 FlexSearch 依赖。

Related Issue / 关联 Issue

本次性能审计第 1 项,无独立 Issue。

Screenshots / 截图

不适用,保留现有页面布局。

Validation / 验证

  • 本机 Node 合成 3,000 条包含 UUID、路径和中文的元数据:原索引构建约 909 ms,保留堆增量约 108 MiB;小写字符串准备约 4 ms,两关键词线性扫描平均约 0.33 ms(200 次)。这些是本机样本,不是所有数据规模的时延保证。

  • 30 项相关测试通过:新增惰性准备、切换应用复用、列表更新失效、多关键词跨字段匹配、中文、大小写、UUID、路径、字面标点和不重复排序的回归;已有 SessionManagerPage 测试全部通过。

  • node node_modules/vitest/vitest.mjs run --no-cache tests/hooks/useSessionSearch.test.ts tests/components/SessionManagerPage.test.tsx

  • bun run typecheck、bun run format:check、git diff --check 通过;Bun 执行 package.json 同名脚本,本机未安装 pnpm。

  • 补充组合验证:将 perf(sessions): replace substring indexing with lazy metadata scans #9823、perf(sessions): index short queries and isolate WAL readers #9827、perf(privacy): avoid Unicode copies and prune redundant scans #9826、perf(usage): index filters and select pooled medians in one scan #9825 基于 5a5111b0 临时合并后,完整 cargo test 通过:5,122 项单元测试、222 项集成测试,11 项忽略;TypeScript、Prettier、cargo fmt --check 和生产前端构建通过。

  • 该组合的前端全量运行有 4,294 项通过,2 项 PiProviderForm 用例超过默认 5 秒时限。原始 main(5a5111b0)上相同两项也超时;仅对本次复跑设置 --testTimeout=15000 后,两项均通过,未改仓库测试时限配置。

Checklist / 检查清单

  • TypeScript 类型检查通过
  • 格式检查通过
  • 无 Rust 改动
  • 无新增用户界面文案

@funkpopo funkpopo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

非常干净的简化:用惰性小写元数据和按空白分片的字面子串过滤取代了 FlexSearch 的 full 全子串索引,在消除大量内存占用和切应用重建开销的同时代码反而更精简。

审查要点确认

  • 惰性构建与按列表粒度复用 (src/hooks/useSessionSearch.ts#L16-L56):空查询完全零开销;仅在首次非空搜索时按 sessions 列表构造一次小写索引文本,切换 providerFilter 或 providerIds 不再重建;多关键词跨字段匹配保留原有的字面子串与大小写不敏感语义。
  • 消除重复排序与清理依赖 (src/components/sessions/SessionManagerPage.tsx#L289-L300):由页面在合并元数据与正文命中结果后统一排序一次,并移除唯一的 flexsearch 依赖。
  • 验证结果:在 main merge-tree 上本地实测通过 pnpm typecheck、pnpm format:check、pnpm knip 以及 useSessionSearch.test.ts / SessionManagerPage.test.tsx(30/30),GitHub 前端检查与全部分片测试通过,予以批准。

@allenxu09 allenxu09 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,很扎实的性能优化。

@allenxu09
allenxu09 merged commit 52f78ba into farion1231:main Nov 21, 2026
20 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Dependency updates frontend Frontend (React/TypeScript)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants