1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
;; See bench.js.
(module
;; A chain of three types. Each type has a "next" field, so we can form linked
;; lists.
(type $A (sub (struct (field $next (ref null $A)))))
(type $B (sub $A (struct (field $next (ref null $A)))))
(type $C (sub $B (struct (field $next (ref null $A)))))
(type $func (func (param (ref $A)) (result i32)))
;; Internal helper to iterate over a linked list and call a function on each
;; item, and return the sum of those calls' results.
(func $iter (param $list (ref null $A)) (param $func (ref $func)) (result i32)
(local $sum i32)
(loop $loop
(if
(ref.is_null
(local.get $list)
)
(then
(return
(local.get $sum)
)
)
(else
(local.set $sum
(i32.add
(local.get $sum)
(call_ref $func
(ref.as_non_null
(local.get $list)
)
(local.get $func)
)
)
)
(local.set $list
(struct.get $A $next
(local.get $list)
)
)
(br $loop)
)
)
)
)
;; Using the helper, and depending on inlining to optimize this, lets us
;; write the exports concisely. First, code to compute the length of the list
;; (for comparison purposes).
(func $len (export "len") (param $list (ref $A)) (result i32)
(call $iter
(local.get $list)
;; Add one each time this is called.
(ref.func $one)
)
)
(func $one (param $list (ref $A)) (result i32)
(i32.const 1)
)
;; At each point in the linked list, check if both the current and next item
;; are inputs are $B, using an if to short-circuit when possible.
(func $iff-both (export "iff-both") (param $list (ref $A)) (result i32)
(call $iter
(local.get $list)
(ref.func $do-iff-both)
)
)
(func $do-iff-both (param $list (ref $A)) (result i32)
(if (result i32)
(ref.test (ref $B)
(struct.get $A $next
(local.get $list)
)
)
(then
(ref.test (ref $B)
(local.get $list)
)
)
(else
(i32.const 0)
)
)
)
;; The same computation, but using an and, so both tests always execute.
(func $and (export "and") (param $list (ref $A)) (result i32)
(call $iter
(local.get $list)
(ref.func $do-and)
)
)
(func $do-and (param $list (ref $A)) (result i32)
(i32.and
(ref.test (ref $B)
(struct.get $A $next
(local.get $list)
)
)
(ref.test (ref $B)
(local.get $list)
)
)
)
;; Similar, but return 1 if either test succeeds (using an if).
(func $iff-either (export "iff-either") (param $list (ref $A)) (result i32)
(call $iter
(local.get $list)
(ref.func $do-iff-either)
)
)
(func $do-iff-either (param $list (ref $A)) (result i32)
(if (result i32)
(ref.test (ref $B)
(struct.get $A $next
(local.get $list)
)
)
(then
(i32.const 1)
)
(else
(ref.test (ref $B)
(local.get $list)
)
)
)
)
;; The same computation, but using an or, so both tests always execute.
(func $or (export "or") (param $list (ref $A)) (result i32)
(call $iter
(local.get $list)
(ref.func $do-or)
)
)
(func $do-or (param $list (ref $A)) (result i32)
(i32.or
(ref.test (ref $B)
(struct.get $A $next
(local.get $list)
)
)
(ref.test (ref $B)
(local.get $list)
)
)
)
;; Use a select to do a test of "is next null ? 0 : test curr".
(func $select (export "select") (param $list (ref $A)) (result i32)
(call $iter
(local.get $list)
(ref.func $do-select)
)
)
(func $do-select (param $list (ref $A)) (result i32)
(select
(i32.const 0)
(ref.test (ref $B)
(local.get $list)
)
(ref.is_null
(struct.get $A $next
(local.get $list)
)
)
)
)
;; Use an iff to do the same.
(func $iff-nextor (export "iff-nextor") (param $list (ref $A)) (result i32)
(call $iter
(local.get $list)
(ref.func $do-iff-nextor)
)
)
(func $do-iff-nextor (param $list (ref $A)) (result i32)
(if (result i32)
(ref.is_null
(struct.get $A $next
(local.get $list)
)
)
(then
(i32.const 0)
)
(else
(ref.test (ref $B)
(local.get $list)
)
)
)
)
;; Use an if over three tests: "test if next is B or C depending on if curr is
;; B."
(func $iff-three (export "iff-three") (param $list (ref $A)) (result i32)
(call $iter
(local.get $list)
(ref.func $do-iff-three)
)
)
(func $do-iff-three (param $list (ref $A)) (result i32)
(local $next (ref null $A))
(local.set $next
(struct.get $A $next
(local.get $list)
)
)
(if (result i32)
(ref.test (ref $B)
(local.get $list)
)
(then
(ref.test (ref $C)
(local.get $next)
)
)
(else
(ref.test (ref $B)
(local.get $next)
)
)
)
)
;; Use a select for the same.
(func $select-three (export "select-three") (param $list (ref $A)) (result i32)
(call $iter
(local.get $list)
(ref.func $do-select-three)
)
)
(func $do-select-three (param $list (ref $A)) (result i32)
(local $next (ref null $A))
(local.set $next
(struct.get $A $next
(local.get $list)
)
)
(select
(ref.test (ref $C)
(local.get $next)
)
(ref.test (ref $B)
(local.get $next)
)
(ref.test (ref $B)
(local.get $list)
)
)
)
;; Creation functions.
(func $makeA (export "makeA") (param $next (ref null $A)) (result anyref)
(struct.new $A
(local.get $next)
)
)
(func $makeB (export "makeB") (param $next (ref null $A)) (result anyref)
(struct.new $B
(local.get $next)
)
)
(func $makeC (export "makeC") (param $next (ref null $A)) (result anyref)
;; This function is not used in benchmarks yet, but it keeps the type $C
;; alive, which prevents $B from looking like it could be final, which might
;; allow the optimizer to simplify more than we want.
(struct.new $C
(local.get $next)
)
)
)
|