[PATCH] D50955: [clangd] Implement TRUE Iterator
Kirill Bobyrev via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 20 01:10:55 PDT 2018
kbobyrev created this revision.
kbobyrev added a reviewer: ioeric.
Herald added subscribers: kadircet, arphaman, jkorous, MaskRay.
This patch introduces TRUE Iterator which efficiently handles posting lists containing all items within `[0, Size)` range.
https://reviews.llvm.org/D50955
Files:
clang-tools-extra/clangd/index/dex/Iterator.cpp
clang-tools-extra/clangd/index/dex/Iterator.h
clang-tools-extra/unittests/clangd/DexIndexTests.cpp
Index: clang-tools-extra/unittests/clangd/DexIndexTests.cpp
===================================================================
--- clang-tools-extra/unittests/clangd/DexIndexTests.cpp
+++ clang-tools-extra/unittests/clangd/DexIndexTests.cpp
@@ -262,6 +262,19 @@
EXPECT_THAT(consume(*DocIterator, 0), ElementsAre());
}
+TEST(DexIndexIterators, True) {
+ auto TrueIterator = createTrue(0U);
+ EXPECT_THAT(TrueIterator->reachedEnd(), true);
+ EXPECT_THAT(consume(*TrueIterator), ElementsAre());
+
+ PostingList L0 = {1, 2, 5, 7};
+ TrueIterator = createTrue(7U);
+ EXPECT_THAT(TrueIterator->peek(), 0);
+ auto AndIterator = createAnd(create(L0), move(TrueIterator));
+ EXPECT_THAT(AndIterator->reachedEnd(), false);
+ EXPECT_THAT(consume(*AndIterator), ElementsAre(1, 2, 5));
+}
+
testing::Matcher<std::vector<Token>>
trigramsAre(std::initializer_list<std::string> Trigrams) {
std::vector<Token> Tokens;
Index: clang-tools-extra/clangd/index/dex/Iterator.h
===================================================================
--- clang-tools-extra/clangd/index/dex/Iterator.h
+++ clang-tools-extra/clangd/index/dex/Iterator.h
@@ -129,6 +129,10 @@
std::unique_ptr<Iterator>
createOr(std::vector<std::unique_ptr<Iterator>> Children);
+/// Returns TRUE Iterator which iterates over "virtual" PostingList containing
+/// all items in range [0, Size) in an efficient manner.
+std::unique_ptr<Iterator> createTrue(DocID Size);
+
/// This allows createAnd(create(...), create(...)) syntax.
template <typename... Args> std::unique_ptr<Iterator> createAnd(Args... args) {
std::vector<std::unique_ptr<Iterator>> Children;
Index: clang-tools-extra/clangd/index/dex/Iterator.cpp
===================================================================
--- clang-tools-extra/clangd/index/dex/Iterator.cpp
+++ clang-tools-extra/clangd/index/dex/Iterator.cpp
@@ -225,6 +225,45 @@
std::vector<std::unique_ptr<Iterator>> Children;
};
+/// TrueIterator handles PostingLists which contain all items of the index. In
+/// order to prevent additional memory consumption, it only stores the size of
+/// this virtual posting list because posting lists of such density are likely
+/// to consume a lot of memory. All operations are performed in O(1) as a
+/// result which also significantly improves performance of the iterator,
+/// because iterating such posting list via DocumentIterator would result in
+/// increased cost of advanceTo().
+class TrueIterator : public Iterator {
+public:
+ TrueIterator(DocID Size) : Size(Size) {}
+
+ bool reachedEnd() const override { return Index >= Size; }
+
+ void advance() override {
+ assert(!reachedEnd() && "Can't advance iterator after it reached the end.");
+ ++Index;
+ }
+
+ void advanceTo(DocID ID) override {
+ assert(!reachedEnd() && "Can't advance iterator after it reached the end.");
+ Index = ID;
+ }
+
+ DocID peek() const override {
+ assert(!reachedEnd() && "TrueIterator can't call peek() at the end.");
+ return Index;
+ }
+
+private:
+ llvm::raw_ostream &dump(llvm::raw_ostream &OS) const override {
+ OS << "(TrueIterator {" << Index << "} out of " << Size << ")";
+ return OS;
+ }
+
+ DocID Index = 0;
+ /// Size of the underlying virtual PostingList.
+ DocID Size;
+};
+
} // end namespace
std::vector<DocID> consume(Iterator &It, size_t Limit) {
@@ -249,6 +288,10 @@
return llvm::make_unique<OrIterator>(move(Children));
}
+std::unique_ptr<Iterator> createTrue(DocID Size) {
+ return llvm::make_unique<TrueIterator>(Size);
+}
+
} // namespace dex
} // namespace clangd
} // namespace clang
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50955.161430.patch
Type: text/x-patch
Size: 3614 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180820/21993e65/attachment.bin>
More information about the cfe-commits
mailing list