llvm.org GIT mirror llvm / 7c5c12b
Add an iplist::clearAndLeakNodesUnsafely() function. The iplist::clear() function can be quite expensive because it traverses the entire list, calling deleteNode() and removeNodeFromList() on each element. If node destruction and deallocation can be handled some other way, clearAndLeakNodesUnsafely() can be used to jettison all nodes without bringing them into cache. The function name is meant to be ominous. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171540 91177308-0d34-0410-b5e6-96231b3b80d8 Jakob Stoklund Olesen 6 years ago
2 changed file(s) with 44 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
464464 return where;
465465 }
466466
467 /// Remove all nodes from the list like clear(), but do not call
468 /// removeNodeFromList() or deleteNode().
469 ///
470 /// This should only be used immediately before freeing nodes in bulk to
471 /// avoid traversing the list and bringing all the nodes into cache.
472 void clearAndLeakNodesUnsafely() {
473 if (Head) {
474 Head = getTail();
475 this->setPrev(Head, Head);
476 }
477 }
467478
468479 private:
469480 // transfer - The heart of the splice function. Move linked list nodes from
2121
2222 Node() {}
2323 Node(int _Value) : Value(_Value) {}
24 ~Node() { Value = -1; }
2425 };
2526
2627 TEST(ilistTest, Basic) {
6162 EXPECT_EQ(3, List.back().Value);
6263 }
6364
65 TEST(ilistTest, UnsafeClear) {
66 ilist List;
67
68 // Before even allocating a sentinel.
69 List.clearAndLeakNodesUnsafely();
70 EXPECT_EQ(0u, List.size());
71
72 // Empty list with sentinel.
73 ilist::iterator E = List.end();
74 List.clearAndLeakNodesUnsafely();
75 EXPECT_EQ(0u, List.size());
76 // The sentinel shouldn't change.
77 EXPECT_TRUE(E == List.end());
78
79 // List with contents.
80 List.push_back(1);
81 ASSERT_EQ(1u, List.size());
82 Node *N = List.begin();
83 EXPECT_EQ(1, N->Value);
84 List.clearAndLeakNodesUnsafely();
85 EXPECT_EQ(0u, List.size());
86 ASSERT_EQ(1, N->Value);
87 delete N;
88
89 // List is still functional.
90 List.push_back(5);
91 List.push_back(6);
92 ASSERT_EQ(2u, List.size());
93 EXPECT_EQ(5, List.front().Value);
94 EXPECT_EQ(6, List.back().Value);
6495 }
96
97 }