Skip to content

Commit

Permalink
perf: improve position searching/dispatching
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarriga committed Dec 1, 2020
1 parent 5d2885d commit ea11517
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 13 additions & 11 deletions rplugin/python3/ultest/handler/positions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from typing import Callable, Dict, List, Optional
from time import sleep

from ..models import Position
from ..vim import VimClient
Expand Down Expand Up @@ -65,17 +66,18 @@ def runner(positions):
def _get_nearest_from(
self, positions: List[Position], current_line: int, strict: bool
):
# TODO: Binary search
last = None
for nearest in positions:
if nearest.line == current_line:
return nearest
if last and last.line < current_line < nearest.line:
return None if strict else last
last = nearest
if not strict and last and last.line < current_line:
return last
return None
l = 0
r = len(positions) - 1
while l <= r:
m = int((l + r) / 2)
mid = positions[m]
if mid.line < current_line:
l = m + 1
elif mid.line > current_line:
r = m - 1
else:
return mid
return positions[r] if not strict and len(positions) > r else None

def _calculate_positions(
self,
Expand Down
2 changes: 2 additions & 0 deletions rplugin/python3/ultest/handler/runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from uuid import uuid4
from time import sleep
import tempfile
import os
import os.path
Expand Down Expand Up @@ -55,3 +56,4 @@ def run_positions(self, positions: Iterable[Position]):
for test in tests:
self._vim.call("ultest#process#start", test)
self._vim.call("ultest#adapter#run_test", test)
sleep(0.03) # Minor sleep to avoid blocking main thread with IO

0 comments on commit ea11517

Please sign in to comment.