Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule L003 performance: Cache the line number and last newline position #3060

Merged
merged 1 commit into from Apr 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/sqlfluff/rules/L003.py
Expand Up @@ -150,6 +150,9 @@ class _Memory:
in_indent: bool = True
trigger: Optional[BaseSegment] = None

line_no: int = dataclasses.field(default=1)
start_process_raw_idx: int = dataclasses.field(default=0)

@property
def noncomparable_lines(self):
return self.hanging_lines.union(self.problem_lines)
Expand Down Expand Up @@ -228,15 +231,17 @@ def _process_raw_stack(
starting_indent_balance = result_buffer[cached_line_count].indent_balance

working_state = _LineSummary(indent_balance=starting_indent_balance)
line_no = 1
started = line_no == (cached_line_count + 1)
for elem in raw_stack:

line_no = memory.line_no
target_line_no = cached_line_count + 1
for idx, elem in enumerate(raw_stack[memory.start_process_raw_idx :]):
is_newline = elem.is_type("newline")
# the below line act to reduce recalculation
if not started:
if line_no < target_line_no:
if is_newline:
line_no += 1
started = line_no == (cached_line_count + 1)
if line_no == target_line_no:
memory.start_process_raw_idx += idx + 1
memory.line_no = line_no
continue

working_state.line_buffer.append(elem)
Expand Down