Skip to content

Commit

Permalink
avoid unnecessary O(n^2) work in localuse (#768)
Browse files Browse the repository at this point in the history
Co-authored-by: WebFreak001 <gh@webfreak.org>
  • Loading branch information
brianush1 and WebFreak001 authored Dec 11, 2023
1 parent 584b245 commit 15ea4b3
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/dcd/server/autocomplete/localuse.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,21 @@ public AutocompleteResponse findLocalUse(AutocompleteRequest request,
config.fileName = "";
const(Token)[] tokenArray = getTokensForParser(cast(ubyte[]) request.sourceCode,
config, &cache);
auto sortedTokens = assumeSorted(tokenArray);
ScopeSymbolPair pair = generateAutocompleteTrees(tokenArray,
&rba, request.cursorPosition, moduleCache);
scope(exit) pair.destroy();

SymbolStuff getSymbolsAtCursor(size_t cursorPosition)
{
auto sortedTokens = assumeSorted(tokenArray);
auto beforeTokens = sortedTokens.lowerBound(cursorPosition);
ScopeSymbolPair pair = generateAutocompleteTrees(tokenArray,
&rba, request.cursorPosition, moduleCache);
auto expression = getExpression(beforeTokens);
return SymbolStuff(getSymbolsByTokenChain(pair.scope_, expression,
cursorPosition, CompletionType.location), pair.symbol, pair.scope_);
}

// gets the symbol matching to cursor pos
SymbolStuff stuff = getSymbolsAtCursor(cast(size_t)request.cursorPosition);
scope(exit) stuff.destroy();

// starts searching only if no ambiguity with the symbol
if (stuff.symbols.length == 1)
Expand Down Expand Up @@ -100,7 +101,6 @@ public AutocompleteResponse findLocalUse(AutocompleteRequest request,
{
size_t pos = cast(size_t) t.index + 1; // place cursor inside the token
SymbolStuff candidate = getSymbolsAtCursor(pos);
scope(exit) candidate.destroy();
if (candidate.symbols.length == 1 &&
candidate.symbols[0].location == sourceSymbol.location &&
candidate.symbols[0].symbolFile == sourceSymbol.symbolFile)
Expand Down

0 comments on commit 15ea4b3

Please sign in to comment.