]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

black/parser: optimize deepcopying nodes (#2611)
authorBatuhan Taskaya <isidentical@gmail.com>
Tue, 16 Nov 2021 02:38:40 +0000 (05:38 +0300)
committerGitHub <noreply@github.com>
Tue, 16 Nov 2021 02:38:40 +0000 (18:38 -0800)
The implementation of the new backtracking logic depends heavily on deepcopying the current state of the parser before seeing one of the new keywords, which by default is an very expensive operations. On my system, formatting these 3 files takes 1.3 seconds.

```
 $ touch tests/data/pattern_matching_*; time python -m black -tpy310 tests/data/pattern_matching_*             19ms
All done! ✨ 🍰 ✨
3 files left unchanged.
python -m black -tpy310 tests/data/pattern_matching_*  2,09s user 0,04s system 157% cpu 1,357 total
```

which can be optimized 3X if we integrate the existing copying logic (`clone`) to the deepcopy system;
```
 $ touch tests/data/pattern_matching_*; time python -m black -tpy310 tests/data/pattern_matching_*              1ms
All done! ✨ 🍰 ✨
3 files left unchanged.
python -m black -tpy310 tests/data/pattern_matching_*  0,66s user 0,02s system 147% cpu 0,464 total
```

This still might have some potential, but that would be way trickier than this initial patch.

src/blib2to3/pytree.py
tests/data/pattern_matching_extras.py

index 7843467e0129d2c3f281ffab3ff76da7d53001f3..001652df09fb85d623ede558f75a2ea102549e4e 100644 (file)
@@ -52,7 +52,7 @@ def type_repr(type_num: int) -> Union[Text, int]:
     return _type_reprs.setdefault(type_num, type_num)
 
 
     return _type_reprs.setdefault(type_num, type_num)
 
 
-_P = TypeVar("_P")
+_P = TypeVar("_P", bound="Base")
 
 NL = Union["Node", "Leaf"]
 Context = Tuple[Text, Tuple[int, int]]
 
 NL = Union["Node", "Leaf"]
 Context = Tuple[Text, Tuple[int, int]]
@@ -109,6 +109,9 @@ class Base(object):
         """
         raise NotImplementedError
 
         """
         raise NotImplementedError
 
+    def __deepcopy__(self: _P, memo: Any) -> _P:
+        return self.clone()
+
     def clone(self: _P) -> _P:
         """
         Return a cloned (deep) copy of self.
     def clone(self: _P) -> _P:
         """
         Return a cloned (deep) copy of self.
index b17922d608bf8cfe49b0df1e98dc228e265c48ee..614e66aebe65ee3039eb4df24d7a746bf56a107a 100644 (file)
@@ -1,3 +1,5 @@
+import match
+
 match something:
     case [a as b]:
         print(b)
 match something:
     case [a as b]:
         print(b)
@@ -7,3 +9,21 @@ match something:
         print(b)
     case Point(int() as x, int() as y):
         print(x, y)
         print(b)
     case Point(int() as x, int() as y):
         print(x, y)
+
+
+match = 1
+case: int = re.match(something)
+
+match re.match(case):
+    case type("match", match):
+        pass
+    case match:
+        pass
+
+
+def func(match: case, case: match) -> case:
+    match Something():
+        case another:
+            ...
+        case func(match, case):
+            ...