balloc 1.4.0 → 2.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.MD +157 -334
  3. package/docs/API /342/200/224 balloc.en.md" +478 -1217
  4. package/docs/MIGRATION /342/200/224 balloc.en.md" +208 -0
  5. package/package.json +2 -44
  6. package/src/README.MD +0 -0
  7. package/src/en/balloc.cjs +510 -960
  8. package/src/en/balloc.js +1 -1
  9. package/src/en/balloc.mjs +551 -1099
  10. package/types/README.MD +0 -0
  11. package/types/en/balloc.d.mts +275 -583
  12. package/README.ar.md +0 -439
  13. package/README.en.md +0 -467
  14. package/README.es.md +0 -439
  15. package/README.jp.md +0 -438
  16. package/README.pt.md +0 -462
  17. package/README.zh.md +0 -438
  18. package/docs/API /342/200/224 balloc.ar.md" +0 -1509
  19. package/docs/API /342/200/224 balloc.es.md" +0 -1325
  20. package/docs/API /342/200/224 balloc.jp.md" +0 -1264
  21. package/docs/API /342/200/224 balloc.pt.md" +0 -1395
  22. package/docs/API /342/200/224 balloc.zh.md" +0 -1324
  23. package/src/ar/balloc.cjs +0 -1303
  24. package/src/ar/balloc.js +0 -2
  25. package/src/ar/balloc.mjs +0 -1591
  26. package/src/es/balloc.cjs +0 -1320
  27. package/src/es/balloc.js +0 -2
  28. package/src/es/balloc.mjs +0 -1608
  29. package/src/jp/balloc.cjs +0 -1260
  30. package/src/jp/balloc.js +0 -2
  31. package/src/jp/balloc.mjs +0 -1548
  32. package/src/pt/balloc.cjs +0 -1326
  33. package/src/pt/balloc.js +0 -2
  34. package/src/pt/balloc.mjs +0 -1619
  35. package/src/zh/balloc.cjs +0 -1205
  36. package/src/zh/balloc.js +0 -2
  37. package/src/zh/balloc.mjs +0 -1493
  38. package/types/ar/balloc.d.mts +0 -793
  39. package/types/es/balloc.d.mts +0 -810
  40. package/types/jp/balloc.d.mts +0 -750
  41. package/types/pt/balloc.d.mts +0 -818
  42. package/types/zh/balloc.d.mts +0 -695
