Skip to content

Commit 7983e36

Browse files
committed
Add ascii-only line optimisation
The vast majority of Python source code is going to be mostly ascii-only text, we can completely skip the entire unicode encode/decode overhead when there's only ascii characters.
1 parent c66a85c commit 7983e36

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

‎rope/base/codeanalyze.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,19 @@ def _get_line_bytes(self, line_idx: int, col_offset: int | None = None) -> bytes
144144
end = min(end, start + col_offset)
145145
return self._code_bytes[start:end].rstrip(b"\r\n")
146146

147+
def _is_line_ascii_only(self, line_idx: int):
148+
start_str = self._starts_str[line_idx]
149+
end_str = self._starts_str[line_idx + 1]
150+
start_bytes = self._starts_bytes[line_idx]
151+
end_bytes = self._starts_bytes[line_idx + 1]
152+
# str length == bytes length iff they're ascii-only because any
153+
# non-ascii characters would be at least 2 bytes
154+
return (end_str - start_str) == (end_bytes - start_bytes)
155+
147156
def _line_region_offset(self, line_idx: int, col_offset: int) -> int:
148157
"""str offset relative to the start of line"""
149-
if col_offset == 0:
150-
return 0
158+
if col_offset == 0 or self._is_line_ascii_only(line_idx):
159+
return col_offset
151160
prefix = self._get_line_text(line_idx, col_offset)
152161
return len(prefix)
153162

0 commit comments

Comments
 (0)