Skip to content

Commit 686b48d

Browse files
MaxGraeydcodeIO
authored andcommitted
updare n-body bench (AssemblyScript#1035)
1 parent f9bb33f commit 686b48d

File tree

6 files changed

+122
-125
lines changed

6 files changed

+122
-125
lines changed

examples/n-body/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ Benchmark
3030
=========
3131

3232
***Environment:***
33-
- MacBook Pro (Retina, 15-inch, Late 2013)
33+
- MacBook Pro (15-inch, 2019)
3434
- macOS 10.14.6
35-
- node.js v12.10.0
36-
- rustc 1.40.0-nightly (37538aa13 2019-09-25)
35+
- node.js v13.5.0
36+
- rustc 1.42.0-nightly (3a3f4a7cb 2019-12-28)
3737

3838
***Results:***
3939

4040
| Target | Time, ***ms*** | Size, ***KB*** |
4141
|-------------------------|-----------------|----------------|
42-
| **AssemblyScript WASM** | **2821** | **1.6** |
43-
| AssemblyScript ASMJS | 3972 | 19* |
44-
| JavaScript | 2656 | 5* |
45-
| Rust WASM | 2560 | 2 |
42+
| **AssemblyScript WASM** | **1650** | **1.6** |
43+
| AssemblyScript ASMJS | 2558 | 10* |
44+
| JavaScript | 1596 | 5* |
45+
| Rust WASM | 1570 | 2 |
4646

4747
___* unminified___

examples/n-body/build/index.asm.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
function asmFunc(global, env, buffer) {
3-
"almost asm";
43
var HEAP8 = new global.Int8Array(buffer);
54
var HEAP16 = new global.Int16Array(buffer);
65
var HEAP32 = new global.Int32Array(buffer);
@@ -24,27 +23,37 @@ function asmFunc(global, env, buffer) {
2423
var assembly_index_system = 0;
2524
var $lib_rt_stub_startOffset = 0;
2625
var $lib_rt_stub_offset = 0;
27-
function $lib_rt_stub___alloc($0, $1) {
28-
var $2 = 0, $3 = 0, $4 = 0, $5 = 0;
29-
if ($0 >>> 0 > 1073741808 >>> 0) {
30-
abort()
31-
}
32-
$3 = $lib_rt_stub_offset + 16 | 0;
33-
$2 = (($3 + ($0 >>> 0 > 1 >>> 0 ? $0 : 1) | 0) + 15 | 0) & -16 | 0;
34-
$4 = __wasm_memory_size();
35-
if ($2 >>> 0 > ($4 << 16 | 0) >>> 0) {
36-
$5 = ((($2 - $3 | 0) + 65535 | 0) & -65536 | 0) >>> 16 | 0;
37-
if ((__wasm_memory_grow((($4 | 0) > ($5 | 0) ? $4 : $5) | 0) | 0) < (0 | 0)) {
38-
if ((__wasm_memory_grow($5 | 0) | 0) < (0 | 0)) {
26+
function $lib_rt_stub_maybeGrowMemory($0) {
27+
var $1 = 0, $2 = 0;
28+
$2 = __wasm_memory_size();
29+
$1 = $2 << 16 | 0;
30+
if ($0 >>> 0 > $1 >>> 0) {
31+
$1 = ((($0 - $1 | 0) + 65535 | 0) & -65536 | 0) >>> 16 | 0;
32+
if ((__wasm_memory_grow((($2 | 0) > ($1 | 0) ? $2 : $1) | 0) | 0) < (0 | 0)) {
33+
if ((__wasm_memory_grow($1 | 0) | 0) < (0 | 0)) {
3934
abort()
4035
}
4136
}
4237
}
43-
$lib_rt_stub_offset = $2;
44-
$2 = $3 - 16 | 0;
38+
$lib_rt_stub_offset = $0;
39+
}
40+
41+
function $lib_rt_stub___alloc($0, $1) {
42+
var $2 = 0, $3 = 0, $4 = 0;
43+
if ($0 >>> 0 > 1073741808 >>> 0) {
44+
abort()
45+
}
46+
$2 = ($0 + 15 | 0) & -16 | 0;
47+
$3 = $2 >>> 0 > 16 >>> 0;
48+
$4 = $lib_rt_stub_offset + 16 | 0;
49+
$3 = $3 ? $2 : 16;
50+
$lib_rt_stub_maybeGrowMemory($4 + $3 | 0);
51+
$2 = $4 - 16 | 0;
52+
HEAP32[$2 >> 2] = $3;
53+
HEAP32[($2 + 4 | 0) >> 2] = 1;
4554
HEAP32[($2 + 8 | 0) >> 2] = $1;
4655
HEAP32[($2 + 12 | 0) >> 2] = $0;
47-
return $3;
56+
return $4;
4857
}
4958

5059
function assembly_index_NBodySystem_constructor($0) {
@@ -209,7 +218,7 @@ function asmFunc(global, env, buffer) {
209218
$0 = $0 | 0;
210219
var $1 = 0;
211220
loop_0 : while (1) {
212-
if (!($1 >>> 0 >= $0 >>> 0)) {
221+
if ($1 >>> 0 < $0 >>> 0) {
213222
assembly_index_NBodySystem_advance(assembly_index_system);
214223
$1 = $1 + 1 | 0;
215224
continue loop_0;
@@ -232,10 +241,14 @@ function asmFunc(global, env, buffer) {
232241

233242
function start() {
234243
$lib_rt_stub_startOffset = 16;
235-
$lib_rt_stub_offset = $lib_rt_stub_startOffset;
244+
$lib_rt_stub_offset = 16;
236245
}
237246

238247
var FUNCTION_TABLE = [];
248+
function __wasm_memory_size() {
249+
return buffer.byteLength / 65536 | 0;
250+
}
251+
239252
function __wasm_memory_grow(pagesToAdd) {
240253
pagesToAdd = pagesToAdd | 0;
241254
var oldPages = __wasm_memory_size() | 0;
@@ -258,10 +271,6 @@ function asmFunc(global, env, buffer) {
258271
return oldPages;
259272
}
260273

261-
function __wasm_memory_size() {
262-
return buffer.byteLength / 65536 | 0;
263-
}
264-
265274
return {
266275
"memory": Object.create(Object.prototype, {
267276
"grow": {

examples/n-body/build/optimized.wasm

-6 Bytes
Binary file not shown.

examples/n-body/build/optimized.wat

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(module
2-
(type $FUNCSIG$v (func))
3-
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
4-
(type $FUNCSIG$ii (func (param i32) (result i32)))
5-
(type $FUNCSIG$vi (func (param i32)))
6-
(type $FUNCSIG$d (func (result f64)))
7-
(type $FUNCSIG$di (func (param i32) (result f64)))
8-
(type $FUNCSIG$iddddddd (func (param f64 f64 f64 f64 f64 f64 f64) (result i32)))
9-
(type $FUNCSIG$i (func (result i32)))
2+
(type $i32_=>_none (func (param i32)))
3+
(type $none_=>_none (func))
4+
(type $i32_=>_i32 (func (param i32) (result i32)))
5+
(type $none_=>_i32 (func (result i32)))
6+
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
7+
(type $f64_f64_f64_f64_f64_f64_f64_=>_i32 (func (param f64 f64 f64 f64 f64 f64 f64) (result i32)))
8+
(type $none_=>_f64 (func (result f64)))
9+
(type $i32_=>_f64 (func (param i32) (result f64)))
1010
(import "env" "memory" (memory $0 0))
1111
(global $assembly/index/system (mut i32) (i32.const 0))
1212
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
@@ -17,7 +17,7 @@
1717
(export "bench" (func $assembly/index/bench))
1818
(export "getBody" (func $assembly/index/getBody))
1919
(start $start)
20-
(func $~lib/rt/stub/maybeGrowMemory (; 0 ;) (type $FUNCSIG$vi) (param $0 i32)
20+
(func $~lib/rt/stub/maybeGrowMemory (; 0 ;) (param $0 i32)
2121
(local $1 i32)
2222
(local $2 i32)
2323
local.get $0
@@ -59,7 +59,7 @@
5959
local.get $0
6060
global.set $~lib/rt/stub/offset
6161
)
62-
(func $~lib/rt/stub/__alloc (; 1 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
62+
(func $~lib/rt/stub/__alloc (; 1 ;) (param $0 i32) (param $1 i32) (result i32)
6363
(local $2 i32)
6464
(local $3 i32)
6565
(local $4 i32)
@@ -94,7 +94,7 @@
9494
local.get $4
9595
i32.store
9696
local.get $2
97-
i32.const -1
97+
i32.const 1
9898
i32.store offset=4
9999
local.get $2
100100
local.get $1
@@ -104,7 +104,7 @@
104104
i32.store offset=12
105105
local.get $3
106106
)
107-
(func $assembly/index/NBodySystem#constructor (; 2 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
107+
(func $assembly/index/NBodySystem#constructor (; 2 ;) (param $0 i32) (result i32)
108108
(local $1 i32)
109109
(local $2 i32)
110110
(local $3 f64)
@@ -187,7 +187,7 @@
187187
i32.store
188188
local.get $1
189189
)
190-
(func $assembly/index/Body#constructor (; 3 ;) (type $FUNCSIG$iddddddd) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 f64) (param $4 f64) (param $5 f64) (param $6 f64) (result i32)
190+
(func $assembly/index/Body#constructor (; 3 ;) (param $0 f64) (param $1 f64) (param $2 f64) (param $3 f64) (param $4 f64) (param $5 f64) (param $6 f64) (result i32)
191191
(local $7 i32)
192192
i32.const 56
193193
i32.const 4
@@ -215,7 +215,7 @@
215215
f64.store offset=48
216216
local.get $7
217217
)
218-
(func $~lib/rt/__allocArray (; 4 ;) (type $FUNCSIG$i) (result i32)
218+
(func $~lib/rt/__allocArray (; 4 ;) (result i32)
219219
(local $0 i32)
220220
(local $1 i32)
221221
i32.const 16
@@ -238,7 +238,7 @@
238238
i32.store offset=12
239239
local.get $0
240240
)
241-
(func $assembly/index/init (; 5 ;) (type $FUNCSIG$v)
241+
(func $assembly/index/init (; 5 ;)
242242
(local $0 i32)
243243
(local $1 i32)
244244
call $~lib/rt/__allocArray
@@ -298,7 +298,7 @@
298298
call $assembly/index/NBodySystem#constructor
299299
global.set $assembly/index/system
300300
)
301-
(func $assembly/index/NBodySystem#advance (; 6 ;) (type $FUNCSIG$vi) (param $0 i32)
301+
(func $assembly/index/NBodySystem#advance (; 6 ;) (param $0 i32)
302302
(local $1 i32)
303303
(local $2 f64)
304304
(local $3 i32)
@@ -498,7 +498,7 @@
498498
end
499499
end
500500
)
501-
(func $assembly/index/NBodySystem#energy (; 7 ;) (type $FUNCSIG$di) (param $0 i32) (result f64)
501+
(func $assembly/index/NBodySystem#energy (; 7 ;) (param $0 i32) (result f64)
502502
(local $1 f64)
503503
(local $2 i32)
504504
(local $3 i32)
@@ -626,16 +626,16 @@
626626
end
627627
local.get $1
628628
)
629-
(func $assembly/index/step (; 8 ;) (type $FUNCSIG$d) (result f64)
629+
(func $assembly/index/step (; 8 ;) (result f64)
630630
global.get $assembly/index/system
631631
call $assembly/index/NBodySystem#advance
632632
global.get $assembly/index/system
633633
call $assembly/index/NBodySystem#energy
634634
)
635-
(func $assembly/index/bench (; 9 ;) (type $FUNCSIG$vi) (param $0 i32)
635+
(func $assembly/index/bench (; 9 ;) (param $0 i32)
636636
(local $1 i32)
637-
block $break|0
638-
loop $loop|0
637+
loop $loop|0
638+
block $break|0
639639
local.get $1
640640
local.get $0
641641
i32.ge_u
@@ -648,10 +648,9 @@
648648
local.set $1
649649
br $loop|0
650650
end
651-
unreachable
652651
end
653652
)
654-
(func $assembly/index/getBody (; 10 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
653+
(func $assembly/index/getBody (; 10 ;) (param $0 i32) (result i32)
655654
(local $1 i32)
656655
local.get $0
657656
global.get $assembly/index/system
@@ -671,13 +670,10 @@
671670
i32.const 0
672671
end
673672
)
674-
(func $start (; 11 ;) (type $FUNCSIG$v)
673+
(func $start (; 11 ;)
675674
i32.const 16
676675
global.set $~lib/rt/stub/startOffset
677676
i32.const 16
678677
global.set $~lib/rt/stub/offset
679678
)
680-
(func $null (; 12 ;) (type $FUNCSIG$v)
681-
nop
682-
)
683679
)

0 commit comments

Comments
 (0)