Debian Codegraph @

tests/benchmarks/bench_tree.cpp

raw symbols SCIP
1 /*
2 SPDX-FileCopyrightText: 2020 Milian Wolff <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #include <algorithm>
8 #include <array>
9 #include <cstdint>
10 #include <iostream>
11 #include <list>
12 #include <random>
13 #include <vector>
14
15 #include <QVector>
16
17 #include <boost/container/pmr/monotonic_buffer_resource.hpp>
18 #include <boost/container/pmr/polymorphic_allocator.hpp>
19 #include <boost/container/pmr/slist.hpp>
20 #include <boost/container/slist.hpp>
21
22 #include "../../src/analyze/allocationdata.h"
23
24 constexpr uint64_t MAX_TREE_DEPTH = 64;
25 constexpr uint64_t NO_BRANCH_DEPTH = 4;
26 constexpr uint64_t BRANCH_WIDTH = 8;
27 constexpr uint64_t NUM_TRACES = 1000000;
28
29 using Trace = std::array<uint64_t, MAX_TREE_DEPTH>;
30
31 uint64_t generateIp(uint64_t level)
32 {
33 if (level % NO_BRANCH_DEPTH) {
34 return level;
35 }
36 static std::mt19937_64 engine(0);
37 static std::uniform_int_distribution<uint64_t> dist(0, BRANCH_WIDTH - 1);
38 return dist(engine);
39 }
40
41 Trace generateTrace()
42 {
43 Trace trace;
44 for (uint64_t i = 0; i < MAX_TREE_DEPTH; ++i) {
45 trace[i] = generateIp(i);
46 }
47 return trace;
48 }
49
50 std::vector<Trace> generateTraces()
51 {
52 std::vector<Trace> traces(NUM_TRACES);
53 std::generate(traces.begin(), traces.end(), generateTrace);
54 return traces;
55 }
56
57 namespace Tree {
58 template <template <typename...> class Container>
59 struct Node
60 {
61 AllocationData cost;
62 uint64_t ip = 0;
63 const Node* parent = nullptr;
64 Container<Node> children;
65 };
66
67 template <template <typename...> class Container>
68 void setParentsImpl(Container<Node<Container>>& nodes, const Node<Container>* parent)
69 {
70 for (auto& node : nodes) {
71 node.parent = parent;
72 setParentsImpl(node.children, &node);
73 }
74 }
75
76 void setParents(QVector<Node<QVector>>& nodes, const Node<QVector>* parent)
77 {
78 setParentsImpl(nodes, parent);
79 }
80
81 void setParents(std::vector<Node<std::vector>>& nodes, const Node<std::vector>* parent)
82 {
83 setParentsImpl(nodes, parent);
84 }
85
86 void setParents(std::list<Node<std::list>>&, const Node<std::list>*)
87 {
88 // nothing to do
89 }
90
91 void setParents(boost::container::slist<Node<boost::container::slist>>&, const Node<boost::container::slist>*)
92 {
93 // nothing to do
94 }
95
96 void setParents(boost::container::pmr::slist<Node<boost::container::pmr::slist>>&,
97 const Node<boost::container::pmr::slist>*)
98 {
99 // nothing to do
100 }
101
102 template <template <typename...> class Container, typename... Allocator>
103 Container<Node<Container>> buildTree(const std::vector<Trace>& traces, const Allocator&... allocator)
104 {
105 auto findNode = [&](Container<Node<Container>>* nodes, uint64_t ip, const Node<Container>* parent) {
106 auto it =
107 std::find_if(nodes->begin(), nodes->end(), [ip](const Node<Container>& node) { return node.ip == ip; });
108 if (it != nodes->end())
109 return it;
110 return nodes->insert(it, Node<Container> {{}, ip, parent, Container<Node<Container>> {allocator...}});
111 };
112
113 Container<Node<Container>> ret(allocator...);
114 const Node<Container>* parent = nullptr;
115 for (const auto& trace : traces) {
116 auto* nodes = &ret;
117 for (const auto& ip : trace) {
118 auto it = findNode(nodes, ip, parent);
119 it->cost.allocations++;
120 nodes = &it->children;
121 parent = &(*it);
122 }
123 }
124
125 setParents(ret, nullptr);
126
127 return ret;
128 }
129
130 template <template <typename...> class Container>
131 uint64_t numNodes(const Node<Container>& node)
132 {
133 return std::accumulate(node.children.begin(), node.children.end(), uint64_t(1),
134 [](uint64_t count, const Node<Container>& node) { return count + numNodes(node); });
135 }
136
137 template <template <typename...> class Container>
138 uint64_t numNodes(const Container<Node<Container>>& tree)
139 {
140 return std::accumulate(tree.begin(), tree.end(), uint64_t(0),
141 [](uint64_t count, const Node<Container>& node) { return count + numNodes(node); });
142 }
143
144 template <template <typename...> class Container>
145 std::pair<uint64_t, uint64_t> run(const std::vector<Trace>& traces)
146 {
147 const auto tree = buildTree<Container>(traces);
148 return {tree.size(), numNodes(tree)};
149 }
150
151 template <>
152 std::pair<uint64_t, uint64_t> run<boost::container::pmr::slist>(const std::vector<Trace>& traces)
153 {
154 boost::container::pmr::monotonic_buffer_resource mbr;
155 const auto tree = buildTree<boost::container::pmr::slist>(traces, &mbr);
156 return {tree.size(), numNodes<boost::container::pmr::slist>(tree)};
157 }
158 }
159
160 enum class Tag
161 {
162 QVector,
163 StdVector,
164 StdList,
165 BoostSlist,
166 BoostPmrSlist,
167 };
168
169 std::pair<uint64_t, uint64_t> run(const std::vector<Trace>& traces, Tag tag)
170 {
171 switch (tag) {
172 case Tag::QVector:
173 return Tree::run<QVector>(traces);
174 case Tag::StdVector:
175 return Tree::run<std::vector>(traces);
176 case Tag::StdList:
177 return Tree::run<std::list>(traces);
178 case Tag::BoostSlist:
179 return Tree::run<boost::container::slist>(traces);
180 case Tag::BoostPmrSlist:
181 return Tree::run<boost::container::pmr::slist>(traces);
182 }
183 Q_UNREACHABLE();
184 }
185
186 int main(int argc, char** argv)
187 {
188 if (argc != 2) {
189 std::cerr << "usage: bench_tree [QVector|std::vector|std::list|boost::slist|boost::pmr::slist]\n";
190 return 1;
191 }
192
193 const auto tag = [&]() {
194 auto t = std::string(argv[1]);
195 if (t == "QVector")
196 return Tag::QVector;
197 if (t == "std::vector")
198 return Tag::StdVector;
199 if (t == "std::list")
200 return Tag::StdList;
201 if (t == "boost::slist")
202 return Tag::BoostSlist;
203 if (t == "boost::pmr::slist")
204 return Tag::BoostPmrSlist;
205 std::cerr << "unhandled tag: " << t << "\n";
206 exit(1);
207 }();
208
209 const auto traces = generateTraces();
210 const auto result = run(traces, tag);
211 std::cout << result.first << ", " << result.second << std::endl;
212 return 0;
213 }