Skip to content

[0162] 字体处理性能优化#3448

Open
da-liii wants to merge 12 commits into
mainfrom
da/0162/font_perf
Open

[0162] 字体处理性能优化#3448
da-liii wants to merge 12 commits into
mainfrom
da/0162/font_perf

Conversation

@da-liii
Copy link
Copy Markdown
Contributor

@da-liii da-liii commented May 21, 2026

摘要

对字体处理代码做实验性质的性能优化,共 11 个 commit。

优化内容

  1. 优化 smart_font resolve 避免重复 get_unicode_range 调用 — 将 get_unicode_range(c) 提取到循环外,只计算一次
  2. 缓存 get_unicode_range 和 get_utf8_code 结果 — 避免重复 UTF-8 转换
  3. 优化 find_font 减少重复 as_string 调用 — 缓存 sz_strdpi_str
  4. 优化 smart_font_bis 字符串构造 — 缓存 vdpi_str/hdpi_str,移除多余 normalize
  5. 移除不必要的字符串拷贝get_extents/get_xpositions/draw_fixedstring r= s 改为 string r
  6. 优化 closest_font 减少重复 as_string 调用 — 缓存 dpi_strattempt_str
  7. 缓存 substitute_math_letter 结果 — 避免重复数学字母替换
  8. 循环不变量提升is_rubber / starts(c, "<wide-") 移出循环
  9. 添加 occurs 快速路径 — 避免不含 = 的 fam 执行无意义 trimmed_tokenize
  10. 清理临时 LIII_DEBUG 日志
  11. 代码审查修复 — 重命名误导性测试,清理冗余注释

测试

  • xmake b smart_font_test && xmake r smart_font_test — 12 个测试全部通过
  • xmake b font_size_test && xmake r font_size_test — 19 个测试全部通过

🤖 Generated with Claude Code

da-liii and others added 12 commits May 20, 2026 23:46
- 为 resolve 添加带 range 参数的重载
- resolve(string c) 只计算一次 get_unicode_range,然后传给循环中的 resolve
- 每个字符减少多次 UTF-8 解码开销

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- 在 unicode.cpp 中添加 unicode_range_cache 缓存 string->string 映射
- 添加 get_utf8_code_cached 缓存 string->int 映射
- 将 smart_font.cpp 中的 get_utf8_code 委托给缓存版本
- 大幅减少 strict_cork_to_utf8 和 decode_from_utf8 的重复调用

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- 缓存 dpi_str 和 sz_str 到局部变量
- 避免在 t1/t2/t3/panic 构造中重复调用 as_string

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- 缓存 vdpi_str 和 hdpi_str 避免重复 as_string
- 移除 smart_font/math_smart_font/prog_smart_font 中多余的
  normalize_half_multiple_size 调用(smart_font_bis 已做)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- 将 string r= s 改为 string r 避免每次复制整个字符串
- advance 内部会给 r 重新赋值,初始拷贝是浪费的

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- 缓存 dpi_str 和 attempt_str 到局部变量
- 避免在 extra 构造中重复调用 as_string

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- 在 resolve(string c) 中提前计算 sf 并复用
- 减少 math letter 替换的重复哈希查找

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- 提前计算 is_rubber、starts(c, \"<wide-\")、fn[SUBFONT_MAIN]->supports(c)
- 避免在 family_tokens 内层循环中重复执行相同检查

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- 在 resolve(c, range, fam, attempt) 中先检查 fam 是否包含 '='
- 对于不含 '=' 的 fam(大多数情况)跳过 tokenize 开销

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
移除验证优化效果时添加的临时调试输出,保持代码整洁。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- test_resolve_performance → test_resolve_mixed_chars(该测试验证正确性而非性能)
- 移除 find_font.cpp 和 font_translate.cpp 中解释显而易见代码的注释

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant