Skip to content

Commit

Permalink
fix: #101
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jan 7, 2025
1 parent d1910a6 commit 566ad98
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 192 deletions.
1 change: 1 addition & 0 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { dtoa_buffered, itoa_buffered } from "util/number";
import { serializeBool } from "./serialize/simple/bool";
import { serializeInteger } from "./serialize/simple/integer";
import { serializeFloat } from "./serialize/simple/float";
import { serializeObject } from "./serialize/simple/object";

class Nullable { }

Expand Down
124 changes: 40 additions & 84 deletions assembly/test.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,53 @@
import { VecBase } from "./types";
import { bytes } from "./util/bytes";
import { bs } from "as-bs";
import { JSON } from ".";
@json
class ItemPluginsBasket {
@alias("catalog_id")
catalogId!: string;
@alias("catalog_item_id")
catalogItemId!: string;
@alias("outlet_id")
outletId!: string;
}

@json
class Vec3 {
base: VecBase = new VecBase();
x: i32 = 1;
@omitif((v: i32) => {
if (v == 1) return true;
return false;
})
y: i32 = 2;
z: i32 = 3;
@inline __ALLOCATE(): void {
bs.ensureSize(1024);
}
@inline __SERIALIZE_BS(ptr: usize, staticSize: bool): void {
store<u64>(bs.offset, 9570664606466171, 0); // {"x"
store<u16>(bs.offset, 58, 8); // :
bs.offset += 10;
JSON.serialize_simple<i32>(load<i32>(ptr, offsetof<this>("x")), staticSize);
store<u64>(bs.offset, 9570668901433388, 0); // ,"y"
store<u16>(bs.offset, 58, 8); // :
bs.offset += 10;
JSON.serialize_simple<i32>(load<i32>(ptr, offsetof<this>("y")), staticSize);
store<u64>(bs.offset, 9570673196400684, 0); // ,"z"
store<u16>(bs.offset, 58, 8); // :
bs.offset += 10;
JSON.serialize_simple<i32>(load<i32>(ptr, offsetof<this>("z")), staticSize);
store<u16>(bs.offset, 125, 0); // }
bs.offset += 2;
}
class ItemPlugins {
@omitnull()
basket!: ItemPluginsBasket | null;
}

function strToNum(data: string): void {
console.log(data);
let srcPtr = changetype<usize>(data);
let n = bytes(data);
while (n >= 8) {
console.log(" i64: " + load<u64>(srcPtr).toString());
n -= 8;
}
while (n >= 4) {
console.log(" i32: " + load<u32>(srcPtr).toString());
n -= 4;
}
while (n >= 2) {
console.log(" i16: " + load<u16>(srcPtr).toString());
n -= 16;
}
while (n >= 0) {
console.log(" i8: " + load<u8>(srcPtr).toString());
n -= 1;
}
@json
class Item {
id!: string;
@omitnull()
plugins!: ItemPlugins | null;
}

@json
class Vec3 {
vxyz: i8 = 1;
y: i8 = 2;
z: i8 = 3;
}

console.log(load<u32>(changetype<usize>("{}")).toString())
strToNum(',"z":')
const vec: Vec3 = {
base: {
base: 9
},
x: 1,
vxyz: 1,
y: 2,
z: 3
}

bs.ensureSize(1024);
// vec.__SERIALIZE_BS(changetype<usize>(vec), true);
let out = __new(bs.offset - bs.buffer, idof<string>())
const serialized = bs.shrinkTo<string>();
console.log("Serialized: " + JSON.stringify(vec));

bench("Serialize Object (New)", () => {
vec.__SERIALIZE_BS(changetype<usize>(vec), true);
bs.out<string>();
});

// __reset()

// bench("Serialize Object (Old)", () => {
// vec.__SERIALIZE(changetype<usize>(vec));
// });

const item: Item = {
id: "1",
plugins: {
basket: {
catalogId: "1",
catalogItemId: "2",
outletId: "3"
}
}
};

function bench(desc: string, routine: () => void): void {
const start = Date.now();
const totalOps = 20_000_000;
let ops = totalOps;
while (--ops) routine();
const elapsed = Date.now() - start;
const opsPerSecond = Math.round(f64(totalOps) / (f64(elapsed) / 1000.0))
console.log(desc + ": " + elapsed.toString() + "ms @ " + opsPerSecond.toString() + "ops/s");
}
// const deserialized = JSON.parse<Foo>(serialized);
// console.log("Deserialized: " + JSON.stringify(deserialized));
bs.ensureSize(2048);
console.log(JSON.stringify(vec));
bs.ensureSize(2048);
console.log(JSON.stringify(item));
2 changes: 1 addition & 1 deletion transform/lib/builder.js.map

Large diffs are not rendered by default.

122 changes: 72 additions & 50 deletions transform/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion transform/lib/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 566ad98

Please sign in to comment.