@blaze-cache/core 0.3.0-beta.1 → 0.4.0-beta.0

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/dist/wasm.js CHANGED
@@ -1,4 +1,4 @@
1
- import init, { setPanicHook, BlazeCache as WasmCache } from '../pkg/blaze_cache_wasm.js';
1
+ import init, { setPanicHook, ensureScratch, BlazeCache as WasmCache, } from '../pkg/blaze_cache_wasm.js';
2
2
  /**
3
3
  * URL of the `.wasm` binary, resolved relative to **this** module.
4
4
  *
@@ -6,6 +6,108 @@ import init, { setPanicHook, BlazeCache as WasmCache } from '../pkg/blaze_cache_
6
6
  * bundler-specific wasm plugin is required — only `type: module` output.
7
7
  */
8
8
  export const wasmUrl = new URL('../pkg/blaze_cache_wasm_bg.wasm', import.meta.url);
9
+ /** Smallest scratch reservation — covers ordinary keys (`user:42`, `key:12345`) outright. */
10
+ const SCRATCH_MIN = 64;
11
+ const textEncoder = new TextEncoder();
12
+ /**
13
+ * Encodes JS keys straight into wasm memory for the `*Raw` bindings.
14
+ *
15
+ * `passStringToWasm0` — the normal string path — pays a wasm-side malloc, a byte copy and a
16
+ * free on *every* call (~13 ns in the framework benchmark). This buffer replaces that with:
17
+ *
18
+ * * **one encode per distinct key** — a repeated key (the hot case in a read loop) is served
19
+ * from {@link KeyScratch.#last} with no write at all, just an offset;
20
+ * * **no wasm-side allocation** — bytes land in a region Rust handed us via `ensureScratch`.
21
+ *
22
+ * The buffer and this encoder are module-level singletons: the Rust scratch is one
23
+ * `thread_local` per wasm instance, so a second encoder would let one cache's "last key"
24
+ * vouch for bytes another cache has since overwritten.
25
+ *
26
+ * Not usable until {@link KeyScratch.setup} has run — and only when the instantiated module
27
+ * actually exports the `*Raw` methods (a caller may pass its own `.wasm` bytes).
28
+ */
29
+ class KeyScratch {
30
+ /** Byte offset of the encoded key inside wasm memory. */
31
+ ptr = 0;
32
+ /** Byte length of the encoded key at {@link KeyScratch.ptr}. */
33
+ len = 0;
34
+ /** `true` once the module behind this scratch is known to speak the raw protocol. */
35
+ enabled = false;
36
+ #memory = null;
37
+ #view = null;
38
+ #base = 0;
39
+ #cap = 0;
40
+ #last = null;
41
+ /**
42
+ * Binds the scratch to a freshly instantiated module, probing for everything the raw
43
+ * path needs. Leaves {@link KeyScratch.enabled} `false` — and callers on the ordinary
44
+ * string path — when any piece is missing, so a stale or foreign `.wasm` binary degrades
45
+ * instead of throwing.
46
+ */
47
+ setup(exports) {
48
+ const memory = exports.memory;
49
+ if (!(memory instanceof WebAssembly.Memory))
50
+ return;
51
+ if (typeof exports.blazecache_getRaw !== 'function' ||
52
+ typeof exports.blazecache_setRaw !== 'function' ||
53
+ typeof exports.blazecache_deleteRaw !== 'function' ||
54
+ typeof exports.ensureScratch !== 'function') {
55
+ return;
56
+ }
57
+ try {
58
+ this.#base = ensureScratch(SCRATCH_MIN);
59
+ this.#cap = SCRATCH_MIN;
60
+ }
61
+ catch {
62
+ return; // glue/binary version skew: the wrapper exists but the export does not
63
+ }
64
+ this.#memory = memory;
65
+ this.enabled = true;
66
+ }
67
+ /**
68
+ * Makes {@link KeyScratch.ptr}/{@link KeyScratch.len} describe `key`.
69
+ *
70
+ * ASCII keys take a `charCodeAt` write loop; anything else goes through `encodeInto`
71
+ * (worst case 3 bytes per UTF-16 unit, which is what the scratch is grown to). The wasm
72
+ * `memory.grow` that any intervening write may have triggered only detaches the view —
73
+ * the scratch *offset* never moves — so a refresh is a view rebuild, not a re-encode.
74
+ */
75
+ encode(key) {
76
+ if (key === this.#last)
77
+ return; // bytes are still in place: no write, no view touch
78
+ const n = key.length;
79
+ if (n * 3 > this.#cap)
80
+ this.#grow(n * 3); // worst case 3 bytes per UTF-16 unit
81
+ let view = this.#view;
82
+ const memory = this.#memory;
83
+ if (view === null || view.buffer !== memory.buffer) {
84
+ view = new Uint8Array(memory.buffer); // absolute offsets: indices match wasm pointers
85
+ this.#view = view;
86
+ }
87
+ // One pass — scan and write together, exactly like the generated `passStringToWasm0`
88
+ // (a separate pre-scan doubled the per-char cost of a distinct key).
89
+ const base = this.#base;
90
+ let j = 0;
91
+ for (; j < n; j += 1) {
92
+ const code = key.charCodeAt(j);
93
+ if (code > 0x7f)
94
+ break;
95
+ view[base + j] = code;
96
+ }
97
+ this.len =
98
+ j === n ? n : textEncoder.encodeInto(key, view.subarray(base, base + n * 3)).written;
99
+ this.ptr = base;
100
+ this.#last = key;
101
+ }
102
+ #grow(need) {
103
+ const want = need < SCRATCH_MIN ? SCRATCH_MIN : need;
104
+ this.#base = ensureScratch(want);
105
+ this.#cap = want;
106
+ this.#view = null; // the old view addressed the pre-growth buffer
107
+ }
108
+ }
109
+ /** The one key scratch shared by every {@link WasmCache} instance (see {@link KeyScratch}). */
110
+ export const keyScratch = new KeyScratch();
9
111
  let pending = null;
10
112
  /**
11
113
  * Loads and instantiates the Blaze Cache wasm module exactly once.
@@ -20,8 +122,10 @@ export function initBlazeWasm(input) {
20
122
  return pending;
21
123
  pending = (async () => {
22
124
  // wasm-bindgen accepts either a positional input or `{ module_or_path }`; the object
23
- // form is the current API (the positional one warns), so use it explicitly.
24
- await init({ module_or_path: input ?? wasmUrl });
125
+ // form is the current API (the positional one warns), so use it explicitly. The
126
+ // resolved value is the module's exports — needed for the raw-key scratch.
127
+ const exports = await init({ module_or_path: input ?? wasmUrl });
128
+ keyScratch.setup(exports);
25
129
  // Forward Rust panics to `console.error` so devtools shows something useful.
26
130
  setPanicHook();
27
131
  })();
package/dist/wasm.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"wasm.js","sourceRoot":"","sources":["../src/wasm.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,EAAE,EAAE,YAAY,EAAE,UAAU,IAAI,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAKzF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,OAAO,GAAQ,IAAI,GAAG,CAAC,iCAAiC,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAEx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AqB;IACjD,IAAI,OAAO;QAAE,OAAO,OAAO,CAAC;IAE5B,OAAO,GAAG,CAAC,KAAK,IAAI,EAAE;QACpB,qFAAqF;QACrF,gFAAgF;QAChF,2EAA2E;QAC3E,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,EAAE,cAAc,EAAE,KAAK,IAAI,OAAO,EAAE,CAAC,CAAC;QACjE,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC1B,6EAA6E;QAC7E,YAAY,EAAE,CAAC;IACjB,CAAC,CAAC,EAAE,CAAC;IAEL,yFAAyF;IACzF,sCAAsC;IACtC,KAAK,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE;QACtB,OAAO,GAAG,IAAI,CAAC;IACjB,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaze-cache/core",
3
- "version": "0.3.0-beta.1",
3
+ "version": "0.4.0-beta.0",
4
4
  "description": "WebAssembly client-side cache for the browser: SIEVE eviction, per-entry TTLs, tag invalidation and optional IndexedDB persistence",
5
5
  "keywords": [
6
6
  "cache",
@@ -19,6 +19,11 @@ export class BlazeCache {
19
19
  * Batch delete: returns `1` for each key that was present, `0` otherwise.
20
20
  */
21
21
  deleteMany(keys: string[]): Uint8Array;
22
+ /**
23
+ * [`delete`](Self::delete) with a pre-encoded key. See [`getRaw`](Self::get_raw) for
24
+ * the host contract.
25
+ */
26
+ deleteRaw(ptr: number, len: number): boolean;
22
27
  /**
23
28
  * Removes `key`, returning whether it was there.
24
29
  */
@@ -54,6 +59,21 @@ export class BlazeCache {
54
59
  * for missing or expired entries. Each hit marks the entry visited for SIEVE.
55
60
  */
56
61
  getMany(keys: string[]): any[];
62
+ /**
63
+ * [`get`](Self::get) with the key already encoded into wasm memory by the host.
64
+ *
65
+ * The TypeScript wrapper encodes each distinct key once into the shared scratch (see
66
+ * [`ensureScratch`]) and passes the offset/length, so a repeat lookup costs no
67
+ * `passStringToWasm0` allocation — that malloc + copy + free was the single largest
68
+ * component of a read.
69
+ *
70
+ * # Host contract
71
+ *
72
+ * `ptr`/`len` must address `len` bytes of valid UTF-8. The wrapper only ever passes
73
+ * bytes it encoded from a JS string (ASCII fast path or `TextEncoder`), so the slice
74
+ * below is valid by construction.
75
+ */
76
+ getRaw(ptr: number, len: number): any | undefined;
57
77
  /**
58
78
  * Returns the value stored under `key`, or `undefined` when it is missing/expired.
59
79
  *
@@ -134,7 +154,13 @@ export class BlazeCache {
134
154
  * clamped rather than trusted: a non-monotonic or out-of-range pair yields no tags
135
155
  * for that entry instead of panicking and trapping the wasm instance.
136
156
  */
137
- setMany(keys: string[], values: any, ttl_ms: Float64Array, tags_flat: string[], tag_offsets: Uint32Array): void;
157
+ setMany(keys: string[], values: any, ttl_ms: Float64Array | number[], tags_flat: string[], tag_offsets: Uint32Array | number[]): void;
158
+ /**
159
+ * [`set`](Self::set) with a pre-encoded key and **no tags** — the untagged write fast
160
+ * path. Untagged writes skip the `Vec<String>` boundary crossing entirely; use
161
+ * [`set`](Self::set) when the entry carries tags.
162
+ */
163
+ setRaw(ptr: number, len: number, value: any, ttl_ms?: number | null): void;
138
164
  /**
139
165
  * Stores `value` under `key`.
140
166
  *
@@ -178,6 +204,22 @@ export class BlazeCache {
178
204
  ttl(key: string): number | undefined;
179
205
  }
180
206
 
207
+ /**
208
+ * Ensures the shared key scratch holds at least `len` bytes and returns its offset in
209
+ * wasm memory.
210
+ *
211
+ * The host encodes keys here — once per *distinct* key; a repeated key is served from the
212
+ * wrapper's last-key cache with no write at all — and hands the offset to
213
+ * [`getRaw`](BlazeCache::get_raw)/[`setRaw`](BlazeCache::set_raw)/
214
+ * [`deleteRaw`](BlazeCache::delete_raw). That is what keeps a read from paying
215
+ * `passStringToWasm0`'s malloc + copy + free on every single call.
216
+ *
217
+ * The returned offset stays valid until the next `ensureScratch` that has to grow the
218
+ * buffer; `memory.grow` never moves it (wasm memory contents are preserved across
219
+ * growth), so the wrapper only has to refresh its `Uint8Array` view, not the pointer.
220
+ */
221
+ export function ensureScratch(len: number): number;
222
+
181
223
  /**
182
224
  * Installs a panic hook that forwards Rust panics to `console.error`, making debugging in
183
225
  * devtools far less painful. Safe to call more than once.
@@ -193,12 +235,14 @@ export interface InitOutput {
193
235
  readonly blazecache_clear: (a: number) => void;
194
236
  readonly blazecache_delete: (a: number, b: number, c: number) => number;
195
237
  readonly blazecache_deleteMany: (a: number, b: number, c: number, d: number) => void;
196
- readonly blazecache_drainDrops: (a: number) => number;
238
+ readonly blazecache_deleteRaw: (a: number, b: number, c: number) => number;
239
+ readonly blazecache_drainDrops: (a: number) => any;
197
240
  readonly blazecache_dropTracking: (a: number) => number;
198
- readonly blazecache_entries: (a: number) => number;
199
- readonly blazecache_entry: (a: number, b: number, c: number) => number;
200
- readonly blazecache_get: (a: number, b: number, c: number) => number;
241
+ readonly blazecache_entries: (a: number) => any;
242
+ readonly blazecache_entry: (a: number, b: number, c: number) => any;
243
+ readonly blazecache_get: (a: number, b: number, c: number) => any;
201
244
  readonly blazecache_getMany: (a: number, b: number, c: number, d: number) => void;
245
+ readonly blazecache_getRaw: (a: number, b: number, c: number) => any;
202
246
  readonly blazecache_has: (a: number, b: number, c: number) => number;
203
247
  readonly blazecache_hasMany: (a: number, b: number, c: number, d: number) => void;
204
248
  readonly blazecache_hasTag: (a: number, b: number, c: number, d: number, e: number) => number;
@@ -206,23 +250,30 @@ export interface InitOutput {
206
250
  readonly blazecache_keys: (a: number, b: number) => void;
207
251
  readonly blazecache_keysWithTag: (a: number, b: number, c: number, d: number) => void;
208
252
  readonly blazecache_new: (a: number) => number;
209
- readonly blazecache_peek: (a: number, b: number, c: number) => number;
253
+ readonly blazecache_peek: (a: number, b: number, c: number) => any;
210
254
  readonly blazecache_prune: (a: number, b: number) => void;
211
255
  readonly blazecache_resetStats: (a: number) => void;
212
- readonly blazecache_set: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
256
+ readonly blazecache_set: (a: number, b: number, c: number, d: any, e: number, f: number, g: number, h: number) => void;
213
257
  readonly blazecache_setDropTracking: (a: number, b: number) => void;
214
- readonly blazecache_setMany: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => void;
258
+ readonly blazecache_setMany: (a: number, b: number, c: number, d: any, e: number, f: number, g: number, h: number, i: number, j: number) => void;
259
+ readonly blazecache_setRaw: (a: number, b: number, c: number, d: any, e: number, f: number) => void;
215
260
  readonly blazecache_size: (a: number) => number;
216
261
  readonly blazecache_stats: (a: number, b: number) => void;
217
262
  readonly blazecache_tagsOf: (a: number, b: number, c: number, d: number) => void;
218
263
  readonly blazecache_touch: (a: number, b: number, c: number, d: number, e: number) => number;
219
264
  readonly blazecache_ttl: (a: number, b: number, c: number, d: number) => void;
265
+ readonly ensureScratch: (a: number) => number;
220
266
  readonly setPanicHook: () => void;
221
267
  readonly __wbindgen_export: (a: number, b: number) => number;
222
268
  readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
223
269
  readonly __wbindgen_export3: (a: number, b: number, c: number) => void;
224
270
  readonly __wbindgen_export4: (a: number) => void;
271
+ readonly __wbindgen_export5: () => number;
272
+ readonly __wbindgen_externrefs: WebAssembly.Table;
225
273
  readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
274
+ readonly __wbindgen_export6: (a: number, b: number) => void;
275
+ readonly __wbindgen_export7: (a: number) => void;
276
+ readonly __wbindgen_start: () => void;
226
277
  }
227
278
 
228
279
  export type SyncInitInput = BufferSource | WebAssembly.Module;