Skip to content

Instantly share code, notes, and snippets.

@tangzhongliang
Last active January 2, 2020 03:53
Show Gist options
  • Save tangzhongliang/770f92ef3fc999be598037e3e40077a5 to your computer and use it in GitHub Desktop.
Save tangzhongliang/770f92ef3fc999be598037e3e40077a5 to your computer and use it in GitHub Desktop.
AssemblyScript 入门练习
https://my.oschina.net/yushulx/blog/1927717
提高nodejs性能的方法有两种
1.通过node-gyp来编译一个Node.js C/C++扩展。2.把C/C++代码编译成WebAssembly
WebAssembly
是跨平台的,使用与前端,如果对性能要求不太高的情况下,也可以用于后端
耗时是c++的1-7倍,在不和js数据通信的情况下耗时接近于C
AS本质上是静态的字节码语言,可以和jvm比较,理论上AS的性能可以追上java,并且由于AS使用的是基本特性,没有java的复杂,速度超过java也是有可能的
也就是js兼容了静态语言和脚本语言的优点
typescript转webassembly更加方便,所以未来typescript是主流趋势
Node扩展
需要在不同的平台上编译,肯定是性能最好的,在复杂应用中是WebAssembly的3倍
c++扩展永远是最快的
// https://github.com/torch2424/wasm-by-example/blob/master/demo-util/
export const wasmBrowserInstantiate = async (wasmModuleUrl, importObject) => {
let response = undefined;
if (!importObject) {
importObject = {
env: {
abort: () => console.log("Abort!")
}
};
}
// Check if the browser supports streaming instantiation
if (WebAssembly.instantiateStreaming) {
// Fetch the module, and instantiate it as it is downloading
response = await WebAssembly.instantiateStreaming(
fetch(wasmModuleUrl),
importObject
);
} else {
// Fallback to using fetch to download the entire module
// And then instantiate the module
const fetchAndInstantiateTask = async () => {
const wasmArrayBuffer = await fetch(wasmModuleUrl).then(response =>
response.arrayBuffer()
);
return WebAssembly.instantiate(wasmArrayBuffer, importObject);
};
response = await fetchAndInstantiateTask();
}
return response;
};
const runWasmAdd = async () => {
// Instantiate our wasm module
const wasmModule = await wasmBrowserInstantiate("./hello-world.wasm");
// Call the Add function export from wasm, save the result
const addResult = wasmModule.instance.exports.add(24, 24);
// Set the result onto the body
document.body.textContent = `Hello World! addResult: ${addResult}`;
};
runWasmAdd();
//!!!!!!!!!!!!!!! asc hello-world.ts -b hello-world.wasm
//!!!!!!!!!!!!!!! https://docs.assemblyscript.org/
// This exports an add function.
// It takes in two 32-bit integer values
// And returns a 32-bit integer value.
export function add(a: i32, b: i32): i32 {
return a + b;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello World - AssemblyScript</title>
<script type="module" src="./hello-world.js"></script>
</head>
<body></body>
</html>
// way to pass data JS/WASM context
let instance = ...;
let myJSArray = new Float32Array(...);
let length = myJSArray.length;
let myWasmArrayPtr = instance.exports.allocateF32Array(length);
let myWasmArray = new Float32Array(instance.exports.memory.buffer, myWasmArrayPtr, length);
// Copy data in to be used by WebAssembly.
myWasmArray.set(myJSArray);
// Process the data in the array.
instance.exports.processF32Array(myWasmArrayPtr, length);
// Copy data out to JavaScript.
myJSArray.set(myWasmArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment