Skip to content

Instantly share code, notes, and snippets.

@RT2Code
Last active March 29, 2022 14:03
Show Gist options
  • Save RT2Code/b98d33514eca8fe3a4af82adec220f73 to your computer and use it in GitHub Desktop.
Save RT2Code/b98d33514eca8fe3a4af82adec220f73 to your computer and use it in GitHub Desktop.
#include "mimalloc-new-delete.h"
#include "mimalloc.h"
#define CATCH_CONFIG_RUNNER
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include "catch.hpp"
#include <cstdint>
#include <vector>
constexpr int components_count{10000};
using Components_t = int32_t;
template<int Id>
struct C final
{
static constexpr int get_id() noexcept { return Id; }
};
template<typename ...Components>
class Archetype final
{
public:
Archetype(int n) :
m_components_count{n}
{
([&]()
{
const auto& component_id = Components::get_id();
if (component_id >= std::ssize(m_component_arrays))
m_component_arrays.resize(component_id + 1);
m_component_arrays[component_id].resize(m_components_count);
}(), ...);
}
template<typename Function>
void for_each(Function function)
{
for_each_entity(std::move(function), std::begin(m_component_arrays[Components::get_id()])...);
}
private:
template<typename Function, typename ...Iterators>
void for_each_entity(Function function, Iterators&& ...iterators)
{
for (int i{}; i < m_components_count; ++i)
{
function(*iterators...);
(++iterators, ...);
}
}
int m_components_count{};
std::vector<std::vector<Components_t>> m_component_arrays;
};
TEST_CASE("mimalloc", "[mimalloc]")
{
BENCHMARK_ADVANCED("1 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0)
{
c0 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("2 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1)
{
c0 += 1;
c1 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("3 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2)
{
c0 += 1;
c1 += 1;
c2 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("4 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>, C<3>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2, auto& c3)
{
c0 += 1;
c1 += 1;
c2 += 1;
c3 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("5 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>, C<3>, C<4>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2, auto& c3, auto& c4)
{
c0 += 1;
c1 += 1;
c2 += 1;
c3 += 1;
c4 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("6 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>, C<3>, C<4>, C<5>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2, auto& c3, auto& c4, auto& c5)
{
c0 += 1;
c1 += 1;
c2 += 1;
c3 += 1;
c4 += 1;
c5 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("7 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>, C<3>, C<4>, C<5>, C<6>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2, auto& c3, auto& c4, auto& c5, auto& c6)
{
c0 += 1;
c1 += 1;
c2 += 1;
c3 += 1;
c4 += 1;
c5 += 1;
c6 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("8 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>, C<3>, C<4>, C<5>, C<6>, C<7>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2, auto& c3, auto& c4, auto& c5, auto& c6, auto& c7)
{
c0 += 1;
c1 += 1;
c2 += 1;
c3 += 1;
c4 += 1;
c5 += 1;
c6 += 1;
c7 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("9 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>, C<3>, C<4>, C<5>, C<6>, C<7>, C<8>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2, auto& c3, auto& c4, auto& c5, auto& c6, auto& c7, auto& c8)
{
c0 += 1;
c1 += 1;
c2 += 1;
c3 += 1;
c4 += 1;
c5 += 1;
c6 += 1;
c7 += 1;
c8 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("10 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>, C<3>, C<4>, C<5>, C<6>, C<7>, C<8>, C<9>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2, auto& c3, auto& c4, auto& c5, auto& c6, auto& c7, auto& c8, auto& c9)
{
c0 += 1;
c1 += 1;
c2 += 1;
c3 += 1;
c4 += 1;
c5 += 1;
c6 += 1;
c7 += 1;
c8 += 1;
c9 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("11 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>, C<3>, C<4>, C<5>, C<6>, C<7>, C<8>, C<9>, C<10>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2, auto& c3, auto& c4, auto& c5, auto& c6, auto& c7, auto& c8, auto& c9, auto& c10)
{
c0 += 1;
c1 += 1;
c2 += 1;
c3 += 1;
c4 += 1;
c5 += 1;
c6 += 1;
c7 += 1;
c8 += 1;
c9 += 1;
c10 += 1;
});
});
return archetype;
};
BENCHMARK_ADVANCED("12 component array iteration")(Catch::Benchmark::Chronometer meter)
{
Archetype<C<0>, C<1>, C<2>, C<3>, C<4>, C<5>, C<6>, C<7>, C<8>, C<9>, C<10>, C<11>> archetype{components_count};
meter.measure([&]()
{
archetype.for_each([](auto& c0, auto& c1, auto& c2, auto& c3, auto& c4, auto& c5, auto& c6, auto& c7, auto& c8, auto& c9, auto& c10, auto& c11)
{
c0 += 1;
c1 += 1;
c2 += 1;
c3 += 1;
c4 += 1;
c5 += 1;
c6 += 1;
c7 += 1;
c8 += 1;
c9 += 1;
c10 += 1;
c11 += 1;
});
});
return archetype;
};
}
int main(int argc, char** argv)
{
mi_option_enable(mi_option_show_errors);
mi_option_enable(mi_option_show_stats);
mi_option_enable(mi_option_verbose);
return Catch::Session().run(argc, argv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment