summaryrefslogtreecommitdiff
path: root/test/example/small_vector.cpp
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2019-02-25 10:04:09 -0800
committerGitHub <noreply@github.com>2019-02-25 10:04:09 -0800
commit605e2b7498a7979b59917aa5db17d5022e974c8b (patch)
tree9cf62a6a4191a8125d86b9d17f272269ea2b8834 /test/example/small_vector.cpp
parentf11b7e712fab6f11ce9f51b85459ab199e817cae (diff)
downloadbinaryen-605e2b7498a7979b59917aa5db17d5022e974c8b.tar.gz
binaryen-605e2b7498a7979b59917aa5db17d5022e974c8b.tar.bz2
binaryen-605e2b7498a7979b59917aa5db17d5022e974c8b.zip
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.
Diffstat (limited to 'test/example/small_vector.cpp')
-rw-r--r--test/example/small_vector.cpp68
1 files changed, 68 insertions, 0 deletions
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 <iostream>
+#include <cassert>
+
+#include "support/small_vector.h"
+
+using namespace wasm;
+
+template<typename T>
+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<SmallVector<int, 0>>();
+ test<SmallVector<int, 1>>();
+ test<SmallVector<int, 2>>();
+ test<SmallVector<int, 10>>();
+ std::cout << "ok.\n";
+}
+