balloc 0.0.0-stage → 1.0.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/README.en.md ADDED
@@ -0,0 +1,433 @@
1
+ You can also find this documentation translated into other languages:
2
+ | Idioma | Link |
3
+ |---|---|
4
+ | Arabic | [README.ar.md](README.ar.md) |
5
+ | Chinese | [README.zh.md](README.zh.md) |
6
+ | Spanish | [README.es.md](README.es.md) |
7
+ | English | [README.en.md](README.en.md) |
8
+ | Japanese | [README.jp.md](README.jp.md) |
9
+ | Portuguese | [README.pt.md](README.pt.md) |
10
+
11
+ # Balloc.js
12
+
13
+ ## A bump allocation memory allocator for JavaScript.
14
+
15
+ **`Balloc`** is an incremental memory allocator based on bump allocation that maintains contiguous data blocks identified by compact numeric handles.
16
+
17
+ ## Motivation
18
+
19
+ The JavaScript/Node.js ecosystem has historically lacked low-level infrastructure for efficient Big Data processing such as parsing, editing, and continuous indexing of large volumes of CSV files, logs, and time series data, while the vast majority of data libraries available in the ecosystem use naive models based on reference graphs and dynamic allocation of object instances (`new Node()`). These approaches impose severe limitations such as:
20
+ * Heap and GC overhead, as the proliferation of millions of individual objects saturates V8's reference graph, causing unpredictable Garbage Collector pauses.
21
+ * Lack of layout control (AoS vs. SoA), where libraries ignore data organization as Array of Structures (AoS) or Structure of Arrays (SoA), resulting in high fragmentation and poor utilization of the CPU's L1/L2/L3 cache.
22
+ * Lack of primitives for building advanced storage engines such as Log-Structured Merge-trees, which require extremely high-throughput mutable write arenas followed by compacted immutability, something impractical on top of conventional JS abstractions.
23
+
24
+ Balloc was created to fill this engineering gap. It was conceived as a foundational memory allocation and low-abstraction dynamic arena management engine. By providing explicit control over contiguous memory layout through TypedArrays, Balloc provides the physical foundation required for the JavaScript ecosystem to support industrial-grade data structures such as LSM-Trees, MemTables, Quotient Filters, and Big Data processors, matching native approaches in C++, Rust, or Go in efficiency and predictability.
25
+
26
+
27
+ ## Design
28
+
29
+ As a low-abstraction memory allocation engine and dynamic arena manager, **`Balloc`** was designed to fit into the JavaScript runtime without competing with JavaScript's native high-level data structures (such as `Object`, `Map`, or `Array`), instead operating at the immediately lower layer to provide **deterministic, high-performance contiguous memory management** without overloading the environment's *Garbage Collector*.
30
+
31
+ The allocator combines three mechanisms:
32
+
33
+ ```text
34
+ ┌─────────────────┐
35
+ │ Balloc │
36
+ └────────┬────────┘
37
+ │
38
+ ┌────────────────┼────────────────┐
39
+ │ │ │
40
+ ▼ ▼ ▼
41
+ Bump allocation Recycling Compaction
42
+ │ │ │
43
+ Fast sequential Reuse of freed regions ReBump / gc()
44
+ sequencial liberadas
45
+
46
+ ```
47
+
48
+ * The bump allocator provides sequential allocation on top of `dataBus`.
49
+ * The recycling system allows previously freed regions to be reused when their capacity matches a new allocation.
50
+ * `gc()` performs explicit compaction when the *heap* contains fragmented free regions.
51
+
52
+ This gives the allocator a simple memory model while maintaining explicit control over capacity, occupancy, recycling, and compaction, because `Balloc` separates the data block from the metadata block.
53
+
54
+ ```text
55
+ Balloc
56
+ │
57
+ ┌─────────┴─────────┐
58
+ │ │
59
+ dataBus controlBus
60
+ │ │
61
+ conteúdo headers
62
+
63
+ ```
64
+
65
+ * `dataBus` contains the set of data blocks.
66
+
67
+
68
+ * `controlBus` contains the set of fixed-size headers that serve as descriptors for the data blocks.
69
+
70
+ Each block is externally identified by a 43-bit logical handle:
71
+
72
+ ```text
73
+ ┌───────────────────────┬───────────────────────────────┐
74
+ │ generation — 11 bits │ header address — 32 bits │
75
+ └───────────────────────┴───────────────────────────────┘
76
+
77
+ ```
78
+
79
+ The handle itself does not directly contain the address of the data block. It identifies the block header, which in turn contains the data address, capacity, and block occupancy.
80
+
81
+ The generation is used by the allocator's *cookie* validation mechanism to detect invalid, stale, or corrupted handles.
82
+
83
+ ## Installation
84
+
85
+ In Node.js, install the following NPM package:
86
+
87
+ ```bash
88
+ npm install Balloc
89
+
90
+ ```
91
+
92
+ In Bun, install:
93
+
94
+ ```bash
95
+ bun add Balloc
96
+
97
+ ```
98
+
99
+ In Deno, install:
100
+
101
+ ```bash
102
+ deno add npm:Balloc
103
+
104
+ ```
105
+
106
+ Using Yarn:
107
+
108
+ ```bash
109
+ yarn add Balloc
110
+
111
+ ```
112
+
113
+ In HTML, use the unpkg CDN:
114
+
115
+ ```text
116
+ [https://unpkg.com/Balloc](https://unpkg.com/Balloc)
117
+
118
+ ```
119
+
120
+ Or use its localized docstring versions for some languages:
121
+
122
+ ```
123
+ Arabic: [https://unpkg.com/Balloc/src/ar/Balloc.mjs](https://unpkg.com/Balloc/src/ar/Balloc.mjs)
124
+ English: [https://unpkg.com/Balloc/src/en/Balloc.mjs](https://unpkg.com/Balloc/src/en/Balloc.mjs)
125
+ Spanish: [https://unpkg.com/Balloc/src/es/Balloc.mjs](https://unpkg.com/Balloc/src/es/Balloc.mjs)
126
+ Japanese: [https://unpkg.com/Balloc/src/jp/Balloc.mjs](https://unpkg.com/Balloc/src/jp/Balloc.mjs)
127
+ Chinese: [https://unpkg.com/Balloc/src/zh/Balloc.mjs](https://unpkg.com/Balloc/src/zh/Balloc.mjs)
128
+
129
+ ```
130
+
131
+ Ou ainda utilize o CDN jsDelivr:
132
+
133
+ ```text
134
+ [https://cdn.jsdelivr.net/npm/Balloc](https://cdn.jsdelivr.net/npm/Balloc)
135
+
136
+ ```
137
+
138
+ CDNs with localization support:
139
+
140
+ ```
141
+ Arabic: [https://cdn.jsdelivr.net/npm/Balloc/src/ar/Balloc.mjs](https://cdn.jsdelivr.net/npm/Balloc/src/ar/Balloc.mjs)
142
+ English: [https://cdn.jsdelivr.net/npm/Balloc/src/en/Balloc.mjs](https://cdn.jsdelivr.net/npm/Balloc/src/en/Balloc.mjs)
143
+ Spanish: [https://cdn.jsdelivr.net/npm/Balloc/src/es/Balloc.mjs](https://cdn.jsdelivr.net/npm/Balloc/src/es/Balloc.mjs)
144
+ Japanese: [https://cdn.jsdelivr.net/npm/Balloc/src/jp/Balloc.mjs](https://cdn.jsdelivr.net/npm/Balloc/src/jp/Balloc.mjs)
145
+ Chinese: [https://cdn.jsdelivr.net/npm/Balloc/src/zh/Balloc.mjs](https://cdn.jsdelivr.net/npm/Balloc/src/zh/Balloc.mjs)
146
+
147
+ ```
148
+
149
+ ## Basic usage
150
+
151
+ ESM:
152
+
153
+ ```js
154
+ import Balloc from "Balloc";
155
+
156
+ const memory = new Balloc();
157
+
158
+ const handler = memory.alloc(100);
159
+
160
+ memory.push(handler, 10);
161
+ memory.push(handler, 20);
162
+ memory.push(handler, 30);
163
+
164
+ console.log(memory.length(handler));
165
+ console.log(memory.occupied(handler));
166
+
167
+ console.log(memory.pop(handler));
168
+ console.log(memory.pop(handler));
169
+
170
+ memory.free(handler);
171
+
172
+ ```
173
+
174
+ CJS:
175
+
176
+ ```js
177
+ const Balloc = require("Balloc").default;
178
+
179
+ const memory = new Balloc();
180
+
181
+ const handler = memory.alloc(100);
182
+
183
+ memory.push(handler, 10);
184
+ memory.push(handler, 20);
185
+ memory.push(handler, 30);
186
+
187
+ console.log(memory.length(handler));
188
+ console.log(memory.occupied(handler));
189
+
190
+ console.log(memory.pop(handler));
191
+ console.log(memory.pop(handler));
192
+
193
+ memory.free(handler);
194
+
195
+ ```
196
+
197
+ ## Getting started
198
+
199
+ The constructor is:
200
+
201
+ ```js
202
+ new Balloc(size, bucketSize, metadataSize)
203
+
204
+ ```
205
+
206
+ | Parameter | Type | Default | Description |
207
+ | --- | --- | --- | --- |
208
+ | `size` | `number` | `65535` | Initial `dataBus` capacity in bytes |
209
+ | `bucketSize` | `BucketType` | `32u` | Element size in bits |
210
+ | `metadataSize` | `number` | `1024` | Initial `controlBus` capacity in bytes |
211
+
212
+ Example:
213
+
214
+ ```js
215
+ const memory = new Balloc();
216
+
217
+ ```
218
+
219
+ For 32-bit unsigned integer elements:
220
+
221
+ ```js
222
+ const memory = new Balloc(65535, "32u");
223
+
224
+ ```
225
+
226
+ `size` must be an integer in the range `[2, 0x80000000]`.
227
+
228
+ `bucketSize` must be a `BucketType`, a string in the format `[size in bits][data type]`, with the following accepted values:
229
+
230
+ | Signed Integer | Unsigned Integer | Float |
231
+ | --- | --- | --- |
232
+ | `8i` | `8u` | |
233
+ | `16i` | `16u` | |
234
+ | `32i` | `32u` | `32f` |
235
+ | `64i` | `64u` | `64f` |
236
+
237
+ *The half-precision type (`16f`), being a recent addition still undergoing consolidation in JavaScript specifications and runtimes, is not yet guaranteed to be supported by Balloc. Although the type is present in the library, its availability may be uncertain for use in your project.*
238
+
239
+ `metadataSize` must be an integer in the range `[16, 0x80000000]`.
240
+
241
+ The allocation size of a block is rounded up to the smallest power of two capable of accommodating the requested number of elements.
242
+
243
+ Por exemplo:
244
+
245
+ ```js
246
+ const handler = memory.alloc(100);
247
+
248
+ ```
249
+
250
+ does not necessarily create a block with exactly 100 physical slots. The allocator reserves a capacity that is a power of two.
251
+
252
+ Conceptually:
253
+
254
+ ```text
255
+ requested: 100 elements
256
+ capacity: 128 elements
257
+
258
+ ```
259
+
260
+ Physical capacity and logical occupancy are distinct:
261
+
262
+ ```text
263
+ length(handler) → physical capacity
264
+ occupied(handler) → occupied elements
265
+ byteSize(handler) → reserved physical bytes
266
+
267
+ ```
268
+
269
+ This distinction allows a block to contain fewer occupied elements than its physical capacity.
270
+
271
+ ## API
272
+
273
+ The fundamental lifecycle of a block is:
274
+
275
+ ```text
276
+ alloc()
277
+ │
278
+ ├── acesso / modificação
279
+ │
280
+ ├── realloc()
281
+ │
282
+ └── free()
283
+ │
284
+ └── recycle
285
+
286
+ ```
287
+
288
+ Balloc provides several data-view models for a block:
289
+
290
+ * Stack
291
+
292
+
293
+ * Queue
294
+
295
+
296
+ * Array / Random Access
297
+
298
+
299
+ The complete API documentation is available in the [`./docs`](./docs) directory and is available translated into several languages:
300
+
301
+ | Idioma | Link |
302
+ |---|---|
303
+ | Arabic | [./docs/API — balloc.ar.md](./docs/API%20—%20balloc.ar.md) |
304
+ | Chinese | [./docs/API — balloc.zh.md](./docs/API%20—%20balloc.zh.md) |
305
+ | Spanish | [./docs/API — balloc.es.md](./docs/API%20—%20balloc.es.md) |
306
+ | English | [./docs/API — balloc.en.md](./docs/API%20—%20balloc.en.md) |
307
+ | Japanese | [./docs/API — balloc.ja.md](./docs/API%20—%20balloc.ja.md) |
308
+ | Portuguese | [./docs/API — balloc.pt.md](./docs/API%20—%20balloc.pt.md) |
309
+
310
+
311
+ The API methods are grouped by affinity into the following categories:
312
+
313
+ * Lifecycle
314
+ * Stack operations
315
+ * Queue operations
316
+ * Array operations
317
+ * Block interchange
318
+ * Serialization
319
+ * Diagnostics
320
+ * Internal functions
321
+
322
+ The following is a summary of these groups:
323
+
324
+ ### Lifecycle
325
+
326
+ | API | Description |
327
+ | --- | --- |
328
+ | `alloc()` | Allocate a block |
329
+ | `realloc()` | Increase block capacity |
330
+ | `free()` | Free a block |
331
+ | `gc()` | Compact the data heap |
332
+
333
+ ### Stack operations
334
+
335
+ | API | Description |
336
+ | --- | --- |
337
+ | `push()` | Add an element |
338
+ | `pop()` | Remove the last element |
339
+ | `peek()` | Read the last element |
340
+
341
+ ### Queue operations
342
+
343
+ | API | Description |
344
+ | --- | --- |
345
+ | `nudge()` | Insert at the beginning |
346
+ | `pull()` | Remove the first element |
347
+ | `head()` | Read the first element |
348
+
349
+ ### Array operations
350
+
351
+ | API | Description |
352
+ | --- | --- |
353
+ | `has()` | Search for a value |
354
+ | `get()` | Read an element |
355
+ | `set()` | Write an element |
356
+ | `clear()` | Clear occupancy and fill the block |
357
+ | `fill()` | Fill a range |
358
+ | `fillAll()` | Fill and occupy the entire block |
359
+ | `put()` | Copy an array into a block |
360
+ | `pick()` | Copy occupied elements out of a block |
361
+
362
+ ### Block interchange
363
+
364
+ | API | Description |
365
+ | --- | --- |
366
+ | `copy()` | Copy between blocks |
367
+ | `copyWithin()` | Copy within a block |
368
+ | `append()` | Append one block to another |
369
+ | `clone()` | Create an independent copy of the block |
370
+ | `swap()` | Swap block regions |
371
+
372
+ ### Serialization
373
+
374
+ | API | Description |
375
+ | --- | --- |
376
+ | `dump()` | Serialize the block contents as CBOR |
377
+ | `load()` | Restore a block from serialized data |
378
+
379
+ ### Diagnostics
380
+
381
+ | API | Description |
382
+ | --- | --- |
383
+ | `byteSize()` | Physical block size in bytes |
384
+ | `occupied()` | Number of occupied elements |
385
+ | `length()` | Physical element capacity |
386
+ | `recycleCount()` | Number of recyclable regions |
387
+ | `recycleBytes` | Currently recyclable bytes |
388
+ | `allocatedBytes` | Bytes associated with non-recycled regions |
389
+ | `unallocatedBytes` | Space remaining after the bump pointer |
390
+ | `fragmentationRatio` | Recycled bytes / used heap area |
391
+
392
+ ### Internal functions
393
+
394
+ These are a special class of API primarily used internally by the allocator and generally not recommended for day-to-day code, as they may cause undocumented side effects. They are intended for library extensions and modifications where a thorough understanding of the Balloc source code is essential.
395
+
396
+ ```text
397
+ Recirculation
398
+ ├── recyclePush()
399
+ └── recyclePop()
400
+
401
+ Growth
402
+ ├── grow()
403
+ └── headerGrow()
404
+
405
+ Validation / metadata
406
+ ├── extractMetadata()
407
+ └── bakeCookie()
408
+
409
+ ```
410
+
411
+ `recyclePush()` inserts a free block into the recycling list.
412
+
413
+ `recyclePop()` removes a free block from the recycling list and returns it.
414
+
415
+ `grow()` expands `dataBus` when the current region can no longer satisfy additional allocations.
416
+
417
+ `headerGrow()` expands `controlBus` when additional headers are required.
418
+
419
+ `extractMetadata()` decodes and validates a handle and extracts its associated metadata.
420
+
421
+ `bakeCookie()` calculates a validation cookie used by the handle validation mechanism.
422
+
423
+ ## 💖 Donations / Support
424
+
425
+ If you like this project and want to support its development:
426
+
427
+ | Pix QR Code | Copy-and-Paste / Random Key |
428
+ | :---: | :--- |
429
+ | ![Pix QR Code](./img/pix.png) | **Random Key:**<br>`e87d773a-0b97-4410-8fc3-f7ec04cce6a2` |
430
+
431
+ ## License
432
+
433
+ [MIT](./LICENSE)