Skip to content

Commit

Permalink
revise array iteration optimization; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crusso committed Jan 9, 2024
1 parent f9d7b7d commit b8a69d6
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/codegen/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ let page_size = Int32.of_int (64*1024)
let page_size64 = Int64.of_int32 page_size
let page_size_bits = 16


(* tentative tagging scheme *)
(* Tentative tagging scheme *)

module TaggingScheme = struct
type bit = I | O
Expand Down Expand Up @@ -3240,7 +3239,7 @@ module MakeCompact (Num : BigNumType) : BigNumType = struct
G.if1 I32Type
(get_res ^^ compile_bitor_const (TaggingScheme.tag_of_typ Type.Int))
(get_n ^^ compile_shrS_const (Int32.of_int (32 - BitTagged.ubits_of Type.Int)) ^^
Num.from_word30 env ^^ get_amount ^^ Num.compile_lsh env)
Num.from_word30 env ^^ get_amount ^^ Num.compile_lsh env) (* TBR: from_word30? *)
)
(get_n ^^ get_amount ^^ Num.compile_lsh env))

Expand Down Expand Up @@ -10383,7 +10382,7 @@ and compile_prim_invocation (env : E.t) ae p es at =
| NextArrayOffset spacing, [e] ->
let advance_by =
match spacing with
| ElementSize -> Int32.shift_left Arr.element_size (32 - BitTagged.ubits_of Type.Int) (* 4 : Nat *)
| ElementSize
| One -> Int32.shift_left 1l (32 - BitTagged.ubits_of Type.Int) (* 1 : Nat *) in
SR.Vanilla,
compile_exp_vanilla env ae e ^^ (* previous byte offset to array *)
Expand All @@ -10399,6 +10398,7 @@ and compile_prim_invocation (env : E.t) ae p es at =
Tagged.load_forwarding_pointer env ^^
compile_exp_vanilla env ae e2 ^^ (* byte offset *)
BitTagged.untag_i32 env Type.Int ^^
compile_shl_const 2l ^^ (* effectively a multiplication by word_size *)
(* Note: the below two lines compile to `i32.add; i32.load offset=OFFSET`
with OFFSET = 13 with forwarding pointers and OFFSET = 9 without forwarding pointers,
thus together also unskewing the pointer and skipping administrative
Expand All @@ -10409,10 +10409,8 @@ and compile_prim_invocation (env : E.t) ae p es at =
| GetPastArrayOffset spacing, [e] ->
let shift =
match spacing with
| ElementSize ->
compile_shl_const 2l ^^ (* effectively a multiplication by word_size *)
BitTagged.tag_i32 env Type.Int
| One -> BigNum.from_word30 env in (* make it a compact bignum *)
| ElementSize
| One -> BigNum.from_word30 env in (* make it a compact bignum *) (* TBR: from_word30? *)
SR.Vanilla,
compile_exp_vanilla env ae e ^^ (* array *)
Arr.len env ^^
Expand Down
23 changes: 23 additions & 0 deletions test/run/array-iter-max.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Prim "mo:⛔";
// test we can iterate over vals and keys of largest array
let max_size = 2**29; // maximum array size
let a = Prim.Array_tabulate<Nat>(max_size,func i = i+1);
var c = 0;
for (i in a.vals()) {
assert i == c+1; c += 1;
}
;
assert c == max_size;
Prim.debugPrint(debug_show c);
var d = 0;
for (k in a.keys()) {
assert k == d; d += 1;
};
assert d == max_size;
Prim.debugPrint(debug_show d);

Prim.Array_tabulate<Nat>(max_size+1,func i = i+1); // should trap

//SKIP run
//SKIP run-ir
//SKIP run-low
23 changes: 23 additions & 0 deletions test/run/mut-array-iter-max.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Prim "mo:⛔";
// test we can iterate over vals and keys of largest mutable array
let max_size = 2**29; // maximum array size
let a = Prim.Array_init<Nat>(max_size, 666);
var c = 0;
for (v in a.vals()) {
assert v == 666; c += 1;
}
;
assert c == max_size;
Prim.debugPrint(debug_show c);
var d = 0;
for (k in a.keys()) {
assert k == d; d += 1;
};
assert d == max_size;
Prim.debugPrint(debug_show d);

Prim.Array_init<Nat>(max_size+1,0); // should trap

//SKIP run
//SKIP run-ir
//SKIP run-low
17 changes: 17 additions & 0 deletions test/run/ok/array-iter-max.wasm-run.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
536_870_912
536_870_912
RTS error: Array allocation too large
Error: failed to run main module `_out/array-iter-max.wasm`

Caused by:
0: failed to invoke command default
1: error while executing at wasm backtrace:
0: rts_trap
1: motoko_rts::trap_with_prefix::hb9fcc00b15d04524
2: motoko_rts::rts_trap_with::h22ec7878647e017a
3: motoko_rts::memory::alloc_array::h29955cba75599907
4: alloc_array
5: Array_tabulate
6: init
7: _start
2: wasm trap: unreachable
1 change: 1 addition & 0 deletions test/run/ok/array-iter-max.wasm-run.ret.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return code 134
17 changes: 17 additions & 0 deletions test/run/ok/mut-array-iter-max.wasm-run.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
536_870_912
536_870_912
RTS error: Array allocation too large
Error: failed to run main module `_out/mut-array-iter-max.wasm`

Caused by:
0: failed to invoke command default
1: error while executing at wasm backtrace:
0: rts_trap
1: motoko_rts::trap_with_prefix::hb9fcc00b15d04524
2: motoko_rts::rts_trap_with::h22ec7878647e017a
3: motoko_rts::memory::alloc_array::h29955cba75599907
4: alloc_array
5: Array_init
6: init
7: _start
2: wasm trap: unreachable
1 change: 1 addition & 0 deletions test/run/ok/mut-array-iter-max.wasm-run.ret.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return code 134

0 comments on commit b8a69d6

Please sign in to comment.