gh-91404: Use computed gotos and reduce indirection in re (#91495)

This commit is contained in:
Brandt Bucher 2022-04-15 09:26:44 -07:00 committed by GitHub
parent d104f4d21f
commit 1b34b5687b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 483 additions and 348 deletions

View file

@ -29,7 +29,11 @@ sre_constants_header = """\
"""
def main(infile='Lib/re/_constants.py', outfile='Modules/_sre/sre_constants.h'):
def main(
infile="Lib/re/_constants.py",
outfile_constants="Modules/_sre/sre_constants.h",
outfile_targets="Modules/_sre/sre_targets.h",
):
ns = {}
with open(infile) as fp:
code = fp.read()
@ -46,6 +50,11 @@ def main(infile='Lib/re/_constants.py', outfile='Modules/_sre/sre_constants.h'):
for value, name in sorted(items):
yield "#define %s %d\n" % (name, value)
def dump_gotos(d, prefix):
for i, item in enumerate(sorted(d)):
assert i == item
yield f" &&{prefix}_{item},\n"
content = [sre_constants_header]
content.append("#define SRE_MAGIC %d\n" % ns["MAGIC"])
content.extend(dump(ns["OPCODES"], "SRE_OP"))
@ -54,7 +63,14 @@ def main(infile='Lib/re/_constants.py', outfile='Modules/_sre/sre_constants.h'):
content.extend(dump2(ns, "SRE_FLAG_"))
content.extend(dump2(ns, "SRE_INFO_"))
update_file(outfile, ''.join(content))
update_file(outfile_constants, ''.join(content))
content = [sre_constants_header]
content.append(f"static void *sre_targets[{len(ns['OPCODES'])}] = {{\n")
content.extend(dump_gotos(ns["OPCODES"], "TARGET_SRE_OP"))
content.append("};\n")
update_file(outfile_targets, ''.join(content))
if __name__ == '__main__':