This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Support limiting down traversals from sources in FileDistance.
AbandonedPublic

Authored by ioeric on Oct 16 2018, 1:23 AM.

Details

Reviewers
sammccall
Summary

This is useful for symbo scope proximity, where down traversals from
the global scope if not desired. The limitation in the current implementation
is that we may lose coverage of paths that when are otherwise reachable from a
more expensive node with larger down traversal limit, as the greedy algorithm
favors nodes with cheapest costs.

Another option is to simply special-case the root path in FileDistance.

Event Timeline

ioeric created this revision.Oct 16 2018, 1:23 AM

The slightly weird down-traversals semantics are indeed slightly weird. I'm happy to move forward with this approach though if we document it.

I did think of one alternative:

  • instead of unsigned Source::MaxDownTraversals, have bool Source::AllowDownTraversals (obviously less flexible)
  • instead of (min-cost, max-down-traversals) in the cache, store (first=min-cost, second=min-cost-allowing-downtraversals).

When populating tree based on sources, set both cache values if down-traversals are allowed, otherwise just the first.
When propagating scores from parent to child, only read from the second cache value, and write to both.
When looking up a result from the cache, use the first cached value.

I don't think there's a lot of practical difference for our use case, the advantage would be the semantics are easier to reason about. WDYT?

clangd/FileDistance.cpp
84

I don't understand this if condition

90

For consistency, tiebreak with down traversals: || Node.Cost == R.first->second.Cost && Node.MaxDownTraversals > R.first->second.MaxDownTraversals (note the opposite sign)

But probably clearer to write this by defining CacheNode::operator<

107

This fills in the cache if it wasn't set - it's probably harmless but not quite as clear. Does lookup not work here?

clangd/FileDistance.h
65

nit: "unlimited" might be a clearer name here.

68

We should document the slightly weird semantics.

unittests/clangd/FileDistanceTests.cpp
124

, because /a itself is reached more cheaply via the root.

ioeric abandoned this revision.Oct 16 2018, 3:06 AM

As discussed offline, the semantics is getting a bit complicated. As what we really want for scope proximity is just disallow down traversals from root, we can go with special-casing the root path for now: D53317