Repository navigation
perf(privacy): avoid Unicode copies and prune redundant scans - #9826
Merged
farion1231 merged 1 commit intoNov 22, 2026
Merged
Conversation
This was referenced Nov 21, 2026
funkpopo
approved these changes
Nov 21, 2026
funkpopo
left a comment
Collaborator
There was a problem hiding this comment.
非常扎实的一组热路径算法与分配优化,每项剪枝都有严格数学依据或全量标量测试托底,在显著压低中文与常规代码正文扫描耗时和内存分配的同时完整守住了原有识别语义。
审查要点确认
- 关键词预筛免全量小写复制 (
src-tauri/src/proxy/privacy_filter/secrets.rs#L265-L355):在全规则关键词为 ASCII 时直接用已有 ASCII 忽略大小写自动机扫描原文,仅在文本含'K'/'İ'或存在自定义非 ASCII 关键词时回退to_lowercase();并遍历全部 Unicode 标量验证了会小写展开为 ASCII 的字符集合。 - 单次上下文判定与香农熵字母表剪枝 (
src-tauri/src/proxy/privacy_filter/secrets.rs#L670-L753):secret_context合并返回None / Weak / Strong,避免对高熵候选重复执行回溯匹配;entropy_at_least通过H(X) <= log2(distinct)(MAX_ENTROPY[distinct] + 1e-12预留浮点舍入余量)跳过绝大多数低熵标识符的浮点对数计算,临界值保留原有累加次序。 - PII 正则字节预检 (
src-tauri/src/proxy/privacy_filter/pii.rs#L122-L168):利用@、ASCII 数字与.字节存在性剪枝不可能命中的邮箱、国内号段/卡号/身份证和 IPv4 正则。 - 验证结果:在当前
mainmerge-tree 上本地实测通过cargo fmt --check、cargo clippy -- -D warnings(--all-targets无新增告警)以及全部proxy::privacy_filter单测(48/48),四平台 GitHub CI 全部通过,予以批准。
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.
Human Note / 本人说明
索引算法,数据结构优化
Summary / 概述
中文请求使关键词预筛对整块文本进行 Unicode 小写复制;高熵候选的上下文被重复匹配,不同字节较少的标识符仍逐桶计算熵,PII 检测无条件运行五组正则。
K、İ或自定义非 ASCII 关键词时保留完整 Unicode 回退。@、ASCII 数字、.预检跳过不可能命中的正则,保留原校验与范围。审计更正:
ſ的to_lowercase()不会变成 ASCIIs。新增遍历所有 Unicode 标量的测试验证当前 ASCII 小写展开字符集合,避免把 Unicode 大小写折叠与 lowercase 混为一谈。Related Issue / 关联 Issue
本次性能审计第 3 项,无独立 Issue。本 PR 修改过滤算法;与 #9821 的调用路径去重相互独立。
Screenshots / 截图
不适用,后端文本检测调整。
Validation / 验证
隐私相关单元及代理集成测试:51 项通过,包括真实本地 HTTP 上游请求中的最终正文、覆写和重试脱敏。
覆盖 Unicode 关键词与旧实现对拍、非 ASCII 自定义关键词、PII 预检、上下文强弱,以及均匀分布和浮点临界熵阈值。
独立本机微基准:1.14 MB 纯中文样本,关键词预筛约 49.9 → 42.0 ms;PII 检测约 86.3 → 1.0 ms;不同字节较少的标识符熵判断约 342 → 46 ns。算法 harness 使用
rustc -O并复用本机 debug 依赖,仅用于前后对照,不代表发布构建或端到端吞吐。cargo test --locked --offline --manifest-path src-tauri/Cargo.toml --lib privacy_filter -- --test-threads=2cargo fmt --check、git diff --check、cargo clippy --locked --offline --manifest-path src-tauri/Cargo.toml -- -D warnings通过。本机通过临时
OPENSSL_DIR使用已有 OpenSSL 3 库和头文件,未改动依赖;测试使用隔离配置目录。补充组合验证:将 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 / 检查清单
-D warnings)