This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Speed up when building rename edit.
ClosedPublic

Authored by hokein on Nov 19 2019, 6:44 AM.

Details

Summary

We used to scan the code everytime when computing the LSP position to the offset
(respect the LSP encoding).

Diff Detail

Event Timeline

hokein created this revision.Nov 19 2019, 6:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 19 2019, 6:44 AM
ilya-biryukov added inline comments.Nov 20 2019, 1:04 AM
clang-tools-extra/clangd/SourceCode.h
54 ↗(On Diff #230065)

How this is different from positionToOffset with Pos.line = 0 and Pos.column = LSPCharacter?
Why do we need an extra function?

clang-tools-extra/clangd/refactor/Rename.cpp
444

We are aiming to convert all ranges in O(n) instead of O(n^2), right?
I think this could be simpler and also have guaranteed performance even for long lines if we do it slightly differently. WDYT?

assert(llvm::is_sorted(Occurences));
// These two always correspond to the same position.
Position LastPos = Position{0, 0};
size_t LastOffset = 0;

std::vector<pair<size_t, size_t>> Offsets;
for (auto R : Occurences) {
  Position ShiftedStart = R.start - LastPos;
  size_t ShiftedStartOffset = positionToOffset(ShiftedStart, Code.substr(LastOffset);

  LastPos = ShiftedStart;
  LastOffset  = ShiftedStartOffset;
  // Do the same for the end position.
  // ...
}

// Now we can easily replacements.
// ...
hokein updated this revision to Diff 230242.Nov 20 2019, 5:44 AM
hokein marked 2 inline comments as done.

address comments.

hokein added inline comments.Nov 20 2019, 5:45 AM
clang-tools-extra/clangd/SourceCode.h
54 ↗(On Diff #230065)

removed now, not needed.

clang-tools-extra/clangd/refactor/Rename.cpp
444

agree, this is smart. note that we need to pay the cost of sorting, but I think it is totally ok as the number is relatively small.

ilya-biryukov accepted this revision.Nov 20 2019, 7:00 AM

LGTM

clang-tools-extra/clangd/refactor/Rename.cpp
453

add assert(LastPos <= P) to protect against malformed inputs (e.g. if one of occurence ranges would have an endpoint that is before the starting point).

458

NIT: braces are redundant and can be dropped.

459

Could we simply propagate error?
I believe we mostly choose simpler code over more detailed error messages in clangd.

This revision is now accepted and ready to land.Nov 20 2019, 7:00 AM
hokein updated this revision to Diff 230261.Nov 20 2019, 7:20 AM
hokein marked 3 inline comments as done.

address comments.

hokein updated this revision to Diff 230264.Nov 20 2019, 7:29 AM

update the test.

This revision was automatically updated to reflect the committed changes.