package/README.en.md DELETED
@@ -1,467 +0,0 @@
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.jp.md](./docs/API%20—%20balloc.jp.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
- * Data ingestion adapters
315
- * Stack operations
316
- * Queue operations
317
- * Array operations
318
- * Block interchange
319
- * Serialization
320
- * Diagnostics
321
- * Internal functions
322
-
323
- The following is a summary of these groups:
324
-
325
- ### Data ingestion adapters
326
-
327
- `loadData(adapter)` delegates synchronous data ingestion to an adapter implementing the `FormatAdapter` contract. The adapter receives the allocator and returns the numeric handler for the primary block it loads.
328
-
329
- ```ts
330
- interface FormatAdapter {
331
- load(allocator: Balloc): number;
332
- }
333
- ```
334
-
335
- The allocator does not manage adapter transactions. If an adapter allocates multiple blocks and a later operation fails, rollback of those allocations is the adapter's responsibility.
336
-
337
- Example:
338
-
339
- ```js
340
- import Balloc from "balloc";
341
-
342
- const allocator = new Balloc(65535, "8u");
343
- const handler = allocator.loadData({
344
- load(allocator) {
345
- const handler = allocator.alloc(4);
346
- allocator.put(handler, new Uint8Array([255, 0, 0, 255]));
347
- return handler;
348
- }
349
- });
350
- ```
351
-
352
- Format-specific adapters can be published independently. For example, `imagedata-adapter` ingests RGBA8 `ImageData` instances.
353
-
354
- ### Lifecycle
355
-
356
- | API | Description |
357
- | --- | --- |
358
- | `alloc()` | Allocate a block |
359
- | `realloc()` | Increase block capacity |
360
- | `shrink()` | Reduce block capacity to fit occupied elements |
361
- | `free()` | Free a block |
362
- | `gc()` | Compact the data heap |
363
-
364
- ### Stack operations
365
-
366
- | API | Description |
367
- | --- | --- |
368
- | `push()` | Add an element |
369
- | `pop()` | Remove the last element |
370
- | `peek()` | Read the last element |
371
-
372
- ### Queue operations
373
-
374
- | API | Description |
375
- | --- | --- |
376
- | `nudge()` | Insert at the beginning |
377
- | `pull()` | Remove the first element |
378
- | `head()` | Read the first element |
379
-
380
- ### Array operations
381
-
382
- | API | Description |
383
- | --- | --- |
384
- | `has()` | Search for a value |
385
- | `indexOf()` | Search for a value (first match) |
386
- | `lastIndexOf()` | Search for a value (last match) |
387
- | `get()` | Read an element |
388
- | `set()` | Write an element |
389
- | `clear()` | Clear occupancy and fill the block |
390
- | `fill()` | Fill a range |
391
- | `fillAll()` | Fill and occupy the entire block |
392
- | `put()` | Copy an array into a block |
393
- | `pick()` | Copy occupied elements out of a block |
394
- | `withView()` | Borrow typed views over occupied elements |
395
-
396
- ### Block interchange
397
-
398
- | API | Description |
399
- | --- | --- |
400
- | `copy()` | Copy between blocks |
401
- | `copyWithin()` | Copy within a block |
402
- | `append()` | Append one block to another |
403
- | `clone()` | Create an independent copy of the block |
404
- | `swap()` | Swap block regions |
405
-
406
- ### Serialization
407
-
408
- | API | Description |
409
- | --- | --- |
410
- | `dump()` | Serialize the block contents as CBOR |
411
- | `load()` | Restore a block from serialized data |
412
-
413
- ### Diagnostics
414
-
415
- | API | Description |
416
- | --- | --- |
417
- | `byteSize()` | Physical block size in bytes |
418
- | `occupied()` | Number of occupied elements |
419
- | `length()` | Physical element capacity |
420
- | `isFull()` | Check whether the block is completely occupied |
421
- | `recycleCount()` | Number of recyclable regions |
422
- | `recycleBytes` | Currently recyclable bytes |
423
- | `allocatedBytes` | Bytes associated with non-recycled regions |
424
- | `unallocatedBytes` | Space remaining after the bump pointer |
425
- | `fragmentationRatio` | Recycled bytes / used heap area |
426
- ### Internal functions
427
-
428
- 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.
429
-
430
- ```text
431
- Recirculation
432
- ├── recyclePush()
433
- └── recyclePop()
434
-
435
- Growth
436
- ├── grow()
437
- └── headerGrow()
438
-
439
- Validation / metadata
440
- ├── extractMetadata()
441
- └── bakeCookie()
442
-
443
- ```
444
-
445
- `recyclePush()` inserts a free block into the recycling list.
446
-
447
- `recyclePop()` removes a free block from the recycling list and returns it.
448
-
449
- `grow()` expands `dataBus` when the current region can no longer satisfy additional allocations.
450
-
451
- `headerGrow()` expands `controlBus` when additional headers are required.
452
-
453
- `extractMetadata()` decodes and validates a handle and extracts its associated metadata.
454
-
455
- `bakeCookie()` calculates a validation cookie used by the handle validation mechanism.
456
-
457
- ## 💖 Donations / Support
458
-
459
- If you like this project and want to support its development:
460
-
461
- | Pix QR Code | Copy-and-Paste / Random Key |
462
- | :---: | :--- |
463
- | ![Pix QR Code](https://raw.githubusercontent.com/AugustoVasques75/balloc/main/packages/balloc/img/pix.png) | **Random Key:**<br>`e87d773a-0b97-4410-8fc3-f7ec04cce6a2` |
464
-
465
- ## License
466
-
467
- [MIT](./LICENSE)