Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 2, 2024
1 parent d8eb6c1 commit 17542bb
Show file tree
Hide file tree
Showing 8 changed files with 803 additions and 1,003 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ Serialization Benchmarks:
| { x: f64, y: f64, z: f64 } | 3,878,604 | 44,557,996 (11.5x) | 113,203,242 (29.2x) | 172,023,231 (44.4x) | 8.61 GB/s |



Deserialization Benchmarks:

| Value | JavaScript (ops/s) | JSON-AS (ops/s) | Difference|
Expand Down
87 changes: 46 additions & 41 deletions assembly/serialize/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,54 @@ import { _intTo16, intTo16, unsafeCharCodeAt } from "../util";
let last: i32 = 0;
for (let i = 0; i < data.length; i++) {
const char = unsafeCharCodeAt(<string>data, i);
if (char === QUOTE || char === BACK_SLASH) {
result.write(<string>data, last, i);
result.writeCodePoint(BACK_SLASH);
last = i;
} else if (16 > char) {
result.write(<string>data, last, i);
last = i + 1;
switch (char) {
case BACKSPACE: {
result.write("\\b");
break;
}
case TAB: {
result.write("\\t");
break;
}
case NEW_LINE: {
result.write("\\n");
break;
}
case FORM_FEED: {
result.write("\\f");
break;
}
case CARRIAGE_RETURN: {
result.write("\\r");
break;
}
default: {
// all chars 0-31 must be encoded as a four digit unicode escape sequence
// \u0000 to \u000f handled here
result.write("\\u000");
result.write(char.toString(16));
break;
if (char > 31) {
if (char === QUOTE || char === BACK_SLASH) {
result.write(<string>data, last, i);
result.writeCodePoint(BACK_SLASH);
last = i;
}
} else {

if (16 > char) {
result.write(<string>data, last, i);
last = i + 1;
switch (char) {
case BACKSPACE: {
result.write("\\b");
break;
}
case TAB: {
result.write("\\t");
break;
}
case NEW_LINE: {
result.write("\\n");
break;
}
case FORM_FEED: {
result.write("\\f");
break;
}
case CARRIAGE_RETURN: {
result.write("\\r");
break;
}
default: {
// all chars 0-31 must be encoded as a four digit unicode escape sequence
// \u0000 to \u000f handled here
result.write("\\u000");
result.write(char.toString(16));
break;
}
}
} else {
result.write(<string>data, last, i);
last = i + 1;
// all chars 0-31 must be encoded as a four digit unicode escape sequence
// \u0010 to \u001f handled here
result.write("\\u00");
result.write(char.toString(16));
}
} else if (32 > char) {
result.write(<string>data, last, i);
last = i + 1;
// all chars 0-31 must be encoded as a four digit unicode escape sequence
// \u0010 to \u001f handled here
result.write("\\u00");
result.write(char.toString(16));
}
}
result.write(<string>data, last);
Expand Down
15 changes: 15 additions & 0 deletions bench/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ class Vec3 {
}
}

@json
class Foo {
a: i32;
b: i32;
c: i32;
d: i32;
e: i32;
f: i32;
g: i32;
h: i32;
i: i32;
j: i32;
k: i32;
}

console.log(load<i32>(changetype<usize>("12")).toString())
const vec: Vec3 = {
x: 3,
Expand Down
Binary file modified build/test.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion build/test.wasm.map

Large diffs are not rendered by default.

1,677 changes: 729 additions & 948 deletions build/test.wat

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions transform/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,9 @@ function escapeQuote(data) {
function strToCalls(data) {
let out = "";
const len = data.length - 1;
if (len >= 8) {
if (len >= 7) {
let i = 0;
for (; i < len - 7; i += 8) {
for (; i < len - 6; i += 8) {
const a = BigInt(data.charCodeAt(i));
const b = BigInt(data.charCodeAt(i + 1));
const c = BigInt(data.charCodeAt(i + 2));
Expand All @@ -466,15 +466,15 @@ function strToCalls(data) {
out += " bl.write_128(i16x8(" + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + ", " + g + ", " + h + ")); /* " + data.charAt(i) + data.charAt(i + 1) + data.charAt(i + 2) + data.charAt(i + 3) + data.charAt(i + 4) + data.charAt(i + 5) + data.charAt(i + 6) + data.charAt(i + 7) + " */\n";
}
if (i < len) {
if (len - i >= 4) {
if (len - i >= 3) {
const a = BigInt(data.charCodeAt(i));
const b = BigInt(data.charCodeAt(i + 1));
const c = BigInt(data.charCodeAt(i + 2));
const d = BigInt(data.charCodeAt(i + 3));
out += " bl.write_64(" + ((d << BigInt(48)) | (c << BigInt(32)) | (b << BigInt(16)) | a) + "/* " + data.charAt(i) + data.charAt(i + 1) + data.charAt(i + 2) + data.charAt(i + 3) + " */\n";
i += 4;
}
if (len - i >= 2) {
if (len - i >= 1) {
const a = data.charCodeAt(i);
const b = data.charCodeAt(i + 1);
out += " bl.write_32(" + ((b << 16) | a) + "); /* " + data.charAt(i) + data.charAt(i + 1) + " */\n";
Expand All @@ -491,9 +491,9 @@ function strToCalls(data) {
out += " bl.write_16(" + data.charCodeAt(len) + "); /* " + data.charAt(len) + " */\n";
}
}
else if (len >= 4) {
else if (len >= 3) {
let i = 0;
for (; i < len - 3; i += 4) {
for (; i < len - 2; i += 4) {
const a = BigInt(data.charCodeAt(i));
const b = BigInt(data.charCodeAt(i + 1));
const c = BigInt(data.charCodeAt(i + 2));
Expand All @@ -509,16 +509,16 @@ function strToCalls(data) {
out += " bl.write_16(" + data.charCodeAt(len) + "); /* " + data.charAt(len) + " */\n";
}
}
else if (len >= 2) {
else if (len >= 1) {
let i = 0;
for (; i < len; i += 2) {
const a = data.charCodeAt(i);
const b = data.charCodeAt(i + 1);
out += " bl.write_32(" + ((b << 16) | a) + "); /* " + data.charAt(i) + data.charAt(i + 1) + " */\n";
}
if (i != len)
out += " bl.write_16(" + data.charCodeAt(len) + "); /* " + data.charAt(len) + " */\n";
if (i > len)
out += " bl.write_16(" + data.charCodeAt(i) + "); /* " + data.charAt(i) + " */\n";
}
return out;
}
console.log(strToCalls('{"x":1,"y":2,"z":3}'));
console.log(strToCalls('{"a":1,"b":2","c":3,"e":4,"f":5,"g":6,"h":7,"i":8,"j":9,"k":10}'));
4 changes: 2 additions & 2 deletions transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ function escapeQuote(data: string): string {
return data.replace(/\"/g, "\\\"");
}

function strToCalls(data) {
function strToCalls(data: string): string {
let out = "";
const len = data.length - 1;
if (len >= 7) {
Expand Down Expand Up @@ -537,4 +537,4 @@ function strToCalls(data) {
return out;
}

console.log(strToCalls('{"x":1,"y":2,"z":3}'))
console.log(strToCalls('{"a":1,"b":2","c":3,"e":4,"f":5,"g":6,"h":7,"i":8,"j":9,"k":10}'))

0 comments on commit 17542bb

Please sign in to comment.