DeltaTree.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. //===- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the DeltaTree and related classes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Rewrite/Core/DeltaTree.h"
  13. #include "clang/Basic/LLVM.h"
  14. #include "llvm/Support/Casting.h"
  15. #include <cassert>
  16. #include <cstring>
  17. using namespace clang;
  18. /// The DeltaTree class is a multiway search tree (BTree) structure with some
  19. /// fancy features. B-Trees are generally more memory and cache efficient
  20. /// than binary trees, because they store multiple keys/values in each node.
  21. ///
  22. /// DeltaTree implements a key/value mapping from FileIndex to Delta, allowing
  23. /// fast lookup by FileIndex. However, an added (important) bonus is that it
  24. /// can also efficiently tell us the full accumulated delta for a specific
  25. /// file offset as well, without traversing the whole tree.
  26. ///
  27. /// The nodes of the tree are made up of instances of two classes:
  28. /// DeltaTreeNode and DeltaTreeInteriorNode. The later subclasses the
  29. /// former and adds children pointers. Each node knows the full delta of all
  30. /// entries (recursively) contained inside of it, which allows us to get the
  31. /// full delta implied by a whole subtree in constant time.
  32. namespace {
  33. /// SourceDelta - As code in the original input buffer is added and deleted,
  34. /// SourceDelta records are used to keep track of how the input SourceLocation
  35. /// object is mapped into the output buffer.
  36. struct SourceDelta {
  37. unsigned FileLoc;
  38. int Delta;
  39. static SourceDelta get(unsigned Loc, int D) {
  40. SourceDelta Delta;
  41. Delta.FileLoc = Loc;
  42. Delta.Delta = D;
  43. return Delta;
  44. }
  45. };
  46. /// DeltaTreeNode - The common part of all nodes.
  47. ///
  48. class DeltaTreeNode {
  49. public:
  50. struct InsertResult {
  51. DeltaTreeNode *LHS, *RHS;
  52. SourceDelta Split;
  53. };
  54. private:
  55. friend class DeltaTreeInteriorNode;
  56. /// WidthFactor - This controls the number of K/V slots held in the BTree:
  57. /// how wide it is. Each level of the BTree is guaranteed to have at least
  58. /// WidthFactor-1 K/V pairs (except the root) and may have at most
  59. /// 2*WidthFactor-1 K/V pairs.
  60. enum { WidthFactor = 8 };
  61. /// Values - This tracks the SourceDelta's currently in this node.
  62. SourceDelta Values[2*WidthFactor-1];
  63. /// NumValuesUsed - This tracks the number of values this node currently
  64. /// holds.
  65. unsigned char NumValuesUsed = 0;
  66. /// IsLeaf - This is true if this is a leaf of the btree. If false, this is
  67. /// an interior node, and is actually an instance of DeltaTreeInteriorNode.
  68. bool IsLeaf;
  69. /// FullDelta - This is the full delta of all the values in this node and
  70. /// all children nodes.
  71. int FullDelta = 0;
  72. public:
  73. DeltaTreeNode(bool isLeaf = true) : IsLeaf(isLeaf) {}
  74. bool isLeaf() const { return IsLeaf; }
  75. int getFullDelta() const { return FullDelta; }
  76. bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; }
  77. unsigned getNumValuesUsed() const { return NumValuesUsed; }
  78. const SourceDelta &getValue(unsigned i) const {
  79. assert(i < NumValuesUsed && "Invalid value #");
  80. return Values[i];
  81. }
  82. SourceDelta &getValue(unsigned i) {
  83. assert(i < NumValuesUsed && "Invalid value #");
  84. return Values[i];
  85. }
  86. /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
  87. /// this node. If insertion is easy, do it and return false. Otherwise,
  88. /// split the node, populate InsertRes with info about the split, and return
  89. /// true.
  90. bool DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes);
  91. void DoSplit(InsertResult &InsertRes);
  92. /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
  93. /// local walk over our contained deltas.
  94. void RecomputeFullDeltaLocally();
  95. void Destroy();
  96. };
  97. /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers.
  98. /// This class tracks them.
  99. class DeltaTreeInteriorNode : public DeltaTreeNode {
  100. friend class DeltaTreeNode;
  101. DeltaTreeNode *Children[2*WidthFactor];
  102. ~DeltaTreeInteriorNode() {
  103. for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i)
  104. Children[i]->Destroy();
  105. }
  106. public:
  107. DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {}
  108. DeltaTreeInteriorNode(const InsertResult &IR)
  109. : DeltaTreeNode(false /*nonleaf*/) {
  110. Children[0] = IR.LHS;
  111. Children[1] = IR.RHS;
  112. Values[0] = IR.Split;
  113. FullDelta = IR.LHS->getFullDelta()+IR.RHS->getFullDelta()+IR.Split.Delta;
  114. NumValuesUsed = 1;
  115. }
  116. const DeltaTreeNode *getChild(unsigned i) const {
  117. assert(i < getNumValuesUsed()+1 && "Invalid child");
  118. return Children[i];
  119. }
  120. DeltaTreeNode *getChild(unsigned i) {
  121. assert(i < getNumValuesUsed()+1 && "Invalid child");
  122. return Children[i];
  123. }
  124. static bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); }
  125. };
  126. } // namespace
  127. /// Destroy - A 'virtual' destructor.
  128. void DeltaTreeNode::Destroy() {
  129. if (isLeaf())
  130. delete this;
  131. else
  132. delete cast<DeltaTreeInteriorNode>(this);
  133. }
  134. /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
  135. /// local walk over our contained deltas.
  136. void DeltaTreeNode::RecomputeFullDeltaLocally() {
  137. int NewFullDelta = 0;
  138. for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i)
  139. NewFullDelta += Values[i].Delta;
  140. if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this))
  141. for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i)
  142. NewFullDelta += IN->getChild(i)->getFullDelta();
  143. FullDelta = NewFullDelta;
  144. }
  145. /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
  146. /// this node. If insertion is easy, do it and return false. Otherwise,
  147. /// split the node, populate InsertRes with info about the split, and return
  148. /// true.
  149. bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta,
  150. InsertResult *InsertRes) {
  151. // Maintain full delta for this node.
  152. FullDelta += Delta;
  153. // Find the insertion point, the first delta whose index is >= FileIndex.
  154. unsigned i = 0, e = getNumValuesUsed();
  155. while (i != e && FileIndex > getValue(i).FileLoc)
  156. ++i;
  157. // If we found an a record for exactly this file index, just merge this
  158. // value into the pre-existing record and finish early.
  159. if (i != e && getValue(i).FileLoc == FileIndex) {
  160. // NOTE: Delta could drop to zero here. This means that the delta entry is
  161. // useless and could be removed. Supporting erases is more complex than
  162. // leaving an entry with Delta=0, so we just leave an entry with Delta=0 in
  163. // the tree.
  164. Values[i].Delta += Delta;
  165. return false;
  166. }
  167. // Otherwise, we found an insertion point, and we know that the value at the
  168. // specified index is > FileIndex. Handle the leaf case first.
  169. if (isLeaf()) {
  170. if (!isFull()) {
  171. // For an insertion into a non-full leaf node, just insert the value in
  172. // its sorted position. This requires moving later values over.
  173. if (i != e)
  174. memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i));
  175. Values[i] = SourceDelta::get(FileIndex, Delta);
  176. ++NumValuesUsed;
  177. return false;
  178. }
  179. // Otherwise, if this is leaf is full, split the node at its median, insert
  180. // the value into one of the children, and return the result.
  181. assert(InsertRes && "No result location specified");
  182. DoSplit(*InsertRes);
  183. if (InsertRes->Split.FileLoc > FileIndex)
  184. InsertRes->LHS->DoInsertion(FileIndex, Delta, nullptr /*can't fail*/);
  185. else
  186. InsertRes->RHS->DoInsertion(FileIndex, Delta, nullptr /*can't fail*/);
  187. return true;
  188. }
  189. // Otherwise, this is an interior node. Send the request down the tree.
  190. auto *IN = cast<DeltaTreeInteriorNode>(this);
  191. if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes))
  192. return false; // If there was space in the child, just return.
  193. // Okay, this split the subtree, producing a new value and two children to
  194. // insert here. If this node is non-full, we can just insert it directly.
  195. if (!isFull()) {
  196. // Now that we have two nodes and a new element, insert the perclated value
  197. // into ourself by moving all the later values/children down, then inserting
  198. // the new one.
  199. if (i != e)
  200. memmove(&IN->Children[i+2], &IN->Children[i+1],
  201. (e-i)*sizeof(IN->Children[0]));
  202. IN->Children[i] = InsertRes->LHS;
  203. IN->Children[i+1] = InsertRes->RHS;
  204. if (e != i)
  205. memmove(&Values[i+1], &Values[i], (e-i)*sizeof(Values[0]));
  206. Values[i] = InsertRes->Split;
  207. ++NumValuesUsed;
  208. return false;
  209. }
  210. // Finally, if this interior node was full and a node is percolated up, split
  211. // ourself and return that up the chain. Start by saving all our info to
  212. // avoid having the split clobber it.
  213. IN->Children[i] = InsertRes->LHS;
  214. DeltaTreeNode *SubRHS = InsertRes->RHS;
  215. SourceDelta SubSplit = InsertRes->Split;
  216. // Do the split.
  217. DoSplit(*InsertRes);
  218. // Figure out where to insert SubRHS/NewSplit.
  219. DeltaTreeInteriorNode *InsertSide;
  220. if (SubSplit.FileLoc < InsertRes->Split.FileLoc)
  221. InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->LHS);
  222. else
  223. InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->RHS);
  224. // We now have a non-empty interior node 'InsertSide' to insert
  225. // SubRHS/SubSplit into. Find out where to insert SubSplit.
  226. // Find the insertion point, the first delta whose index is >SubSplit.FileLoc.
  227. i = 0; e = InsertSide->getNumValuesUsed();
  228. while (i != e && SubSplit.FileLoc > InsertSide->getValue(i).FileLoc)
  229. ++i;
  230. // Now we know that i is the place to insert the split value into. Insert it
  231. // and the child right after it.
  232. if (i != e)
  233. memmove(&InsertSide->Children[i+2], &InsertSide->Children[i+1],
  234. (e-i)*sizeof(IN->Children[0]));
  235. InsertSide->Children[i+1] = SubRHS;
  236. if (e != i)
  237. memmove(&InsertSide->Values[i+1], &InsertSide->Values[i],
  238. (e-i)*sizeof(Values[0]));
  239. InsertSide->Values[i] = SubSplit;
  240. ++InsertSide->NumValuesUsed;
  241. InsertSide->FullDelta += SubSplit.Delta + SubRHS->getFullDelta();
  242. return true;
  243. }
  244. /// DoSplit - Split the currently full node (which has 2*WidthFactor-1 values)
  245. /// into two subtrees each with "WidthFactor-1" values and a pivot value.
  246. /// Return the pieces in InsertRes.
  247. void DeltaTreeNode::DoSplit(InsertResult &InsertRes) {
  248. assert(isFull() && "Why split a non-full node?");
  249. // Since this node is full, it contains 2*WidthFactor-1 values. We move
  250. // the first 'WidthFactor-1' values to the LHS child (which we leave in this
  251. // node), propagate one value up, and move the last 'WidthFactor-1' values
  252. // into the RHS child.
  253. // Create the new child node.
  254. DeltaTreeNode *NewNode;
  255. if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this)) {
  256. // If this is an interior node, also move over 'WidthFactor' children
  257. // into the new node.
  258. DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode();
  259. memcpy(&New->Children[0], &IN->Children[WidthFactor],
  260. WidthFactor*sizeof(IN->Children[0]));
  261. NewNode = New;
  262. } else {
  263. // Just create the new leaf node.
  264. NewNode = new DeltaTreeNode();
  265. }
  266. // Move over the last 'WidthFactor-1' values from here to NewNode.
  267. memcpy(&NewNode->Values[0], &Values[WidthFactor],
  268. (WidthFactor-1)*sizeof(Values[0]));
  269. // Decrease the number of values in the two nodes.
  270. NewNode->NumValuesUsed = NumValuesUsed = WidthFactor-1;
  271. // Recompute the two nodes' full delta.
  272. NewNode->RecomputeFullDeltaLocally();
  273. RecomputeFullDeltaLocally();
  274. InsertRes.LHS = this;
  275. InsertRes.RHS = NewNode;
  276. InsertRes.Split = Values[WidthFactor-1];
  277. }
  278. //===----------------------------------------------------------------------===//
  279. // DeltaTree Implementation
  280. //===----------------------------------------------------------------------===//
  281. //#define VERIFY_TREE
  282. #ifdef VERIFY_TREE
  283. /// VerifyTree - Walk the btree performing assertions on various properties to
  284. /// verify consistency. This is useful for debugging new changes to the tree.
  285. static void VerifyTree(const DeltaTreeNode *N) {
  286. const auto *IN = dyn_cast<DeltaTreeInteriorNode>(N);
  287. if (IN == 0) {
  288. // Verify leaves, just ensure that FullDelta matches up and the elements
  289. // are in proper order.
  290. int FullDelta = 0;
  291. for (unsigned i = 0, e = N->getNumValuesUsed(); i != e; ++i) {
  292. if (i)
  293. assert(N->getValue(i-1).FileLoc < N->getValue(i).FileLoc);
  294. FullDelta += N->getValue(i).Delta;
  295. }
  296. assert(FullDelta == N->getFullDelta());
  297. return;
  298. }
  299. // Verify interior nodes: Ensure that FullDelta matches up and the
  300. // elements are in proper order and the children are in proper order.
  301. int FullDelta = 0;
  302. for (unsigned i = 0, e = IN->getNumValuesUsed(); i != e; ++i) {
  303. const SourceDelta &IVal = N->getValue(i);
  304. const DeltaTreeNode *IChild = IN->getChild(i);
  305. if (i)
  306. assert(IN->getValue(i-1).FileLoc < IVal.FileLoc);
  307. FullDelta += IVal.Delta;
  308. FullDelta += IChild->getFullDelta();
  309. // The largest value in child #i should be smaller than FileLoc.
  310. assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc <
  311. IVal.FileLoc);
  312. // The smallest value in child #i+1 should be larger than FileLoc.
  313. assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc);
  314. VerifyTree(IChild);
  315. }
  316. FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta();
  317. assert(FullDelta == N->getFullDelta());
  318. }
  319. #endif // VERIFY_TREE
  320. static DeltaTreeNode *getRoot(void *Root) {
  321. return (DeltaTreeNode*)Root;
  322. }
  323. DeltaTree::DeltaTree() {
  324. Root = new DeltaTreeNode();
  325. }
  326. DeltaTree::DeltaTree(const DeltaTree &RHS) {
  327. // Currently we only support copying when the RHS is empty.
  328. assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 &&
  329. "Can only copy empty tree");
  330. Root = new DeltaTreeNode();
  331. }
  332. DeltaTree::~DeltaTree() {
  333. getRoot(Root)->Destroy();
  334. }
  335. /// getDeltaAt - Return the accumulated delta at the specified file offset.
  336. /// This includes all insertions or delections that occurred *before* the
  337. /// specified file index.
  338. int DeltaTree::getDeltaAt(unsigned FileIndex) const {
  339. const DeltaTreeNode *Node = getRoot(Root);
  340. int Result = 0;
  341. // Walk down the tree.
  342. while (true) {
  343. // For all nodes, include any local deltas before the specified file
  344. // index by summing them up directly. Keep track of how many were
  345. // included.
  346. unsigned NumValsGreater = 0;
  347. for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e;
  348. ++NumValsGreater) {
  349. const SourceDelta &Val = Node->getValue(NumValsGreater);
  350. if (Val.FileLoc >= FileIndex)
  351. break;
  352. Result += Val.Delta;
  353. }
  354. // If we have an interior node, include information about children and
  355. // recurse. Otherwise, if we have a leaf, we're done.
  356. const auto *IN = dyn_cast<DeltaTreeInteriorNode>(Node);
  357. if (!IN) return Result;
  358. // Include any children to the left of the values we skipped, all of
  359. // their deltas should be included as well.
  360. for (unsigned i = 0; i != NumValsGreater; ++i)
  361. Result += IN->getChild(i)->getFullDelta();
  362. // If we found exactly the value we were looking for, break off the
  363. // search early. There is no need to search the RHS of the value for
  364. // partial results.
  365. if (NumValsGreater != Node->getNumValuesUsed() &&
  366. Node->getValue(NumValsGreater).FileLoc == FileIndex)
  367. return Result+IN->getChild(NumValsGreater)->getFullDelta();
  368. // Otherwise, traverse down the tree. The selected subtree may be
  369. // partially included in the range.
  370. Node = IN->getChild(NumValsGreater);
  371. }
  372. // NOT REACHED.
  373. }
  374. /// AddDelta - When a change is made that shifts around the text buffer,
  375. /// this method is used to record that info. It inserts a delta of 'Delta'
  376. /// into the current DeltaTree at offset FileIndex.
  377. void DeltaTree::AddDelta(unsigned FileIndex, int Delta) {
  378. assert(Delta && "Adding a noop?");
  379. DeltaTreeNode *MyRoot = getRoot(Root);
  380. DeltaTreeNode::InsertResult InsertRes;
  381. if (MyRoot->DoInsertion(FileIndex, Delta, &InsertRes)) {
  382. Root = new DeltaTreeInteriorNode(InsertRes);
  383. #ifdef VERIFY_TREE
  384. MyRoot = Root;
  385. #endif
  386. }
  387. #ifdef VERIFY_TREE
  388. VerifyTree(MyRoot);
  389. #endif
  390. }