Skip to content

Commit ec52f0f

Browse files
committed
perf(static): resolve prepared source offsets lazily
Signed-off-by: Christopher Kevin <[email protected]>
1 parent 3e39b67 commit ec52f0f

2 files changed

Lines changed: 106 additions & 6 deletions

File tree

‎src/skillspector/nodes/analyzers/static_runner.py‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,32 @@ def _bounded_view_slices(view: SecurityTextView) -> Iterator[SecurityTextView]:
799799
break
800800

801801

802+
@dataclass(frozen=True, kw_only=True)
803+
class _AbsoluteSourceView(SecurityTextView):
804+
"""Resolve absolute coordinates only for findings that need a source lookup.
805+
806+
This secondary view is used by prepared analysis and line restoration, never
807+
by text transformations that inspect or slice ``source_offsets`` directly.
808+
"""
809+
810+
derived_view: SecurityTextView
811+
parent_view: SecurityTextView | None
812+
source_start: int
813+
814+
def source_offset(self, derived_offset: int) -> int:
815+
offsets = self.derived_view.source_offsets
816+
size = len(self.text) if offsets is None else len(offsets)
817+
if size == 0:
818+
return 0
819+
# Materialized absolute maps clamp at the final character, including
820+
# identity-derived maps whose own endpoint lookup permits len(text).
821+
index = min(max(derived_offset, 0), size - 1)
822+
offset = self.derived_view.source_offset(index)
823+
if self.parent_view is not None:
824+
offset = self.parent_view.source_offset(offset)
825+
return self.source_start + offset
826+
827+
802828
def _absolute_source_view(
803829
view: SecurityTextView,
804830
*,
@@ -808,12 +834,13 @@ def _absolute_source_view(
808834
"""Compose one bounded view's coordinates with its whole-artifact origin."""
809835
if parent is None and source_start == 0:
810836
return view
811-
offsets = view.source_offsets if view.source_offsets is not None else range(len(view.text))
812-
if parent is not None:
813-
mapped = array("I", (source_start + parent.source_offset(offset) for offset in offsets))
814-
else:
815-
mapped = array("I", (source_start + offset for offset in offsets))
816-
return SecurityTextView(view.name, view.text, mapped)
837+
return _AbsoluteSourceView(
838+
name=view.name,
839+
text=view.text,
840+
derived_view=view,
841+
parent_view=parent,
842+
source_start=source_start,
843+
)
817844

818845

819846
def _whitespace_continuity_tokens(

‎tests/unit/test_static_runner_prepared_context.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,76 @@ def test_ignorable_separator_view_does_not_expand_p7_or_p8(command: str) -> None
196196
)
197197
assert findings == []
198198
assert reason is None
199+
200+
201+
@pytest.mark.parametrize("with_parent", [False, True], ids=["window", "parent"])
202+
def test_absolute_source_mapping_does_no_per_character_work_at_construction(
203+
monkeypatch: pytest.MonkeyPatch, with_parent: bool
204+
) -> None:
205+
class RecordingView(SecurityTextView):
206+
def source_offset(self, derived_offset: int) -> int:
207+
calls.append(derived_offset)
208+
return super().source_offset(derived_offset)
209+
210+
calls: list[int] = []
211+
view = SecurityTextView("raw", "x" * runner.SECURITY_VIEW_WINDOW_CHARS)
212+
parent = RecordingView("parent", view.text) if with_parent else None
213+
214+
def forbidden_array(*args, **kwargs):
215+
pytest.fail("Source mapping allocated an eager per-character array")
216+
217+
monkeypatch.setattr(runner, "array", forbidden_array)
218+
absolute = runner._absolute_source_view(view, source_start=123, parent=parent)
219+
assert calls == []
220+
assert absolute.source_offset(17) == 140
221+
assert calls == ([17] if with_parent else [])
222+
223+
224+
@pytest.mark.parametrize(
225+
("text", "source_offsets", "parent_offsets", "expected"),
226+
[
227+
("abc", None, None, [100, 100, 101, 102, 102, 102]),
228+
("abc", [2, 4, 7], None, [102, 102, 104, 107, 107, 107]),
229+
("abc", [0, 1, 1], [2, 4, 7], [102, 102, 104, 104, 104, 104]),
230+
("", [], [2, 4, 7], [0, 0, 0, 0, 0, 0]),
231+
],
232+
ids=["identity", "derived", "parent", "empty"],
233+
)
234+
def test_absolute_source_mapping_preserves_clamping_and_composition(
235+
text: str,
236+
source_offsets: list[int] | None,
237+
parent_offsets: list[int] | None,
238+
expected: list[int],
239+
) -> None:
240+
from array import array
241+
242+
view = SecurityTextView(
243+
"derived", text, None if source_offsets is None else array("I", source_offsets)
244+
)
245+
parent = (
246+
None
247+
if parent_offsets is None
248+
else SecurityTextView("parent", "abc", array("I", parent_offsets))
249+
)
250+
absolute = runner._absolute_source_view(view, source_start=100, parent=parent)
251+
assert absolute.name == view.name
252+
assert absolute.text == view.text
253+
assert [absolute.source_offset(i) for i in [-3, 0, 1, 2, 3, 99]] == expected
254+
255+
256+
def test_absolute_source_mapping_composes_bounded_derived_slice() -> None:
257+
text = "x" * (runner.SECURITY_VIEW_WINDOW_CHARS + 100)
258+
second = list(runner._bounded_view_slices(SecurityTextView("raw", text)))[1]
259+
absolute = runner._absolute_source_view(second, source_start=250_000)
260+
expected_start = 250_000 + runner.SECURITY_VIEW_WINDOW_CHARS - runner._WINDOW_OVERLAP_CHARS
261+
assert absolute.source_offset(0) == expected_start
262+
assert absolute.source_offset(len(second.text) - 1) == 250_000 + len(text) - 1
263+
264+
265+
def test_absolute_source_mapping_preserves_unshifted_identity_view() -> None:
266+
view = SecurityTextView("raw", "abc")
267+
absolute = runner._absolute_source_view(view)
268+
assert absolute is view
269+
assert absolute.source_offset(-1) == 0
270+
assert absolute.source_offset(3) == 3
271+
assert absolute.source_offset(99) == 3

0 commit comments

Comments
 (0)