asm 数组的双向传递

asm 数组的双向传递

asm目前测试中比js大概能快 1.2 – 1.5倍左右

c/c++ 能快1.7 – 2.3 倍左右

asm的最大优势还是书写可以使用类似ts的语法, 比go和c系更加方便,  学习和改造已有工具的成本不大

但是性能还是问题, 这点性能并不足以有动力去做改造…. 还是有点慢…….且配套设施和工具就远不如go和c了

 

index.ts

// The entry file of your WebAssembly module.
/** Creates a new array and returns it to JavaScript. */
export function createArray(length: i32): Int32Array {
  return new Int32Array(length);
}

/** Randomizes the specified array"s values. */
export function randomizeArray(arr: Int32Array): void {
  for (let i = 0, k = arr.length; i < k; ++i) {
    let value = i32((Math.random() * 2.0 - 1.0) * i32.MAX_VALUE);
    unchecked((arr[i] = value));
  }
}

export function double(data: Int32Array): Int32Array {
  return data.map((i) => i * 2);
}

/** Computes the sum of an array"s values and returns the sum to JavaScript. */
export function sumArray(data: Int32Array): i32 {
  return data.reduce((pre, cur) => pre + cur, 0);
}

// We"ll need the unique Int32Array id when allocating one in JavaScript
export const Int32Array_ID = idof<Int32Array>();

export function add(a: i32, b: i32): i32 {
  return a + b;
}

export function fib(n: i32): i32 {
  return n < 2 ? n : fib(n - 1) + fib(n - 2);
}

 

 

vue文件, 可以使用webpack打包的!

<template> </template>

<script>
import loader from "@assemblyscript/loader";

export default {
  async mounted() {
    const WASM_URL = "../../build/optimized.wasm";
    const wasmModule = await loader.instantiateStreaming(fetch(WASM_URL));
    console.log("wasmModule: ", wasmModule);
    const instance = wasmModule.instance;
    console.log("instance: ", instance);
    const exports = wasmModule.exports;
    console.log("exports", exports);
    const log = (msg = "
") => {
      console.log("log: ", msg);
    };

    for (let i = 0; i < 10; i++) log(["fib", i, exports.fib(i)].join("-"));

    function example1() {
      log("=== Example1 ===");
      const { __release, __getArray } = exports;
      let arrayPtr = exports.createArray(5);
      log(`Array pointer: ${arrayPtr}`);
      log("Initial values: " + __getArray(arrayPtr).join(", "));

      // Randomize the array in WebAssembly and log it again
      exports.randomizeArray(arrayPtr);
      log("Randomized values: " + __getArray(arrayPtr).join(", "));

      exports.double(arrayPtr);
      log("double values: " + __getArray(arrayPtr).join(", "));

      // Compute the array values" sum and log it. This will overflow i32 range.
      let total = exports.sumArray(arrayPtr);
      log(`Sum : ${total}`);

      // We are done with the array, so release the reference
      __release(arrayPtr);
      log();
    }
    example1();

    // A slightly more advanced example allocating the array in JavaScript instead
    // of WebAssembly, and utilizing a live view to modify it in WebAssembly memory.
    // Still involves just a single reference to track.
    function example2() {
      log("=== Example2 ===");

      // Obtain the necessary runtime helpers
      const {
        __retain,
        __release,
        __allocArray,
        __getArray,
        __getArrayView,
      } = exports;

      // Create a new array, but this time in JavaScript. Note that we have to
      // retain a reference to our allocation while we use it.
      let arrayPtr = __retain(
        __allocArray(exports.Int32Array_ID, [3, 4, 5, 6, 7, 8, 9])
      );
      log("Array pointer: " + arrayPtr);

      // Log its elements to make sure these are the provided values
      log("Initial values: " + __getArray(arrayPtr).join(", "));

      // Compute the array values" sum and log it
      let total = exports.sumArray(arrayPtr);
      log("Sum: " + total);

      let double = exports.double(arrayPtr);
      log("double: " + __getArray(double).join(", "));

      // Instead of copying, let"s obtain a live view on the array and modify its
      // values right in WebAssembly memory.
      let view = __getArrayView(arrayPtr);
      view.reverse();

      // Log the array"s elements, now reversed
      log("Reversed values: " + __getArray(arrayPtr).join(", "));

      // We are done with the array, so release the reference
      __release(arrayPtr);

      log();
    }
    example2();
  },
};
</script>

<style></style>

 

输出正常

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » asm 数组的双向传递