Skip to content

Commit

Permalink
Attempt to speed up deepfreeze.py (#107887)
Browse files Browse the repository at this point in the history
* Instead of calling get_identifiers_and_strings(), extract identifiers and strings from pycore_global_strings.h.
* Avoid ast.literal_eval(), it's very slow.
  • Loading branch information
gvanrossum authored Aug 14, 2023
1 parent 3974534 commit a2a4b9f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ regen-frozen: Tools/build/freeze_modules.py $(FROZEN_FILES_IN)
.PHONY: regen-deepfreeze
regen-deepfreeze: $(DEEPFREEZE_OBJS)

DEEPFREEZE_DEPS=$(srcdir)/Tools/build/deepfreeze.py $(FREEZE_MODULE_DEPS) $(FROZEN_FILES_OUT)
DEEPFREEZE_DEPS=$(srcdir)/Tools/build/deepfreeze.py Include/internal/pycore_global_strings.h $(FREEZE_MODULE_DEPS) $(FROZEN_FILES_OUT)

# BEGIN: deepfreeze modules
Python/deepfreeze/deepfreeze.c: $(DEEPFREEZE_DEPS)
Expand Down
35 changes: 23 additions & 12 deletions Tools/build/deepfreeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
by Python 3.10, and 3.11 features are not available.
"""
import argparse
import ast
import builtins
import collections
import contextlib
Expand All @@ -17,10 +16,10 @@
from typing import Dict, FrozenSet, TextIO, Tuple

import umarshal
from generate_global_objects import get_identifiers_and_strings

ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

verbose = False
identifiers, strings = get_identifiers_and_strings()

# This must be kept in sync with opcode.py
RESUME = 151
Expand Down Expand Up @@ -114,13 +113,27 @@ def __init__(self, file: TextIO) -> None:
self.hits, self.misses = 0, 0
self.finis: list[str] = []
self.inits: list[str] = []
self.identifiers, self.strings = self.get_identifiers_and_strings()
self.write('#include "Python.h"')
self.write('#include "internal/pycore_gc.h"')
self.write('#include "internal/pycore_code.h"')
self.write('#include "internal/pycore_frame.h"')
self.write('#include "internal/pycore_long.h"')
self.write("")

def get_identifiers_and_strings(self) -> tuple[set[str], dict[str, str]]:
filename = os.path.join(ROOT, "Include", "internal", "pycore_global_strings.h")
with open(filename) as fp:
lines = fp.readlines()
identifiers: set[str] = set()
strings: dict[str, str] = {}
for line in lines:
if m := re.search(r"STRUCT_FOR_ID\((\w+)\)", line):
identifiers.add(m.group(1))
if m := re.search(r'STRUCT_FOR_STR\((\w+), "(.*?)"\)', line):
strings[m.group(2)] = m.group(1)
return identifiers, strings

@contextlib.contextmanager
def indent(self) -> None:
save_level = self.level
Expand Down Expand Up @@ -171,9 +184,9 @@ def generate_bytes(self, name: str, b: bytes) -> str:
return f"& {name}.ob_base.ob_base"

def generate_unicode(self, name: str, s: str) -> str:
if s in strings:
return f"&_Py_STR({strings[s]})"
if s in identifiers:
if s in self.strings:
return f"&_Py_STR({self.strings[s]})"
if s in self.identifiers:
return f"&_Py_ID({s})"
if len(s) == 1:
c = ord(s)
Expand Down Expand Up @@ -441,12 +454,10 @@ def is_frozen_header(source: str) -> bool:


def decode_frozen_data(source: str) -> types.CodeType:
lines = source.splitlines()
while lines and re.match(FROZEN_DATA_LINE, lines[0]) is None:
del lines[0]
while lines and re.match(FROZEN_DATA_LINE, lines[-1]) is None:
del lines[-1]
values: Tuple[int, ...] = ast.literal_eval("".join(lines).strip())
values: list[int] = []
for line in source.splitlines():
if re.match(FROZEN_DATA_LINE, line):
values.extend([int(x) for x in line.split(",") if x.strip()])
data = bytes(values)
return umarshal.loads(data)

Expand Down

0 comments on commit a2a4b9f

Please sign in to comment.