Skip to content

Commit

Permalink
integrate transform optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Aug 6, 2024
1 parent f0cbb81 commit 9e1ea78
Show file tree
Hide file tree
Showing 6 changed files with 616 additions and 19 deletions.
10 changes: 4 additions & 6 deletions assembly/custom/itoa.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const f1_10000 = 26844;

@inline export function itoa_fast(out: usize, val: u32): void {
const low = val % 100000;
const high = val / 100000;

let tmp_high = high * (f1_10000) - (high / 4);
let tmp_low = low * (f1_10000) - (low / 4);
let tmp_high = high * (26844) - (high / 4);
let tmp_low = low * (26844) - (low / 4);

const h1 = 48 + (tmp_high >> 28);
tmp_high = (tmp_high & 0x0fffffff) * 5;
tmp_high = (tmp_high & 268435455) * 5;

const h2 = 48 + (tmp_high >> 27);
tmp_high = (tmp_high & 134217727) * 5;
Expand All @@ -22,7 +20,7 @@ const f1_10000 = 26844;
const h5 = 48 + (tmp_low >> 24);

const l1 = 48 + (tmp_low >> 28);
tmp_low = (tmp_low & 0x0fffffff) * 5;
tmp_low = (tmp_low & 268435455) * 5;

const l2 = 48 + (tmp_low >> 27);
tmp_low = (tmp_low & 134217727) * 5;
Expand Down
24 changes: 12 additions & 12 deletions bench/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import { itoa_buffered } from "util/number";

@json
class Vec3 {
x: i32;
y: i32;
z: i32;
__SERIALIZE_BS(): void {
bs.write_128_u(i16x8(123, 34, 120, 34, 58, 49, 44, 34)); /* {"x":1," */
bs.write_128_u(i16x8(121, 34, 58, 50, 44, 34, 122, 34)); /* y":2,"z" */
bs.write_32_u(3342394); /* :3 */
bs.write_16_u(125); /* } */
}
x: i32;
y: i32;
z: i32;
__SERIALIZE_BS(): void {
bs.write_128_u(i16x8(123, 34, 120, 34, 58, 49, 44, 34)); /* {"x":1," */
bs.write_128_u(i16x8(121, 34, 58, 50, 44, 34, 122, 34)); /* y":2,"z" */
bs.write_32_u(3342394); /* :3 */
bs.write_16_u(125); /* } */
}
}
const out = memory.data(1000);
const vec: Vec3 = {
x: 3,
y: 1,
z: 8,
x: 3,
y: 1,
z: 8,
};

bench("itoa fast", () => {
Expand Down
Binary file modified bench/benchmark.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"scripts": {
"test": "ast test && rm -rf ./build/",
"pretest": "rm -rf ./build/ && ast build",
"build:test": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm --enable simd",
"build:test": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform --transform ./transform/lib/afaf.js -o ./build/test.wasm --enable simd",
"build:bench": "asc bench/benchmark.ts -o bench/benchmark.wasm --transform ./transform --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime stub --enable simd",
"build:bench:fast": "asc bench/benchmark.ts -o bench/benchmark.wasm --transform ./transform --transform ./transform/lib/afaf.js --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime stub --enable simd",
"bench:wasmtime": "wasmtime ./bench/benchmark.wasm",
"bench:wasmer": "wasmer --llvm ./bench/benchmark.wasm",
"build:transform": "tsc -p ./transform",
Expand Down
82 changes: 82 additions & 0 deletions transform/lib/afaf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { IntegerLiteralExpression, Node, } from "assemblyscript/dist/assemblyscript.js";
import { Transform } from "assemblyscript/dist/transform.js";
import { toString, isStdlib } from "visitor-as/dist/utils.js";
import { Visitor } from "./visitor.js";
class AFAFTransform extends Visitor {
constructor() {
super(...arguments);
this.topStmts = [];
this.seenLits = new Set();
}
changeInt(_node) {
if (!_node)
return null;
if (!(_node instanceof IntegerLiteralExpression))
return null;
const node = _node;
const val = i64_to_string(node.value);
if (this.seenLits.has(val))
return Node.createIdentifierExpression("_" + val, node.range);
const variable = Node.createVariableStatement(null, [
Node.createVariableDeclaration(Node.createIdentifierExpression("_" + val, node.range), null, 8 /* CommonFlags.Const */, null, node, node.range)
], node.range);
const reference = Node.createIdentifierExpression("_" + val, node.range);
this.topStmts.push(variable);
this.seenLits.add(val);
return reference;
}
visitBinaryExpression(node) {
super.visitBinaryExpression(node);
const left = node.left;
const right = node.right;
node.left = this.changeInt(left) || node.left;
node.right = this.changeInt(right) || node.right;
}
visitParenthesizedExpression(node) {
super.visitParenthesizedExpression(node);
node.expression = this.changeInt(node.expression) || node.expression;
}
visitCallExpression(node) {
for (let i = 0; i < node.args.length; i++) {
node.args[i] = this.changeInt(node.args[i]) || node.args[i];
}
}
visitSource(node) {
super.visitSource(node);
this.seenLits.clear();
for (const stmt of this.topStmts) {
node.statements.unshift(stmt);
}
if (this.topStmts) {
console.log(node.normalizedPath + "\n\n" + toString(node));
this.topStmts = [];
}
}
}
export default class Transformer extends Transform {
// Trigger the transform after parse.
afterParse(parser) {
// Create new transform
const transformer = new AFAFTransform();
// Sort the sources so that user scripts are visited last
const sources = parser.sources
.sort((_a, _b) => {
const a = _a.internalPath;
const b = _b.internalPath;
if (a[0] === "~" && b[0] !== "~") {
return -1;
}
else if (a[0] !== "~" && b[0] === "~") {
return 1;
}
else {
return 0;
}
});
// Loop over every source
for (const source of sources) {
if (!isStdlib(source))
transformer.visit(source);
}
}
}
Loading

0 comments on commit 9e1ea78

Please sign in to comment.