Using a pool to improve performance

Codon uses garbage collection, so I feel like this problem should permit using a memory pool, as would be common in this kind of problem. There are other ways to make the original code faster without a pool, but I think this makes the most sense.
pull/116/head
Stephen Berry 2022-12-16 08:12:06 -06:00
parent 320dc0aeed
commit b9bfe283f2
1 changed files with 22 additions and 22 deletions

View File

@ -5,33 +5,32 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
namespace {
struct Node { struct Node {
std::unique_ptr<Node> left; Node* left{};
std::unique_ptr<Node> right; Node* right{};
}; };
std::unique_ptr<Node> make_tree(int d) { inline Node* make_tree(int d, std::vector<Node>& pool) {
auto node = std::make_unique<Node>();
if (d > 0) { if (d > 0) {
node->left = make_tree(d - 1); return &pool.emplace_back(Node{make_tree(d - 1, pool), make_tree(d - 1, pool)});
node->right = make_tree(d - 1); }
else {
return &pool.emplace_back(Node{});
} }
return node;
} }
int check_tree(Node *node) { inline int check_tree(const Node* node) {
auto *l = node->left.get(), *r = node->right.get(); if (!node->left)
if (!l)
return 1; return 1;
else else
return 1 + check_tree(l) + check_tree(r); return 1 + check_tree(node->left) + check_tree(node->right);
} }
int make_check(const std::pair<int, int> &itde) { inline int make_check(const std::pair<int, int> &itde, std::vector<Node>& pool) {
int i = itde.first, d = itde.second; int i = itde.first, d = itde.second;
auto tree = make_tree(d); pool.clear();
return check_tree(tree.get()); auto tree = make_tree(d, pool);
return check_tree(tree);
} }
struct ArgChunks { struct ArgChunks {
@ -53,7 +52,6 @@ struct ArgChunks {
return !chunk.empty(); return !chunk.empty();
} }
}; };
} // namespace
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
using clock = std::chrono::high_resolution_clock; using clock = std::chrono::high_resolution_clock;
@ -65,11 +63,13 @@ int main(int argc, char *argv[]) {
int n = std::stoi(argv[1]); int n = std::stoi(argv[1]);
int max_depth = std::max(min_depth + 2, n); int max_depth = std::max(min_depth + 2, n);
int stretch_depth = max_depth + 1; int stretch_depth = max_depth + 1;
std::vector<Node> pool{};
std::cout << "stretch tree of depth " << stretch_depth std::cout << "stretch tree of depth " << stretch_depth
<< "\t check: " << make_check({0, stretch_depth}) << std::endl; << "\t check: " << make_check({0, stretch_depth}, pool) << '\n';
auto long_lived_tree = make_tree(max_depth); auto long_lived_tree = make_tree(max_depth, pool);
int mmd = max_depth + min_depth; int mmd = max_depth + min_depth;
for (int d = min_depth; d < stretch_depth; d += 2) { for (int d = min_depth; d < stretch_depth; d += 2) {
int i = (1 << (mmd - d)); int i = (1 << (mmd - d));
@ -77,13 +77,13 @@ int main(int argc, char *argv[]) {
ArgChunks iter(i, d); ArgChunks iter(i, d);
while (iter.next()) { while (iter.next()) {
for (auto &argchunk : iter.chunk) { for (auto &argchunk : iter.chunk) {
cs += make_check(argchunk); cs += make_check(argchunk, pool);
} }
} }
std::cout << i << "\t trees of depth " << d << "\t check: " << cs << std::endl; std::cout << i << "\t trees of depth " << d << "\t check: " << cs << '\n';
} }
std::cout << "long lived tree of depth " << max_depth std::cout << "long lived tree of depth " << max_depth
<< "\t check: " << check_tree(long_lived_tree.get()) << std::endl; << "\t check: " << check_tree(long_lived_tree) << '\n';
std::cout << (duration_cast<milliseconds>(clock::now() - t).count() / 1e3) std::cout << (duration_cast<milliseconds>(clock::now() - t).count() / 1e3)
<< std::endl; << std::endl;
} }