knitting 0.1.63 → 0.1.70

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.
Files changed (80) hide show
  1. package/README.md +525 -335
  2. package/knitting.browser.js +1 -1
  3. package/map.md +0 -6
  4. package/package.json +3 -3
  5. package/prebuilds/darwin-arm64-node-127/knitting_buffer_pointer.node +0 -0
  6. package/prebuilds/darwin-arm64-node-127/knitting_doorbell.node +0 -0
  7. package/prebuilds/darwin-arm64-node-137/knitting_buffer_pointer.node +0 -0
  8. package/prebuilds/darwin-arm64-node-137/knitting_doorbell.node +0 -0
  9. package/prebuilds/darwin-x64-node-127/knitting_buffer_pointer.node +0 -0
  10. package/prebuilds/darwin-x64-node-127/knitting_doorbell.node +0 -0
  11. package/prebuilds/darwin-x64-node-137/knitting_buffer_pointer.node +0 -0
  12. package/prebuilds/darwin-x64-node-137/knitting_doorbell.node +0 -0
  13. package/prebuilds/linux-x64-node-127/knitting_buffer_pointer.node +0 -0
  14. package/prebuilds/linux-x64-node-127/knitting_doorbell.node +0 -0
  15. package/prebuilds/linux-x64-node-137/knitting_buffer_pointer.node +0 -0
  16. package/prebuilds/linux-x64-node-137/knitting_doorbell.node +0 -0
  17. package/prebuilds/win32-x64/knitting_windows_shared_memory.dll +0 -0
  18. package/prebuilds/win32-x64-node-127/knitting_buffer_pointer.node +0 -0
  19. package/prebuilds/win32-x64-node-127/knitting_doorbell.node +0 -0
  20. package/prebuilds/win32-x64-node-127/knitting_shared_memory.node +0 -0
  21. package/prebuilds/win32-x64-node-127/knitting_shm.node +0 -0
  22. package/prebuilds/win32-x64-node-137/knitting_buffer_pointer.node +0 -0
  23. package/prebuilds/win32-x64-node-137/knitting_doorbell.node +0 -0
  24. package/prebuilds/win32-x64-node-137/knitting_shared_memory.node +0 -0
  25. package/prebuilds/win32-x64-node-137/knitting_shm.node +0 -0
  26. package/scripts/build-native-addons.ts +5 -0
  27. package/shared-memory.d.ts +3 -0
  28. package/shared-memory.js +3 -0
  29. package/src/api.js +105 -42
  30. package/src/common/with-resolvers.js +2 -5
  31. package/src/common/worker-runtime.d.ts +7 -0
  32. package/src/common/worker-runtime.js +7 -0
  33. package/src/connections/buffer-reference.d.ts +10 -36
  34. package/src/connections/buffer-reference.js +15 -170
  35. package/src/connections/node-addons.d.ts +1 -1
  36. package/src/connections/shared-array-buffer-payload.d.ts +7 -0
  37. package/src/connections/shared-array-buffer-payload.js +27 -11
  38. package/src/knitting_buffer_pointer.cc +57 -2
  39. package/src/knitting_doorbell.cc +220 -0
  40. package/src/memory/knitting-body.d.ts +44 -0
  41. package/src/memory/knitting-body.js +51 -0
  42. package/src/memory/knitting-buffer-http.d.ts +116 -0
  43. package/src/memory/knitting-buffer-http.js +255 -0
  44. package/src/memory/knitting-buffer.d.ts +250 -0
  45. package/src/memory/knitting-buffer.js +695 -0
  46. package/src/memory/lazy-region-registry.d.ts +83 -0
  47. package/src/memory/lazy-region-registry.js +355 -0
  48. package/src/memory/lock.d.ts +38 -15
  49. package/src/memory/lock.js +205 -79
  50. package/src/memory/payloadCodec.d.ts +18 -2
  51. package/src/memory/payloadCodec.js +309 -65
  52. package/src/memory/regionRegistry.d.ts +6 -0
  53. package/src/memory/regionRegistry.js +125 -240
  54. package/src/memory/shared-buffer-io.d.ts +7 -0
  55. package/src/memory/shared-buffer-io.js +34 -8
  56. package/src/runtime/deno-doorbell.d.ts +26 -0
  57. package/src/runtime/deno-doorbell.js +117 -0
  58. package/src/runtime/dispatcher.d.ts +8 -6
  59. package/src/runtime/dispatcher.js +80 -58
  60. package/src/runtime/host-arg-arena.d.ts +3 -0
  61. package/src/runtime/host-arg-arena.js +16 -0
  62. package/src/runtime/node-doorbell.d.ts +14 -0
  63. package/src/runtime/node-doorbell.js +84 -0
  64. package/src/runtime/pool.d.ts +21 -15
  65. package/src/runtime/pool.js +104 -116
  66. package/src/runtime/process-worker.d.ts +9 -0
  67. package/src/runtime/process-worker.js +22 -2
  68. package/src/runtime/tx-queue.d.ts +2 -5
  69. package/src/runtime/tx-queue.js +52 -48
  70. package/src/types.d.ts +35 -71
  71. package/src/worker/loop.js +79 -57
  72. package/src/worker/rx-queue.d.ts +2 -3
  73. package/src/worker/rx-queue.js +34 -40
  74. package/src/worker/shared-return.d.ts +9 -0
  75. package/src/worker/shared-return.js +22 -0
  76. package/src/worker/task-loader.js +1 -2
  77. package/src/worker/timers.d.ts +2 -6
  78. package/src/worker/timers.js +14 -19
  79. package/unsafe.d.ts +2 -1
  80. package/unsafe.js +2 -1
