Skip to content

Commit b9613c8

Browse files
authored
hints: Refactor out patch sorting key function (#450)
Move the helper function out of another function, to potentially improve performance a little for test cases with many files. Function-wise, this should be a no-op commit.
1 parent fecd402 commit b9613c8

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

cvise/utils/hint.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -563,23 +563,8 @@ def sort_hints(bundle: HintBundle) -> None:
563563

564564
def _merge_overlapping_patches(patches: Sequence[_PatchWithBundleRef]) -> Sequence[_PatchWithBundleRef]:
565565
"""Returns non-overlapping hint patches, merging patches where necessary."""
566-
567-
def sorting_key(ref: _PatchWithBundleRef):
568-
p = ref.patch
569-
is_replacement = p.value is not None
570-
# Criteria:
571-
# * prefer seeing positionless patches (without left/right) first;
572-
# * positioned patches should be first sorted by their left;
573-
# * prefer seeing larger patches first, hence sort by decreasing "r";
574-
# * (if still a tie) prefer deletion over text replacement.
575-
return (
576-
-math.inf if p.left is None else p.left,
577-
-math.inf if p.right is None else -p.right,
578-
is_replacement,
579-
)
580-
581566
merged: list[_PatchWithBundleRef] = []
582-
for cur in sorted(patches, key=sorting_key):
567+
for cur in sorted(patches, key=_patch_merge_sorting_key):
583568
if merged:
584569
prev = merged[-1]
585570
p = prev.patch
@@ -598,6 +583,22 @@ def sorting_key(ref: _PatchWithBundleRef):
598583
return merged
599584

600585

586+
def _patch_merge_sorting_key(ref: _PatchWithBundleRef) -> tuple:
587+
"""Sorting key used for merging overlapping patches."""
588+
p = ref.patch
589+
is_replacement = p.value is not None
590+
# Criteria:
591+
# * prefer seeing positionless patches (without left/right) first;
592+
# * positioned patches should be first sorted by their left;
593+
# * prefer seeing larger patches first, hence sort by decreasing "r";
594+
# * (if still a tie) prefer deletion over text replacement.
595+
return (
596+
-math.inf if p.left is None else p.left,
597+
-math.inf if p.right is None else -p.right,
598+
is_replacement,
599+
)
600+
601+
601602
def _lines_range(f: TextIO, begin_index: int | None, end_index: int | None) -> list[str]:
602603
for _ in range(begin_index or 0):
603604
next(f) # simply discard

0 commit comments

Comments
 (0)