diff --git a/lib/Projectrans/Phase2Translator.py b/lib/Projectrans/Phase2Translator.py index 203c58f..4856850 100644 --- a/lib/Projectrans/Phase2Translator.py +++ b/lib/Projectrans/Phase2Translator.py @@ -922,7 +922,16 @@ def _PyiCacheWorkerInitializer(temp_dir: str) -> None: def _count_braces_outside_strings(text: str) -> int: - """统计花括号差值,跳过 c"..." 字符串字面量内部的括号""" + """统计花括号差值,跳过 c"..." 字符串字面量内部的括号 + + 快路径(prof 基线:本函数 102,397 次调用 / self 2.0s,占 _split_ll cumtime + 3.35s 的 83%,是全流程 str.strip 之后第二大的"纯 Python 逐字符"热点): + 行内不含 `c"` 时,"跳过字符串内部括号"这一语义退化为"整行括号计数", + 直接用 C 级 str.count 一次算完。含 `c"` 的行(主要见于 c"..." 全局串常量) + 仍走下面的逐字符扫描,语义逐字不变。 + """ + if 'c"' not in text: + return text.count('{') - text.count('}') count = 0 i = 0 while i < len(text): @@ -1720,7 +1729,9 @@ class Phase2Translator: if in_global_def: text_lines.append(line) global_type_parts.append(line.strip()) - global_brace_depth += _count_braces_outside_strings(line) + # 预筛:无花括号的行必然贡献 0(与计数器返回值逐字等价,见其快路径) + if '{' in line or '}' in line: + global_brace_depth += _count_braces_outside_strings(line) if global_brace_depth <= 0: full_type = ' '.join(global_type_parts).strip() depth = 0 @@ -1744,7 +1755,10 @@ class Phase2Translator: if in_define: text_lines.append(line) - brace_depth += _count_braces_outside_strings(line) + # 预筛:函数体指令行绝大多数不含花括号,跳过即省掉一次函数调用 + # (prof:本函数 102k 次调用;有花括号的行仍按原语义计数) + if '{' in line or '}' in line: + brace_depth += _count_braces_outside_strings(line) if brace_depth <= 0: in_define = False continue @@ -1770,9 +1784,11 @@ class Phase2Translator: brace_depth = 1 in_define = True if prev_stripped.startswith('define '): - decl_line = re.sub(r'^define\s+', 'declare ', prev_stripped) - decl_line = re.sub(r'\b(linkonce_odr|weak_odr|linkonce|weak|common|appending|internal|private)\s+', '', decl_line) - decl_line = re.sub(r'\balwaysinline\s+', '', decl_line) + # 与上面 define 分支同一组预编译正则(模式串一字未改), + # 避免 re 模块级的"模式串哈希→缓存查找"逐行开销 + decl_line = _RE_DEFINE_PREFIX.sub('declare ', prev_stripped) + decl_line = _RE_DEFINE_LINKAGE.sub('', decl_line) + decl_line = _RE_ALWAYSINLINE.sub('', decl_line) paren_pos = decl_line.rfind(')') if paren_pos >= 0: decl_line = decl_line[:paren_pos + 1]