From 31828a693f35c829fa52a44c9eeab8bc92ba7692 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Mon, 10 Jan 2022 21:00:19 +0300 Subject: [PATCH] Add changelog / some nits --- CHANGES.md | 2 ++ src/blib2to3/pgen2/parse.py | 13 +++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d5cfb623c9a..52ac49f1438 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,8 @@ - Tuple unpacking on `return` and `yield` constructs now implies 3.8+ (#2700) - Unparenthesized tuples on annotated assignments (e.g `values: Tuple[int, ...] = 1, 2, 3`) now implies 3.8+ (#2708) +- Speed-up the new backtracking parser about 4X in general (enabled when + `--target-version` is set to 3.10 and higher). (#2728) ## 21.12b0 diff --git a/src/blib2to3/pgen2/parse.py b/src/blib2to3/pgen2/parse.py index e25f583d11c..8fe96672897 100644 --- a/src/blib2to3/pgen2/parse.py +++ b/src/blib2to3/pgen2/parse.py @@ -73,7 +73,7 @@ def ilabels(self) -> Set[int]: @contextmanager def switch_to(self, ilabel: int) -> Iterator[None]: - with self.patch(): + with self.backtrack(): self.parser.stack = self._points[ilabel] try: yield @@ -83,12 +83,11 @@ def switch_to(self, ilabel: int) -> Iterator[None]: self.parser.stack = self._start_point @contextmanager - def patch(self) -> Iterator[None]: + def backtrack(self) -> Iterator[None]: """ - Patch basic state operations (push/pop/shift) with node-level - immutable variants. These still will operate on the stack; but - they won't create any new nodes, or modify the contents of any - other existing nodes. + Use the node-level invariant ones for basic parsing operations (push/pop/shift). + These still will operate on the stack; but they won't create any new nodes, or + modify the contents of any other existing nodes. This saves us a ton of time when we are backtracking, since we want to restore to the initial state as quick as possible, which @@ -349,8 +348,6 @@ def classify(self, type: int, value: Text, context: Context) -> List[int]: raise ParseError("bad token", type, value, context) return [ilabel] - STATE_OPERATIONS = ["shift", "push", "pop"] - def shift(self, type: int, value: Text, newstate: int, context: Context) -> None: """Shift a token. (Internal)""" if self.is_backtracking: