Skip to content

Commit

Permalink
Performance improvements (#56)
Browse files Browse the repository at this point in the history
* Replace [] with `() in binaryen macros

* Performance improvements

* Remove change to functional notation
  • Loading branch information
drew-y authored Sep 26, 2024
1 parent d255b0f commit 0dcc28d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/syntax-objects/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export class List extends Syntax {
super(opts);
const value = opts.value;

if (!value || value instanceof Array) {
this.push(...(value ?? []));
if (value instanceof Array) {
value.forEach((v) => this.push(v));
} else if (value instanceof List) {
this.push(...value.toArray());
value.toArray().forEach((v) => this.push(v));
}
}

Expand Down
12 changes: 6 additions & 6 deletions std/fixed_array.voyd
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use std::macros::all
pub fn new_fixed_array<T>(size: i32) -> FixedArray<T>
binaryen_gc_call(
arrayNew,
[bin_type_to_heap_type(FixedArray<T>), size],
`(bin_type_to_heap_type(FixedArray<T>), size),
FixedArray<T>
)

pub fn get<T>(arr: FixedArray<T>, index: i32) -> T
binaryen_gc_call(
arrayGet,
[arr, index, BnrType<T>, BnrConst(false)],
`(arr, index, BnrType<T>, BnrConst(false)),
T
)

pub fn set<T>(arr: FixedArray<T>, index: i32, value: T) -> FixedArray<T>
binaryen_gc_call(arraySet, [arr, index, value], FixedArray<T>)
binaryen_gc_call(arraySet, `(arr, index, value), FixedArray<T>)
arr

pub fn copy<T>(dest_array: FixedArray<T>, opts: {
Expand All @@ -24,14 +24,14 @@ pub fn copy<T>(dest_array: FixedArray<T>, opts: {
from_index: i32,
count: i32
}) -> FixedArray<T>
binaryen_gc_call(arrayCopy, [
binaryen_gc_call(arrayCopy, `(
dest_array,
opts.to_index,
opts.from,
opts.from_index,
opts.count
], FixedArray<T>)
), FixedArray<T>)
dest_array

pub fn length<T>(arr: FixedArray<T>) -> i32
binaryen_gc_call(arrayLen, [arr], i32)
binaryen_gc_call(arrayLen, `(arr), i32)
3 changes: 1 addition & 2 deletions std/macros.voyd
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ pub macro binaryen_gc_call(func, args, return_type)
` binaryen func: $func namespace: gc args: $args return_type: $return_type

pub macro bin_type_to_heap_type(type)
// binaryen_gc_call(modBinaryenTypeToHeapType, ` [BnrType<($type)>])
` binaryen
func: modBinaryenTypeToHeapType
namespace: gc
args: [BnrType<($type)>]
args: `(BnrType<($type)>)
12 changes: 6 additions & 6 deletions std/operators.voyd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ macro def_wasm_operator(op, wasm_fn, arg_type, return_type)
let body = ` binaryen
func: $wasm_fn
namespace: $arg_type
args: [left, right]
args: `(left, right)

` define_function $op $params return_type($return_type) $body

Expand Down Expand Up @@ -116,28 +116,28 @@ pub fn '=='(left: bool, right: bool) -> bool
binaryen
func: eq
namespace: i32
args: [left, right]
args: `(left, right)

pub fn 'and'(left: bool, right: bool) -> bool
binaryen
func: 'and'
namespace: i32
args: [left, right]
args: `(left, right)

pub fn 'or'(left: bool, right: bool) -> bool
binaryen
func: 'or'
namespace: i32
args: [left, right]
args: `(left, right)

pub fn 'xor'(left: bool, right: bool) -> bool
binaryen
func: 'xor'
namespace: i32
args: [left, right]
args: `(left, right)

pub fn not(value: bool) -> bool
binaryen
func: eqz
namespace: i32
args: [value]
args: `(value)
10 changes: 5 additions & 5 deletions std/string_lib.voyd
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ use fixed_array::all
use operators::all

fn create_string(size: i32) -> string
binaryen_gc_call(arrayNew, [bin_type_to_heap_type(string), size], string)
binaryen_gc_call(arrayNew, `(bin_type_to_heap_type(string), size), string)

fn copy_str(dest_str: string, opts: {
from: string,
to_index: i32,
from_index: i32,
count: i32
}) -> string
binaryen_gc_call(arrayCopy, [
binaryen_gc_call(arrayCopy, `(
dest_str,
opts.to_index,
opts.from,
opts.from_index,
opts.count
], string)
), string)
dest_str

pub fn length(str: string) -> i32
binaryen_gc_call(arrayLen, [str], i32)
binaryen_gc_call(arrayLen, `(str), i32)

fn compute_index(index: i32, length: i32) -> i32
if index < 0 then: length + index else: index
Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn char_code_at(str: string, index: i32) -> i32
else:
binaryen_gc_call(
arrayGet,
[str, computed_index, BnrType<i32>, BnrConst(false)],
`(str, computed_index, BnrType<i32>, BnrConst(false)),
i32
)

Expand Down

0 comments on commit 0dcc28d

Please sign in to comment.