Open
Description
Test Case
binary-trees-wasi-opt.wasm.zip
Steps to Reproduce
- Run
time wasmtime run -W gc=y binary-trees-wasi-opt.wasm
Actual Results
stretch tree of depth 21 check: 4194303
1048576 trees of depth 4 check: 32505856
262144 trees of depth 6 check: 33292288
65536 trees of depth 8 check: 33488896
16384 trees of depth 10 check: 33538048
4096 trees of depth 12 check: 33550336
1024 trees of depth 14 check: 33553408
256 trees of depth 16 check: 33554176
64 trees of depth 18 check: 33554368
16 trees of depth 20 check: 33554416
long lived tree of depth 20 check: 2097151
real 2m13.350s
user 2m12.736s
sys 0m0.454s
Expected Results
It should run much faster.
V8: 1.2s
Firefox: 4.8s
Safari: 5.9s
Extra Info
The benchmark is:
class TreeNode {
TreeNode left;
TreeNode right;
TreeNode bottomUpTree(int depth) {
if (depth > 0) {
TreeNode leftChild = this.bottomUpTree(depth - 1);
TreeNode rightChild = this.bottomUpTree(depth - 1);
TreeNode node = new TreeNode();
node.left = leftChild;
node.right = rightChild;
return node;
} else {
TreeNode node = new TreeNode();
node.left = null;
node.right = null;
return node;
}
}
int itemCheck() {
if (this.left == null) {
return 1;
} else {
return 1 + this.left.itemCheck() + this.right.itemCheck();
}
}
}
void main() {
int minDepth = 4;
int n = 20; // Increase n to make it more interesting
int maxDepth;
if (minDepth + 2 > n) {
maxDepth = minDepth + 2;
} else {
maxDepth = n;
}
int stretchDepth = maxDepth + 1;
TreeNode stretchTree = new TreeNode();
TreeNode temp = stretchTree.bottomUpTree(stretchDepth);
int check = temp.itemCheck();
print("stretch tree of depth ");
print(stretchDepth);
print(" check: ");
println(check);
TreeNode longLivedTree = new TreeNode();
longLivedTree = longLivedTree.bottomUpTree(maxDepth);
int depth = minDepth;
while (depth <= maxDepth) {
int iterations = 1 << (maxDepth - depth + minDepth);
check = 0;
int i = 1;
while (i <= iterations) {
TreeNode tree = new TreeNode();
tree = tree.bottomUpTree(depth);
check = check + tree.itemCheck();
i = i + 1;
}
print(iterations);
print(" trees of depth ");
print(depth);
print(" check: ");
println(check);
depth = depth + 2;
}
print("long lived tree of depth ");
print(maxDepth);
print(" check: ");
println(longLivedTree.itemCheck());
}
compiled with https://github.com/jrmuizel/wasm-lang1
There's a browser version here: https://zingy-sorbet-48e9a5.netlify.app/binary-trees-opt.html