From 267e9881b41d00ffc07c0ac96d1713dbd97eab4f Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Wed, 19 Aug 2026 00:32:21 +0800 Subject: [PATCH] =?UTF-8?q?perf(structgen):=20finalize=20=E7=89=B9?= =?UTF-8?q?=E6=AE=8A=E5=AD=97=E7=AC=A6=20struct=20=E5=90=8D=E5=8A=A0?= =?UTF-8?q?=E5=BC=95=E5=8F=B7=E5=90=88=E5=B9=B6=E4=B8=BA=E5=8D=95=E6=AC=A1?= =?UTF-8?q?=20re.sub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 逐 name 全文本 re.sub 改为单正则 alternation 一次扫描,消除 N 次 O(text) 扫描(lookahead 边界保证备选互斥,语义等价) --- lib/core/LLVMCG/StructGen.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/core/LLVMCG/StructGen.py b/lib/core/LLVMCG/StructGen.py index 865045f..97e994a 100644 --- a/lib/core/LLVMCG/StructGen.py +++ b/lib/core/LLVMCG/StructGen.py @@ -202,13 +202,16 @@ class StructGenMixin: insert_pos = header_end struct_block: str = '\n'.join(struct_defs) + '\n' ir_text = ir_text[:insert_pos + 1] + struct_block + ir_text[insert_pos + 1:] - for name in struct_names: - if name[0].isdigit() or '.' in name: - ir_text = re.sub( - r'%' + re.escape(name) + r'(?=[^a-zA-Z0-9_$\.]|$)', - r'%"' + name + r'"', - ir_text - ) + # 仅含特殊字符(数字开头或含点)的 struct 名需要加引号: + # 合并为单个正则一次扫描全文本,替代逐 name 各自 re.sub(每个 name + # 一次 O(text) 扫描)。lookahead 边界排除保证各备选互斥,语义等价。 + special_names: list[str] = [n for n in struct_names if n[0].isdigit() or '.' in n] + if special_names: + ir_text = re.sub( + r'%(' + '|'.join(re.escape(n) for n in special_names) + r')(?=[^a-zA-Z0-9_$\.]|$)', + lambda m: r'%"' + m.group(1) + r'"', + ir_text + ) return ir_text