Skip to content

Commit

Permalink
Improve TypedArray#map allocation (AssemblyScript#821)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey authored and dcodeIO committed Sep 10, 2019
1 parent bf6967a commit a4dd3a9
Show file tree
Hide file tree
Showing 3 changed files with 1,047 additions and 493 deletions.
16 changes: 11 additions & 5 deletions std/assembly/typedarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1464,13 +1464,19 @@ function MAP<TArray extends ArrayBufferView, T>(
array: TArray,
fn: (value: T, index: i32, self: TArray) => T,
): TArray {
var length = array.length;
var len = array.length;
var dataStart = array.dataStart;
var out = instantiate<TArray>(length);
var outDataStart = out.dataStart;
for (let i = 0; i < length; i++) {

var byteLength = len << alignof<T>();
var out = changetype<TArray>(__alloc(offsetof<TArray>(), idof<TArray>()));
var buffer = __alloc(byteLength, idof<ArrayBuffer>());
out.data = changetype<ArrayBuffer>(buffer); // retain
out.dataStart = buffer;
out.dataLength = byteLength;

for (let i = 0; i < len; i++) {
store<T>(
outDataStart + (<usize>i << alignof<T>()),
buffer + (<usize>i << alignof<T>()),
fn(load<T>(dataStart + (<usize>i << alignof<T>())), i, array)
);
}
Expand Down
Loading

0 comments on commit a4dd3a9

Please sign in to comment.