json-as 1.1.23 → 1.1.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change Log
2
2
 
3
+ ## 2025-11-28 - 1.1.24
4
+
5
+ - feat: Implement a moving average window to determine buffer size (essentially, allow the buffer size to shrink) [#163](https://github.com/JairusSW/json-as/pull/163)
6
+
3
7
  ## 2025-11-06 - 1.1.23
4
8
 
5
9
  - fix: Map keys should follow proper typing and quote rules [#161](https://github.com/JairusSW/json-as/issues/161)
@@ -1,6 +1,7 @@
1
1
  import { bs } from "../lib/as-bs";
2
2
  import { JSON } from ".";
3
3
 
4
+
4
5
  @json
5
6
  class Example {
6
7
  constructor(
@@ -36,6 +37,7 @@ class Example {
36
37
  bs.offset += 2;
37
38
  }
38
39
 
40
+
39
41
  @inline
40
42
  __INITIALIZE(): this {
41
43
  store<string>(changetype<usize>(this), "", offsetof<this>("a"));
package/lib/as-bs.ts CHANGED
@@ -1,17 +1,19 @@
1
1
  import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
2
+ const SHRINK_EVERY_N: u32 = 200;
3
+ const MIN_BUFFER_SIZE: u32 = 128;
2
4
 
3
5
  /**
4
6
  * Central buffer namespace for managing memory operations.
5
7
  */
6
8
  export namespace bs {
7
- /** Current buffer pointer. */ // @ts-ignore
8
- export let buffer: ArrayBuffer = new ArrayBuffer(128); //__new(128, idof<ArrayBuffer>());
9
+ /** Current buffer pointer. */
10
+ export let buffer: ArrayBuffer = new ArrayBuffer(MIN_BUFFER_SIZE);
9
11
 
10
12
  /** Current offset within the buffer. */
11
13
  export let offset: usize = changetype<usize>(buffer);
12
14
 
13
15
  /** Byte length of the buffer. */
14
- let bufferSize: usize = 128;
16
+ let bufferSize: usize = MIN_BUFFER_SIZE;
15
17
 
16
18
  /** Proposed size of output */
17
19
  export let stackSize: usize = 0;
@@ -19,6 +21,9 @@ export namespace bs {
19
21
  let pauseOffset: usize = 0;
20
22
  let pauseStackSize: usize = 0;
21
23
 
24
+ let typicalSize: u32 = MIN_BUFFER_SIZE;
25
+ let counter: u32 = 0;
26
+
22
27
  /**
23
28
  * Stores the state of the buffer, allowing further changes to be reset
24
29
  */
@@ -56,9 +61,8 @@ export namespace bs {
56
61
  */
57
62
  // @ts-ignore: decorator
58
63
  @inline export function ensureSize(size: u32): void {
59
- // console.log("Ensure " + (size).toString() + " -> " + (offset + size).toString() + " (" + (offset - changetype<usize>(buffer)).toString() + ")");
60
64
  if (offset + size > bufferSize + changetype<usize>(buffer)) {
61
- const deltaBytes = size + 128;
65
+ const deltaBytes = size + MIN_BUFFER_SIZE;
62
66
  bufferSize += deltaBytes;
63
67
  // @ts-ignore: exists
64
68
  const newPtr = changetype<ArrayBuffer>(__renew(changetype<usize>(buffer), bufferSize));
@@ -74,7 +78,6 @@ export namespace bs {
74
78
  */
75
79
  // @ts-ignore: decorator
76
80
  @inline export function proposeSize(size: u32): void {
77
- // console.log("Propose " + (size).toString() + " -> " + (stackSize + size).toString() + " (" + (offset - changetype<usize>(buffer)).toString() + ")");
78
81
  if ((stackSize += size) > bufferSize) {
79
82
  const deltaBytes = size;
80
83
  bufferSize += deltaBytes;
@@ -86,19 +89,17 @@ export namespace bs {
86
89
  }
87
90
 
88
91
  /**
89
- * Increases the proposed size by n + 128 if necessary.
92
+ * Increases the proposed size by n + MIN_BUFFER_SIZE if necessary.
90
93
  * If necessary, reallocates the buffer to the exact new size.
91
94
  * @param size - The size to grow by.
92
95
  */
93
96
  // @ts-ignore: decorator
94
97
  @inline export function growSize(size: u32): void {
95
- // console.log("Grow " + (size).toString() + " -> " + (stackSize + size).toString() + " (" + (offset - changetype<usize>(buffer)).toString() + ")");
96
98
  if ((stackSize += size) > bufferSize) {
97
- const deltaBytes = size + 128;
99
+ const deltaBytes = size + MIN_BUFFER_SIZE;
98
100
  bufferSize += deltaBytes;
99
101
  // @ts-ignore
100
102
  const newPtr = changetype<ArrayBuffer>(__renew(changetype<usize>(buffer), bufferSize));
101
- // if (buffer != newPtr) console.log(" Old: " + changetype<usize>(buffer).toString() + "\n New: " + changetype<usize>(newPtr).toString());
102
103
  offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
103
104
  buffer = newPtr;
104
105
  }
@@ -147,11 +148,20 @@ export namespace bs {
147
148
  // @ts-ignore: Decorator valid here
148
149
  @inline export function out<T>(): T {
149
150
  const len = offset - changetype<usize>(buffer);
150
- // console.log("Out " + (len).toString() + " -> " + (stackSize).toString() + " (" + (offset - changetype<usize>(buffer)).toString() + ")");
151
151
  // @ts-ignore: exists
152
152
  const _out = __new(len, idof<T>());
153
153
  memory.copy(_out, changetype<usize>(buffer), len);
154
154
 
155
+ counter++;
156
+ typicalSize = (typicalSize + <u32>len) >> 1;
157
+
158
+ if (counter >= SHRINK_EVERY_N) {
159
+ if (bufferSize > typicalSize << 2) {
160
+ resize(typicalSize << 1);
161
+ }
162
+ counter = 0;
163
+ }
164
+
155
165
  offset = changetype<usize>(buffer);
156
166
  stackSize = 0;
157
167
  return changetype<T>(_out);
@@ -171,24 +181,18 @@ export namespace bs {
171
181
  if (len != changetype<OBJECT>(dst - TOTAL_OVERHEAD).rtSize) __renew(len, idof<T>());
172
182
  memory.copy(dst, changetype<usize>(buffer), len);
173
183
 
184
+ counter++;
185
+ typicalSize = (typicalSize + len) >> 1;
186
+
187
+ if (counter >= SHRINK_EVERY_N) {
188
+ if (bufferSize > typicalSize << 2) {
189
+ resize(typicalSize << 1);
190
+ }
191
+ counter = 0;
192
+ }
193
+
174
194
  offset = changetype<usize>(buffer);
175
195
  stackSize = 0;
176
196
  return changetype<T>(dst);
177
197
  }
178
198
  }
179
-
180
- // @ts-ignore: Decorator valid here
181
- // @inline function nextPowerOf2(n: u32): u32 {
182
- // return 1 << (32 - clz(n - 1));
183
- // }
184
-
185
- // @ts-ignore: Decorator valid here
186
- @inline function bytes<T>(o: T): i32 {
187
- if (isInteger<T>() || isFloat<T>()) {
188
- return sizeof<T>();
189
- } else if (isManaged<T>() || isReference<T>()) {
190
- return changetype<OBJECT>(changetype<usize>(o) - TOTAL_OVERHEAD).rtSize;
191
- } else {
192
- throw new Error("Cannot convert type " + nameof<T>() + " to bytes!");
193
- }
194
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "1.1.23",
3
+ "version": "1.1.24",
4
4
  "author": "Jairus Tanaka",
5
5
  "repository": {
6
6
  "type": "git",