perf(tpc): 按 profile 消掉两处主热点(stub 索引每 miss 重扫目录 / vtable 每类全扫函数)

依据 PROF/main_prof_10152.prof(main_time.txt: 失败 2362.46s,72 个文件全部走
顺序 _translate_file,tottime 合计 2288.9s):

  tottime      calls   函数
  491.7s      723782   nt._path_isdir            ← 总时长 21%
  268.6s       15762   StructGen._generate_structs
  182.9s      479819   nt.listdir                ← 8%
  87.2s    657640754   builtins.isinstance
  78.6s     41514263   ntpath.join
  75.9s     83047878   ntpath.splitroot
  75.7s         1631   StructGen._create_Vtable_globals   (46ms/次)
  66.6s       238007   HandlesImports._build_stub_index
  63.0s    317868145   str.startswith
  38.4s    175751378   str.endswith
  ...
  ── 仅 isdir + listdir + join/splitroot/endswith ≈ 850s ≈ 总时长的 37%

一、_build_stub_index 的零 syscall 快路径(HandlesImports + Phase2Translator)
    它由 _TryLoadStructFromStub 的**每次索引 miss** 调用(238k 次),每次都要
    2× os.path.isdir + 2× os.listdir + 全量 endswith/join 重拼文件集合 ——
    这正是上面 isdir 723k / listdir 479k 的主要来源。
    改为:写入方显式置脏(ImportHandle.MarkStubSetDirty),未置脏时直接返回。
    置脏点:① 每个文件翻译入口(_translate_file,顺序与 worker 模式皆按文件粒度
    覆盖)② 写 temp/<sha1>.stub.ll 之后 ③ 注册 include 写 output/<sha1>.stub.ll
    之后。索引建完即清脏。语义:增量重扫、output 覆盖 temp 全部不变,只是把
    "集合是否变化"由目录扫描改为写入方免费告知(编译进程内 stub 只增不改)。

二、_create_Vtable_globals 的 O(类数 × 函数数) → O(函数数)
    原实现对每个候选类都遍历一遍 self.functions 找 "{ClassName}." 前缀且非
    declare 的函数;改为一次性预计算"非 declare 函数名的全部点分前缀"集合,
    每类只做一次集合查询。判据等价(startswith(prefix) ⟺ prefix 在点分前缀集)。

重编后请再抓一次 profile 复核收益;下一梯队热点是 _generate_structs
(268.6s tottime / 35k 次调用)与 _parse_llvm_type(3.5M 次调用)。
This commit is contained in:
TermiNexus committed 2026-09-29 14:36:57 +08:00
1 parent f1e7fd6c28
commit 4fc067e3bf
3 files changed
+58 -11

No files matched your search

+12
View File
@@ -1876,6 +1876,11 @@ class Phase2Translator:
不会污染共享缓存。
"""
_t0: float = time.time()
# 每个文件翻译入口置脏:本文件/其它文件在此之间新写的 *.stub.ll 全部可见
# (ImportHandle._build_stub_index 的零 syscall 快路径据此判断是否重扫目录;
# 顺序模式与 worker 模式的每文件粒度皆覆盖,见 MarkStubSetDirty)。
from lib.core.Handles.HandlesImports import ImportHandle as _IH
_IH.MarkStubSetDirty()
with open(src_path, 'r', encoding='utf-8') as f:
code = f.read()
@@ -2149,6 +2154,9 @@ class Phase2Translator:
base = os.path.splitext(out_path)[0]
with open(base + '.stub.ll', 'w', encoding='utf-8', newline='\n') as f:
f.write(stub_content)
# 新 stub 已落盘 → 置脏,使本文件后续(及下个文件)的索引 miss 能重扫到它
from lib.core.Handles.HandlesImports import ImportHandle as _IH2
_IH2.MarkStubSetDirty()
with open(base + '.text.ll', 'w', encoding='utf-8', newline='\n') as f:
f.write(text_content)
self._save_deps(sha1, ModuleSha1Map)
@@ -3870,6 +3878,10 @@ class Phase2Translator:
# 注册到 stub_files
if _CachedIsFile(stub_path):
self.stub_files[sha1] = stub_path
# output/<sha1>.stub.ll 存在(本步刚落盘或从全局缓存拷入)→ 置脏,
# 让后续索引 miss 重扫到新增的 *.stub.ll(见 _build_stub_index 快路径)
from lib.core.Handles.HandlesImports import ImportHandle as _IH3
_IH3.MarkStubSetDirty()
elif _CachedIsFile(sig_path):
# 如果 stub 文件不存在,也尝试从 temp 目录查找
temp_stub = os.path.join(self.temp_dir, f"{sha1}.stub.ll")