Skip to content

Commit f499deb

Browse files
committed
fix: memory problems
1 parent 039299b commit f499deb

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed

assembly/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export type Raw = string;
3636
* JSON Encoder/Decoder for AssemblyScript
3737
*/
3838
export namespace JSON {
39+
export namespace Memory {
40+
export function shrink(): void {
41+
bs.resize(64);
42+
}
43+
}
3944
/**
4045
* Serializes valid JSON data
4146
* ```js

assembly/serialize/simple/integer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export function serializeInteger<T extends number>(data: T): void {
55
bs.ensureSize(sizeof<T>() << 3);
66
const bytesWritten = itoa_buffered(bs.offset, data) << 1;
77
bs.offset += bytesWritten;
8+
bs.growSize(bytesWritten);
89
}
910

1011
// 32 {"x":,"y":,"z"}

assembly/serialize/simple/string.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function serializeString(src: string): void {
2323
const code = load<u16>(srcPtr);
2424
if (code == 34 || code == 92 || code < 32) {
2525
const remBytes = srcPtr - lastPtr;
26-
// memory.copy(bs.offset, lastPtr, remBytes);
26+
memory.copy(bs.offset, lastPtr, remBytes);
2727
bs.offset += remBytes;
2828
const escaped = load<u32>(SERIALIZE_ESCAPE_TABLE + (code << 2));
2929
if ((escaped & 0xffff) != BACK_SLASH) {
@@ -41,7 +41,7 @@ export function serializeString(src: string): void {
4141
srcPtr += 2;
4242
}
4343
const remBytes = srcEnd - lastPtr;
44-
// memory.copy(bs.offset, lastPtr, remBytes);
44+
memory.copy(bs.offset, lastPtr, remBytes);
4545
bs.offset += remBytes;
4646
store<u16>(bs.offset, QUOTE);
4747
bs.offset += 2;

assembly/test.ts

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,59 @@
11
import { JSON } from ".";
2-
import { bs } from "../modules/as-bs/assembly";
32
import { bytes } from "./util";
43

5-
// bs.ensureSize(1024);
4+
@json
5+
class Obj {
6+
public a: string = "hello";
7+
public b: string = "world";
8+
public c: string = "\"\t\f\u0000\u0001";
9+
}
10+
11+
@json
12+
class Vec3 {
13+
x: f32 = 0.0;
14+
y: f32 = 0.0;
15+
z: f32 = 0.0;
16+
}
17+
18+
@json
19+
class Player {
20+
@alias("first name")
21+
firstName!: string;
22+
lastName!: string;
23+
lastActive!: i32[];
24+
// Drop in a code block, function, or expression that evaluates to a boolean
25+
@omitif((age) => age < 18)
26+
@omitif('this.age <= 0')
27+
age!: i32;
28+
@omitnull()
29+
pos!: Vec3 | null;
30+
isVerified!: boolean;
31+
}
32+
33+
const player: Player = {
34+
firstName: "Jairus",
35+
lastName: "Tanaka",
36+
lastActive: [2, 7, 2025],
37+
age: 18,
38+
pos: {
39+
x: 3.4,
40+
y: 1.2,
41+
z: 8.3
42+
},
43+
isVerified: true
44+
};
45+
646
const a1 = JSON.stringify("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000a\u000b\u000c\u000d\u000e\u000f\u000f\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f");
7-
console.log("Bytes " + bytes(a1).toString());
8-
console.log("a1: " + a1);
47+
// console.log("Bytes " + bytes(a1).toString());
48+
console.log("a1: " + a1);
49+
50+
const obj = new Obj();
51+
const a2 = JSON.stringify(obj);
52+
// console.log("Bytes " + bytes(a2).toString());
53+
console.log("a2: " + a2);
54+
55+
const a3 = JSON.stringify(player);
56+
// console.log("Bytes " + bytes(a3).toString());
57+
console.log("a3: " + a3);
58+
59+
JSON.Memory.shrink();

modules/as-bs/assembly/index.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,35 @@ export namespace bs {
1616
/** Proposed size of output */
1717
export let stackSize: usize = 0;
1818

19+
/**
20+
* Proposes that the buffer size is should be greater than or equal to the proposed size.
21+
* If necessary, reallocates the buffer to the exact new size.
22+
* @param size - The size to propose.
23+
*/
24+
// @ts-ignore: decorator
25+
@inline export function ensureSize(size: u32): void {
26+
// console.log("Ensure " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
27+
if (offset + size > bufferSize + changetype<usize>(buffer)) {
28+
const deltaBytes = nextPowerOf2(size + 64);
29+
bufferSize += deltaBytes;
30+
// @ts-ignore: exists
31+
const newPtr = changetype<ArrayBuffer>(__renew(
32+
changetype<usize>(buffer),
33+
bufferSize
34+
));
35+
offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
36+
buffer = newPtr;
37+
}
38+
}
39+
1940
/**
2041
* Proposes that the buffer size is should be greater than or equal to the proposed size.
2142
* If necessary, reallocates the buffer to the exact new size.
2243
* @param size - The size to propose.
2344
*/
2445
// @ts-ignore: decorator
2546
@inline export function proposeSize(size: u32): void {
26-
console.log("Propose " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
47+
// console.log("Propose " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
2748
if ((stackSize += size) > bufferSize) {
2849
const deltaBytes = nextPowerOf2(size);
2950
bufferSize += deltaBytes;
@@ -44,7 +65,7 @@ export namespace bs {
4465
*/
4566
// @ts-ignore: decorator
4667
@inline export function growSize(size: u32): void {
47-
console.log("Grow " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
68+
// console.log("Grow " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
4869
if ((stackSize += size) > bufferSize) {
4970
const deltaBytes = nextPowerOf2(size + 64);
5071
bufferSize += deltaBytes;
@@ -53,7 +74,7 @@ export namespace bs {
5374
changetype<usize>(buffer),
5475
bufferSize
5576
));
56-
if (buffer != newPtr) console.log(" Old: " + changetype<usize>(buffer).toString() + "\n New: " + changetype<usize>(newPtr).toString());
77+
// if (buffer != newPtr) console.log(" Old: " + changetype<usize>(buffer).toString() + "\n New: " + changetype<usize>(newPtr).toString());
5778
offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
5879
buffer = newPtr;
5980
}

0 commit comments

Comments
 (0)