2017-12-14 19:25:49 +08:00
|
|
|
//===--- MemIndex.h - Dynamic in-memory symbol index. -------------- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
|
|
|
|
|
|
|
|
#include "Index.h"
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
[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
|
|
|
/// MemIndex is a naive in-memory index suitable for a small set of symbols.
|
2017-12-14 19:25:49 +08:00
|
|
|
class MemIndex : public SymbolIndex {
|
|
|
|
public:
|
2018-09-01 03:53:37 +08:00
|
|
|
/// Maps from a symbol ID to all corresponding symbol occurrences.
|
|
|
|
/// The map doesn't own occurrence objects.
|
|
|
|
using OccurrenceMap =
|
|
|
|
llvm::DenseMap<SymbolID, std::vector<const SymbolOccurrence *>>;
|
2017-12-14 19:25:49 +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
|
|
|
MemIndex() = default;
|
|
|
|
// All symbols and occurrences must outlive this index.
|
|
|
|
// TODO: find a better type for Occurrences here.
|
|
|
|
template <typename SymbolRange>
|
|
|
|
MemIndex(SymbolRange &&Symbols, OccurrenceMap Occurrences)
|
|
|
|
: Occurrences(std::move(Occurrences)) {
|
|
|
|
for (const Symbol &S : Symbols)
|
|
|
|
Index[S.ID] = &S;
|
|
|
|
}
|
|
|
|
// Symbols are owned by BackingData, Index takes ownership.
|
|
|
|
template <typename Range, typename Payload>
|
|
|
|
MemIndex(Range &&Symbols, OccurrenceMap Occurrences, Payload &&BackingData)
|
|
|
|
: MemIndex(std::forward<Range>(Symbols), std::move(Occurrences)) {
|
|
|
|
KeepAlive = std::shared_ptr<void>(
|
|
|
|
std::make_shared<Payload>(std::move(BackingData)), nullptr);
|
|
|
|
}
|
2018-09-01 03:53:37 +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
|
|
|
/// Builds an index from a slab. The index takes ownership of the data.
|
|
|
|
static std::unique_ptr<SymbolIndex> build(SymbolSlab Slab,
|
2018-09-01 03:53:37 +08:00
|
|
|
SymbolOccurrenceSlab Occurrences);
|
2018-01-10 22:44:34 +08:00
|
|
|
|
2017-12-28 22:47:01 +08:00
|
|
|
bool
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
fuzzyFind(const FuzzyFindRequest &Req,
|
2017-12-28 22:47:01 +08:00
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const override;
|
2017-12-14 19:25:49 +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
|
|
|
void lookup(const LookupRequest &Req,
|
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const override;
|
2018-03-14 17:48:05 +08:00
|
|
|
|
2018-08-06 21:14:32 +08:00
|
|
|
void findOccurrences(const OccurrencesRequest &Req,
|
|
|
|
llvm::function_ref<void(const SymbolOccurrence &)>
|
|
|
|
Callback) const override;
|
|
|
|
|
2018-08-24 17:12:54 +08:00
|
|
|
size_t estimateMemoryUsage() const override;
|
|
|
|
|
2017-12-14 19:25:49 +08:00
|
|
|
private:
|
|
|
|
// Index is a set of symbols that are deduplicated by symbol IDs.
|
|
|
|
llvm::DenseMap<SymbolID, const Symbol *> Index;
|
2018-09-01 03:53:37 +08:00
|
|
|
// A map from symbol ID to symbol occurrences, support query by IDs.
|
[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
|
|
|
OccurrenceMap Occurrences;
|
|
|
|
std::shared_ptr<void> KeepAlive; // poor man's move-only std::any
|
2017-12-14 19:25:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
|