@@ -0,0 +1,250 @@
1
+ /**
2
+ * `KnittingSharedBuffer` -- a thread-level pool of shared byte
3
+ * regions whose handle mimics `SharedArrayBuffer` rather than `Buffer`.
4
+ *
5
+ * Shape:
6
+ *
7
+ * - Each thread owns one pool: its own arena SAB, its own lock sector, its
8
+ * own identity space. Nothing is shared between pools except the SABs a
9
+ * descriptor points at.
10
+ * - `alloc(n)` takes a region from the lazy registry
11
+ * (`lazy-region-registry.ts`). Identity exhaustion is not a failure: it
12
+ * falls back to a standalone `SharedArrayBuffer`, so the pool never blocks
13
+ * and never evicts a live borrow.
14
+ * - The **region** owns the bytes. Views are minted from it (`u8()`,
15
+ * `view(Ctor)`) and are plain, non-owning typed arrays. That is the same
16
+ * split JS already has between a `SharedArrayBuffer` and its views, and it
17
+ * is what keeps a `Float64Array` over the region from being silently
18
+ * orphaned when some unrelated `Uint8Array` over it is dropped.
19
+ * - The wire form is a descriptor, never bytes. A consumer thread attaches
20
+ * to the producer's SABs once and materializes its own region handle.
21
+ * - Release is one XOR into the owning lane's shared word, from whichever
22
+ * thread holds the last handle.
23
+ *
24
+ * Why GC-driven release is safe here, given the lazy registry:
25
+ *
26
+ * An identity is reusable only after the owner has *observed* its release
27
+ * toggle (`reconcile` clears the used bit only when hostLast ^ workerBits
28
+ * agree). So an identity cannot be handed out again while a release for it
29
+ * is still outstanding, which means a late release can never apply to a
30
+ * newer generation -- there is no ABA to protect against with a generation
31
+ * counter. A forgotten handle costs one identity until the collector runs;
32
+ * it cannot corrupt a live region. That degradation is capacity-only, and
33
+ * the standalone-SAB fallback is what keeps it from turning into a stall.
34
+ *
35
+ * That argument holds for exactly one releaser per identity, which is why
36
+ * sending a region is `moveTo()` and not `describe()`. Two releasers
37
+ * reintroduce the ABA the toggle cannot see: the first release lets
38
+ * `reconcile` recycle the identity into a new region, and the second one
39
+ * then frees a live stranger, whose bytes the next `alloc` hands out while
40
+ * it is still being read. `describe()` is inspection only.
41
+ *
42
+ * What it still cannot do: revoke a view that was already minted. Minting is
43
+ * checked -- `u8()` after release throws instead of handing back a window onto
44
+ * somebody else's recycled bytes -- but a view handed out earlier and retained
45
+ * past release keeps aliasing the region. That is a JS limitation, not a
46
+ * design choice, and it is why `copy()` exists.
47
+ */
48
+ import { type LazyRegionRegistryMode } from "./lazy-region-registry.js";
49
+ import { type ReadBodyOrReferOptions } from "./knitting-buffer-http.js";
50
+ import type { KnittingBody } from "./knitting-body.js";
51
+ export declare const KNITTING_BUFFER_CODEC = "knitting.buffer";
52
+ export type KnittingBufferDescriptor = {
53
+ codec: typeof KNITTING_BUFFER_CODEC;
54
+ kind: "region";
55
+ lane: number;
56
+ slot: number;
57
+ byteOffset: number;
58
+ byteLength: number;
59
+ } | {
60
+ codec: typeof KNITTING_BUFFER_CODEC;
61
+ kind: "buffer";
62
+ lane: number;
63
+ byteLength: number;
64
+ buffer: SharedArrayBuffer;
65
+ };
66
+ type ViewConstructor<T extends ArrayBufferView> = new (buffer: ArrayBufferLike, byteOffset: number, length: number) => T;
67
+ /**
68
+ * Module-internal ownership transfer. Reachable only through the allocator's
69
+ * `moveTo()`, which is the one place a region legitimately stops being ours.
70
+ */
71
+ declare const DISOWN: unique symbol;
72
+ /**
73
+ * An owned region of shared memory. Mints views; does not pretend to be one.
74
+ *
75
+ * Exported for `instanceof` and for typing. It is not constructible: regions
76
+ * come from `createKnittingAllocator().alloc()` or from adopting a descriptor.
77
+ */
78
+ export declare class KnittingSharedBuffer {
79
+ #private;
80
+ constructor(mint: symbol, buffer: SharedArrayBuffer, byteOffset: number, byteLength: number, lane?: number, slot?: number, release?: (free: boolean) => void, trim?: (byteLength: number) => void);
81
+ get byteLength(): number;
82
+ get byteOffset(): number;
83
+ /** Owning lane, and the region identity within it (-1 for a standalone SAB). */
84
+ get lane(): number;
85
+ get slot(): number;
86
+ /** True once this handle is spent, whether by `release()` or by a move. */
87
+ get released(): boolean;
88
+ /** True when ownership was handed to a consumer by `allocator.moveTo()`. */
89
+ get moved(): boolean;
90
+ /**
91
+ * The standalone SharedArrayBuffer behind an overflow region, or undefined
92
+ * for a pooled one.
93
+ *
94
+ * Pooled regions deliberately have no way to reach their backing store: it
95
+ * is the whole arena, and handing it out would expose every other live
96
+ * region's bytes. An overflow region owns its buffer outright, so it is the
97
+ * only one that can travel as a buffer rather than as a descriptor.
98
+ */
99
+ static standaloneBufferOf(region: KnittingSharedBuffer): SharedArrayBuffer | undefined;
100
+ /**
101
+ * Surrender the identity without freeing it: after this the handle is inert
102
+ * and the consumer that adopted the descriptor is the sole releaser.
103
+ *
104
+ * A no-op on a spent handle, so a double `moveTo()` cannot hand the same
105
+ * identity to two consumers -- the second call throws before reaching here.
106
+ */
107
+ static [DISOWN](region: KnittingSharedBuffer): void;
108
+ /** The byte view. Memoized: repeated calls do not construct. */
109
+ u8(): Uint8Array;
110
+ /** A typed view over the whole region. Memoized per constructor. */
111
+ view<T extends ArrayBufferView>(Ctor: ViewConstructor<T>): T;
112
+ /**
113
+ * Give back the tail of a region that was reserved larger than needed, and
114
+ * report the region as `byteLength` bytes from here on.
115
+ *
116
+ * The point is streamed input of unknown length: an HTTP body with no
117
+ * `Content-Length` cannot be sized up front, so reserve an upper bound,
118
+ * write into it, then commit what actually arrived. Views minted before the
119
+ * commit are dropped, because their length is now wrong.
120
+ *
121
+ * Shrink only. Growing would mean relocating the bytes, which is the copy
122
+ * this whole path exists to avoid.
123
+ */
124
+ commit(byteLength: number): this;
125
+ /** An independently owned copy, valid after this region is released. */
126
+ copy(): Uint8Array;
127
+ release(): void;
128
+ [Symbol.dispose](): void;
129
+ }
130
+ /** A region handed over directly. One `instanceof`. */
131
+ export declare const detectByInstance: (value: unknown) => KnittingSharedBuffer | undefined;
132
+ /**
133
+ * A view this pool minted. One WeakMap hit, and it never touches `.buffer` --
134
+ * which matters because reading `.buffer` on a heap-backed typed array can
135
+ * materialize the ArrayBuffer wrapper on first access.
136
+ */
137
+ export declare const detectByMintedView: (value: unknown) => KnittingSharedBuffer | undefined;
138
+ /**
139
+ * Any view that lands inside a pool arena, including a `subarray` of a minted
140
+ * view. Costs a `.buffer` read, a WeakMap hit, and a binary search over the
141
+ * live extent table.
142
+ */
143
+ export declare const detectByArena: (value: unknown) => KnittingBufferDescriptor | undefined;
144
+ /**
145
+ * The full check the codec would run on a payload value. Returns a descriptor
146
+ * to ship by reference, or undefined to serialize the value normally.
147
+ *
148
+ * Detection only: like `describe()`, it does not transfer ownership. Whatever
149
+ * ships the descriptor owes the consumer one of the two safe pairings
150
+ * documented on `describe()`.
151
+ */
152
+ export declare const detectRegion: (value: unknown) => KnittingBufferDescriptor | undefined;
153
+ /**
154
+ * Bump window, and therefore the pool's memory high-water.
155
+ *
156
+ * Deliberately small. A wide window makes *allocation* cheaper -- the bump
157
+ * pointer runs longer before it has to reconcile and reclaim holes -- but that
158
+ * only counts the bookkeeping, never the bytes. Once real traffic writes them
159
+ * the ranking inverts: a wide window sprays consecutive regions across cold
160
+ * memory, and the cache misses cost more than the reconcile it avoided.
161
+ * Locality wins, so the default is a small multiple of a typical live set
162
+ * rather than the whole arena.
163
+ *
164
+ * It is a default, not a recommendation: size it to `payload x in-flight`.
165
+ * Two concurrent 1 MiB payloads do not fit here, and a reservation the window
166
+ * cannot satisfy takes the overflow path rather than waiting. `stats()`
167
+ * reports `overflows` precisely so this is visible instead of mysterious.
168
+ */
169
+ export declare const DEFAULT_ARENA_BYTE_LENGTH: number;
170
+ export type KnittingAllocatorOptions = {
171
+ lane?: number;
172
+ slots?: number;
173
+ /** Bump window; see `DEFAULT_ARENA_BYTE_LENGTH` for why small is the default. */
174
+ arenaByteLength?: number;
175
+ /**
176
+ * Collector backstop for a missed release.
177
+ * true register every region
178
+ * false never register: a missed release leaks an identity
179
+ * "pressure" register only once live identities cross the watermark
180
+ *
181
+ * "pressure" applies the same argument as the lazy reconcile: while the pool
182
+ * is roomy a forgotten identity costs nothing, so paying the registration
183
+ * cost on every region to reclaim it early buys nothing. Registration starts
184
+ * when scarcity makes reclaim worth its price. Worst case is bounded: regions handed out while
185
+ * roomy are never registered, so at watermark w at most w of the identities
186
+ * can be stranded before every new region becomes reclaimable.
187
+ */
188
+ gcBackstop?: boolean | "pressure";
189
+ backstopWatermark?: number;
190
+ };
191
+ /** This pool's own counters merged with its region registry's. */
192
+ export type KnittingAllocatorStats = {
193
+ pooled: number;
194
+ overflows: number;
195
+ registered: number;
196
+ live: number;
197
+ slots: number;
198
+ mode: LazyRegionRegistryMode;
199
+ tableLength: number;
200
+ tailEnd: number;
201
+ highWater: number;
202
+ reconciles: number;
203
+ firstFits: number;
204
+ appends: number;
205
+ resets: number;
206
+ };
207
+ /** What a consumer thread needs to attach: SABs, not pointers. */
208
+ export type KnittingAllocatorTransport = {
209
+ lane: number;
210
+ lockSAB: SharedArrayBuffer;
211
+ arena: SharedArrayBuffer;
212
+ slots: number;
213
+ arenaByteLength: number;
214
+ };
215
+ /** Written out rather than inferred: JSR cannot resolve an inferred return type. */
216
+ export type KnittingAllocator = {
217
+ lane: number;
218
+ /** Bump window this pool was built with; the ceiling on a pooled region. */
219
+ arenaByteLength: number;
220
+ alloc: (byteLength: number) => KnittingSharedBuffer;
221
+ allocUpTo: (maxByteLength: number) => KnittingSharedBuffer;
222
+ allocOrRefer: (request: Request, options: ReadBodyOrReferOptions) => Promise<KnittingBody>;
223
+ describe: (region: KnittingSharedBuffer) => KnittingBufferDescriptor;
224
+ moveTo: (region: KnittingSharedBuffer) => KnittingBufferDescriptor;
225
+ reconcile: () => boolean;
226
+ stats: () => KnittingAllocatorStats;
227
+ resetCounters: () => void;
228
+ transport: () => KnittingAllocatorTransport;
229
+ };
230
+ /** The consumer side of another thread's pool: it adopts, it never allocates. */
231
+ export type AttachedKnittingAllocator = {
232
+ lane: number;
233
+ adopt: (descriptor: KnittingBufferDescriptor | SharedArrayBuffer, options?: {
234
+ gcBackstop?: boolean;
235
+ borrow?: boolean;
236
+ }) => KnittingSharedBuffer;
237
+ };
238
+ export declare const createKnittingAllocator: ({ lane, slots, arenaByteLength, gcBackstop, backstopWatermark, }?: KnittingAllocatorOptions) => KnittingAllocator;
239
+ /**
240
+ * The consumer side of another thread's pool. It never allocates; it
241
+ * materializes regions over the producer's arena and releases the producer's
242
+ * identity with one XOR into the shared word.
243
+ */
244
+ export declare const attachKnittingAllocator: ({ lane, lockSAB, arena, slots }: {
245
+ lane: number;
246
+ lockSAB: SharedArrayBuffer;
247
+ arena: SharedArrayBuffer;
248
+ slots: number;
249
+ }) => AttachedKnittingAllocator;
250
+ export {};