From 605e2b7498a7979b59917aa5db17d5022e974c8b Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 25 Feb 2019 10:04:09 -0800 Subject: SmallVector (#1912) Trying to refactor the code to be simpler and less redundant, I ran into some perf issues that it seems like a small vector, with fixed-size storage and optional additional storage as needed, might help with. This implements that class and uses it in a few places. This seems to help, I see some 1-2% fewer instructions and cycles in `perf stat`, but it's hard to tell if it really makes a noticeable difference. --- test/example/small_vector.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 test/example/small_vector.cpp (limited to 'test/example/small_vector.cpp') diff --git a/test/example/small_vector.cpp b/test/example/small_vector.cpp new file mode 100644 index 000000000..732cbfac3 --- /dev/null +++ b/test/example/small_vector.cpp @@ -0,0 +1,68 @@ +#include +#include + +#include "support/small_vector.h" + +using namespace wasm; + +template +void test() { + { + T t; + // build up + assert(t.empty()); + assert(t.size() == 0); + t.push_back(1); + assert(!t.empty()); + assert(t.size() == 1); + t.push_back(2); + assert(!t.empty()); + assert(t.size() == 2); + t.push_back(3); + assert(!t.empty()); + // unwind + assert(t.size() == 3); + assert(t.back() == 3); + t.pop_back(); + assert(t.size() == 2); + assert(t.back() == 2); + t.pop_back(); + assert(t.size() == 1); + assert(t.back() == 1); + t.pop_back(); + assert(t.size() == 0); + assert(t.empty()); + } + { + T t; + // build up + t.push_back(1); + t.push_back(2); + t.push_back(3); + // unwind + t.clear(); + assert(t.size() == 0); + assert(t.empty()); + } + { + T t, u; + assert(t == u); + t.push_back(1); + assert(t != u); + u.push_back(1); + assert(t == u); + u.pop_back(); + assert(t != u); + u.push_back(2); + assert(t != u); + } +} + +int main() { + test>(); + test>(); + test>(); + test>(); + std::cout << "ok.\n"; +} + -- cgit v1.2.3