2017-12-14 19:25:49 +08:00
|
|
|
//===--- MemIndex.cpp - Dynamic in-memory symbol index. ----------*- C++-*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-12-14 19:25:49 +08:00
|
|
|
//
|
|
|
|
//===-------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MemIndex.h"
|
2018-09-07 17:40:36 +08:00
|
|
|
#include "FuzzyMatch.h"
|
|
|
|
#include "Logger.h"
|
|
|
|
#include "Quality.h"
|
2018-09-28 02:23:23 +08:00
|
|
|
#include "Trace.h"
|
[clangd] Store explicit template specializations in index for code navigation purposes
Summary:
This introduces ~4k new symbols, and ~10k refs for LLVM. We need that
information for providing better code navigation support:
- When references for a class template is requested, we should return these specializations as well.
- When children of a specialization is requested, we should be able to query for those symbols(instead of just class template)
Number of symbols: 378574 -> 382784
Number of refs: 5098857 -> 5110689
Reviewers: hokein, gribozavr
Reviewed By: gribozavr
Subscribers: nridge, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59083
llvm-svn: 356125
2019-03-14 16:35:17 +08:00
|
|
|
#include "clang/Index/IndexSymbol.h"
|
2017-12-14 19:25:49 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
2019-06-15 10:26:47 +08:00
|
|
|
std::unique_ptr<SymbolIndex> MemIndex::build(SymbolSlab Slab, RefSlab Refs,
|
|
|
|
RelationSlab Relations) {
|
2018-09-10 19:46:07 +08:00
|
|
|
// Store Slab size before it is moved.
|
|
|
|
const auto BackingDataSize = Slab.bytes() + Refs.bytes();
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
auto Data = std::make_pair(std::move(Slab), std::move(Refs));
|
2019-08-15 07:52:23 +08:00
|
|
|
return std::make_unique<MemIndex>(Data.first, Data.second, Relations,
|
2019-06-15 10:26:47 +08:00
|
|
|
std::move(Data), BackingDataSize);
|
2018-08-21 18:32:27 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
bool MemIndex::fuzzyFind(
|
|
|
|
const FuzzyFindRequest &Req,
|
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const {
|
2017-12-19 19:37:40 +08:00
|
|
|
assert(!StringRef(Req.Query).contains("::") &&
|
|
|
|
"There must be no :: in query.");
|
2018-09-28 02:23:23 +08:00
|
|
|
trace::Span Tracer("MemIndex fuzzyFind");
|
2017-12-19 19:37:40 +08:00
|
|
|
|
2018-09-13 22:27:03 +08:00
|
|
|
TopN<std::pair<float, const Symbol *>> Top(
|
|
|
|
Req.Limit ? *Req.Limit : std::numeric_limits<size_t>::max());
|
2018-01-18 16:35:04 +08:00
|
|
|
FuzzyMatcher Filter(Req.Query);
|
|
|
|
bool More = false;
|
[clangd] Factor out the data-swapping functionality from MemIndex/DexIndex.
Summary:
This is now handled by a wrapper class SwapIndex, so MemIndex/DexIndex can be
immutable and focus on their job.
Old and busted:
I have a MemIndex, which holds a shared_ptr<vector<Symbol*>>, which keeps the
symbol slab alive. I update by calling build(shared_ptr<vector<Symbol*>>).
New hotness: I have a SwapIndex, which holds a unique_ptr<SymbolIndex>, which
holds a MemIndex, which holds a shared_ptr<void>, which keeps backing
data alive.
I update by building a new MemIndex and calling SwapIndex::reset().
Reviewers: kbobyrev, ioeric
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51422
llvm-svn: 341318
2018-09-03 22:37:43 +08:00
|
|
|
for (const auto Pair : Index) {
|
|
|
|
const Symbol *Sym = Pair.second;
|
2017-12-19 19:37:40 +08:00
|
|
|
|
[clangd] Factor out the data-swapping functionality from MemIndex/DexIndex.
Summary:
This is now handled by a wrapper class SwapIndex, so MemIndex/DexIndex can be
immutable and focus on their job.
Old and busted:
I have a MemIndex, which holds a shared_ptr<vector<Symbol*>>, which keeps the
symbol slab alive. I update by calling build(shared_ptr<vector<Symbol*>>).
New hotness: I have a SwapIndex, which holds a unique_ptr<SymbolIndex>, which
holds a MemIndex, which holds a shared_ptr<void>, which keeps backing
data alive.
I update by building a new MemIndex and calling SwapIndex::reset().
Reviewers: kbobyrev, ioeric
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51422
llvm-svn: 341318
2018-09-03 22:37:43 +08:00
|
|
|
// Exact match against all possible scopes.
|
2019-01-07 23:45:19 +08:00
|
|
|
if (!Req.AnyScope && !llvm::is_contained(Req.Scopes, Sym->Scope))
|
[clangd] Factor out the data-swapping functionality from MemIndex/DexIndex.
Summary:
This is now handled by a wrapper class SwapIndex, so MemIndex/DexIndex can be
immutable and focus on their job.
Old and busted:
I have a MemIndex, which holds a shared_ptr<vector<Symbol*>>, which keeps the
symbol slab alive. I update by calling build(shared_ptr<vector<Symbol*>>).
New hotness: I have a SwapIndex, which holds a unique_ptr<SymbolIndex>, which
holds a MemIndex, which holds a shared_ptr<void>, which keeps backing
data alive.
I update by building a new MemIndex and calling SwapIndex::reset().
Reviewers: kbobyrev, ioeric
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51422
llvm-svn: 341318
2018-09-03 22:37:43 +08:00
|
|
|
continue;
|
2018-09-07 02:52:26 +08:00
|
|
|
if (Req.RestrictForCodeCompletion &&
|
|
|
|
!(Sym->Flags & Symbol::IndexedForCodeCompletion))
|
[clangd] Factor out the data-swapping functionality from MemIndex/DexIndex.
Summary:
This is now handled by a wrapper class SwapIndex, so MemIndex/DexIndex can be
immutable and focus on their job.
Old and busted:
I have a MemIndex, which holds a shared_ptr<vector<Symbol*>>, which keeps the
symbol slab alive. I update by calling build(shared_ptr<vector<Symbol*>>).
New hotness: I have a SwapIndex, which holds a unique_ptr<SymbolIndex>, which
holds a MemIndex, which holds a shared_ptr<void>, which keeps backing
data alive.
I update by building a new MemIndex and calling SwapIndex::reset().
Reviewers: kbobyrev, ioeric
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51422
llvm-svn: 341318
2018-09-03 22:37:43 +08:00
|
|
|
continue;
|
2017-12-19 19:37:40 +08:00
|
|
|
|
2018-09-06 21:15:03 +08:00
|
|
|
if (auto Score = Filter.match(Sym->Name))
|
|
|
|
if (Top.push({*Score * quality(*Sym), Sym}))
|
|
|
|
More = true; // An element with smallest score was discarded.
|
2017-12-14 19:25:49 +08:00
|
|
|
}
|
2018-09-28 02:23:23 +08:00
|
|
|
auto Results = std::move(Top).items();
|
|
|
|
SPAN_ATTACH(Tracer, "results", static_cast<int>(Results.size()));
|
|
|
|
for (const auto &Item : Results)
|
2018-09-06 21:15:03 +08:00
|
|
|
Callback(*Item.second);
|
2018-01-18 16:35:04 +08:00
|
|
|
return More;
|
2017-12-14 19:25:49 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 17:48:05 +08:00
|
|
|
void MemIndex::lookup(const LookupRequest &Req,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const {
|
2018-09-28 02:23:23 +08:00
|
|
|
trace::Span Tracer("MemIndex lookup");
|
2018-03-14 17:48:05 +08:00
|
|
|
for (const auto &ID : Req.IDs) {
|
|
|
|
auto I = Index.find(ID);
|
|
|
|
if (I != Index.end())
|
|
|
|
Callback(*I->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
void MemIndex::refs(const RefsRequest &Req,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::function_ref<void(const Ref &)> Callback) const {
|
2018-09-28 02:23:23 +08:00
|
|
|
trace::Span Tracer("MemIndex refs");
|
2019-01-15 02:11:09 +08:00
|
|
|
uint32_t Remaining =
|
|
|
|
Req.Limit.getValueOr(std::numeric_limits<uint32_t>::max());
|
2018-09-01 03:53:37 +08:00
|
|
|
for (const auto &ReqID : Req.IDs) {
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
auto SymRefs = Refs.find(ReqID);
|
|
|
|
if (SymRefs == Refs.end())
|
2018-09-01 03:53:37 +08:00
|
|
|
continue;
|
2019-01-15 02:11:09 +08:00
|
|
|
for (const auto &O : SymRefs->second) {
|
|
|
|
if (Remaining > 0 && static_cast<int>(Req.Filter & O.Kind)) {
|
|
|
|
--Remaining;
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
Callback(O);
|
2019-01-15 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
2018-09-01 03:53:37 +08:00
|
|
|
}
|
2018-08-21 18:32:27 +08:00
|
|
|
}
|
|
|
|
|
2019-06-15 10:26:47 +08:00
|
|
|
void MemIndex::relations(
|
|
|
|
const RelationsRequest &Req,
|
|
|
|
llvm::function_ref<void(const SymbolID &, const Symbol &)> Callback) const {
|
|
|
|
uint32_t Remaining =
|
|
|
|
Req.Limit.getValueOr(std::numeric_limits<uint32_t>::max());
|
|
|
|
for (const SymbolID &Subject : Req.Subjects) {
|
|
|
|
LookupRequest LookupReq;
|
|
|
|
auto It = Relations.find(std::make_pair(Subject, Req.Predicate));
|
|
|
|
if (It != Relations.end()) {
|
|
|
|
for (const auto &Obj : It->second) {
|
|
|
|
if (Remaining > 0) {
|
|
|
|
--Remaining;
|
|
|
|
LookupReq.IDs.insert(Obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lookup(LookupReq, [&](const Symbol &Object) { Callback(Subject, Object); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 17:12:54 +08:00
|
|
|
size_t MemIndex::estimateMemoryUsage() const {
|
2019-06-15 10:26:47 +08:00
|
|
|
return Index.getMemorySize() + Refs.getMemorySize() +
|
|
|
|
Relations.getMemorySize() + BackingDataSize;
|
2018-08-24 17:12:54 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 19:25:49 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|