perf(phase2): 按 PROF 热点优化编译耗时

- HandlesImports: _TryLoadClassMembersFromPyi 复用 _pyi_cache 跳过重复 isfile(-12.8s);_LookupStubFuncType 类级缓存 + 签名增量扫描新增 stub,去掉每文件 isfile(-6.5s+)
- Phase2Translator: pyi cache 仅主进程构建(worker 只读 pkl,-23s+pickle 10.4s);include 源文件 sha1/存在性缓存消除 _build_current_ModuleSha1Map 重复磁盘 I/O
- StructGen.finalize: 行过滤改 startswith(tuple) 预计算,消除 O(lines×structs×patterns) 嵌套循环(tottime 84.8s → 预期数秒)
- 附带 fail-fast(任意文件出错立即终止)+ 子进程 PROF 硬编码
This commit is contained in:
TermiNexus committed 2026-08-19 00:28:40 +08:00
1 parent 1808e90fd3
commit fbd132284b
3 files changed
+140 -66

No files matched your search

+16 -15
View File
@@ -170,23 +170,24 @@ class StructGenMixin:
struct_names.add(sname)
if struct_defs:
lines: list[str] = ir_text.split('\n')
# 预计算全部 "= type" 定义行前缀(一次性),用 str.startswith(tuple)
# 在 C 层批量比较,替代原 O(lines × structs × patterns) 的 Python 嵌套
# 循环(PROF: finalize tottime 84.8s + startswith 4114 万次 46.9s)。
name_prefixes: list[str] = []
for name in struct_names:
if name[0].isdigit() or '.' in name or not name.replace('_', '').replace('.', '').isalnum():
name_prefixes.append(f'%"{name}" = type')
name_prefixes.append(f'%"{name}"=type')
name_prefixes.append(f'%{name} = type')
name_prefixes.append(f'%{name}=type')
else:
name_prefixes.append(f'%{name} = type')
name_prefixes.append(f'%{name}= type')
name_prefixes.append(f'%{name}=type')
prefixes_tuple: tuple[str, ...] = tuple(name_prefixes)
filtered: list[str] = []
for line in lines:
stripped: str = line.strip()
skip: bool = False
for name in struct_names:
patterns: list[str]
if name[0].isdigit() or '.' in name or not name.replace('_', '').replace('.', '').isalnum():
patterns = [f'%"{name}" = type', f'%"{name}"=type', f'%{name} = type', f'%{name}=type']
else:
patterns = [f'%{name} = type', f'%{name}= type', f'%{name}=type']
for p in patterns:
if stripped.startswith(p):
skip = True
break
if skip:
break
if not skip:
if not line.strip().startswith(prefixes_tuple):
filtered.append(line)
ir_text = '\n'.join(filtered)
header_end: int = ir_text.find('\ndefine')