Skip to content

Commit f01a14a

Browse files
Fix bugs and performance issues in rewrite-python (#6851)
* Fix bugs and performance issues in rewrite-python - Fix shared class-level mutable dict in InMemoryExecutionContext that caused state leakage between instances - Fix swapped last_access_time/last_modified_time args in FileAttributes.from_path - Fix error message in Cursor.first_enclosing_or_throw using wrong variable (T.__name__ instead of type.__name__) - Use identity check (is not) instead of equality (!=) in all 63 PaddingHelper.padding properties to avoid O(n) __eq__ comparisons - Use deque.popleft() instead of list.pop(0) in RpcReceiveQueue for O(1) message consumption - Combine multiple replace() calls into single operations in visit_right_padded and parse_python_source - Remove unused imports and dead code * Extract shared utilities and buffer RPC stdin reads in rewrite-python - Extract duplicated import helpers (get_qualid_name, get_name_string, get_alias_name, pad_right) into import_utils.py, replacing copies in add_import.py, remove_import.py, and change_import.py - Extract duplicated format helpers (common_margin, concatenate_prefix) into format/_helpers.py, replacing copies in minimum_viable_spacing.py and normalize_format.py - Replace byte-at-a-time os.read(fd, 1) in RPC server with a buffered _StdinBuffer that reads in 8 KB chunks, reducing syscalls per message from ~25+ to 1-2
1 parent dd5dcb9 commit f01a14a

18 files changed

Lines changed: 320 additions & 391 deletions

rewrite-python/rewrite/src/rewrite/discovery.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818

1919
import inspect
2020
from importlib.metadata import entry_points
21-
from typing import List, Tuple, Type, TYPE_CHECKING
21+
from typing import List, Tuple, Type
2222

2323
from rewrite.category import CategoryDescriptor
2424
from rewrite.decorators import get_recipe_category
2525
from rewrite.marketplace import RecipeMarketplace
2626
from rewrite.recipe import Recipe
2727

28-
if TYPE_CHECKING:
29-
pass
30-
3128

3229
def discover_recipes() -> RecipeMarketplace:
3330
"""

rewrite-python/rewrite/src/rewrite/execution.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def put_message(self, key, value):
3636

3737

3838
class InMemoryExecutionContext(ExecutionContext):
39-
_messages: dict[str, Any] = {}
39+
def __init__(self):
40+
self._messages: dict[str, Any] = {}
4041

4142
def get_message(self, key: str, default_value=None) -> Any:
4243
return self._messages[key] if key in self._messages else default_value

rewrite-python/rewrite/src/rewrite/java/extensions.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ def visit_right_padded(v: 'JavaVisitor', right: Optional[JRightPadded[T]], p: P)
3535
if t is None:
3636
return None
3737

38-
right = right.replace(element=t)
39-
right = right.replace(after=v.visit_space(right.after, p))
40-
right = right.replace(markers=v.visit_markers(right.markers, p))
41-
return right
38+
after = v.visit_space(right.after, p)
39+
markers = v.visit_markers(right.markers, p)
40+
if t is right.element and after is right.after and markers is right.markers:
41+
return right
42+
return JRightPadded(t, after, markers)
4243

4344

4445
def visit_left_padded(v: 'JavaVisitor', left: Optional[JLeftPadded[T]], p: P) -> Optional[JLeftPadded[T]]:

rewrite-python/rewrite/src/rewrite/java/support_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ def padding(self) -> JContainer.PaddingHelper[J2]:
569569
else:
570570
p = self._padding()
571571
# noinspection PyProtectedMember
572-
if p is None or p._t != self:
572+
if p is None or p._t is not self:
573573
p = JContainer.PaddingHelper(self)
574574
object.__setattr__(self, '_padding', weakref.ref(p))
575575
return p

0 commit comments

Comments
 (0)