balloc 1.3.1 → 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.
- package/CHANGELOG.md +44 -0
- package/README.MD +171 -318
- package/docs/API /342/200/224 balloc.en.md" +472 -1156
- package/docs/MIGRATION /342/200/224 balloc.en.md" +208 -0
- package/package.json +2 -44
- package/src/README.MD +0 -0
- package/src/en/balloc.cjs +515 -933
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +561 -1064
- package/types/README.MD +0 -0
- package/types/en/balloc.d.mts +279 -554
- package/README.ar.md +0 -409
- package/README.en.md +0 -437
- package/README.es.md +0 -409
- package/README.jp.md +0 -408
- package/README.pt.md +0 -433
- package/README.zh.md +0 -408
- package/docs/API /342/200/224 balloc.ar.md" +0 -1453
- package/docs/API /342/200/224 balloc.es.md" +0 -1269
- package/docs/API /342/200/224 balloc.jp.md" +0 -1208
- package/docs/API /342/200/224 balloc.pt.md" +0 -1339
- package/docs/API /342/200/224 balloc.zh.md" +0 -1268
- package/src/ar/balloc.cjs +0 -1273
- package/src/ar/balloc.js +0 -2
- package/src/ar/balloc.mjs +0 -1548
- package/src/es/balloc.cjs +0 -1288
- package/src/es/balloc.js +0 -2
- package/src/es/balloc.mjs +0 -1563
- package/src/jp/balloc.cjs +0 -1228
- package/src/jp/balloc.js +0 -2
- package/src/jp/balloc.mjs +0 -1503
- package/src/pt/balloc.cjs +0 -1293
- package/src/pt/balloc.js +0 -2
- package/src/pt/balloc.mjs +0 -1568
- package/src/zh/balloc.cjs +0 -1175
- package/src/zh/balloc.js +0 -2
- package/src/zh/balloc.mjs +0 -1450
- package/types/ar/balloc.d.mts +0 -762
- package/types/es/balloc.d.mts +0 -777
- package/types/jp/balloc.d.mts +0 -717
- package/types/pt/balloc.d.mts +0 -781
- package/types/zh/balloc.d.mts +0 -664
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,50 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [2.0.0] - 2026-11-29
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- Independent memory blocks backed by their own `ArrayBuffer` instances.
|
|
12
|
+
- Numeric block handles managed by an allocator-local registry.
|
|
13
|
+
- Per-block capacity and occupancy tracking.
|
|
14
|
+
- `byteSize()` for querying a block's physical size in bytes.
|
|
15
|
+
- `fillAll()` for filling a block and setting its occupancy to full capacity.
|
|
16
|
+
- `shrink()` for reducing a block's capacity to fit its occupied elements.
|
|
17
|
+
- `loadData(adapter)` for synchronous ingestion through the `FormatAdapter` contract.
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
- Replaced the shared-heap architecture with independently allocated typed-array blocks.
|
|
21
|
+
- Changed the constructor to `new Balloc(maxByteSize, defaultBucketType)`. The first argument is now the maximum permitted size of an individual allocation; it no longer preallocates a shared heap.
|
|
22
|
+
- Changed `alloc(size, bucketType)` to interpret `size` in bytes. The requested size must be aligned to the selected element width and remain below `maxByteSize`; capacity is no longer rounded to a power of two.
|
|
23
|
+
- Kept explicit `realloc(handler, newSize)` sizes in elements. When omitted, the new capacity is twice the current element capacity, subject to the allocation limit.
|
|
24
|
+
- Changed `fill()` to preserve the greater of the previous occupancy and the requested range end. Use `fillAll()` when the entire block must become occupied.
|
|
25
|
+
- Changed `copy()` to return the destination block's typed-array view rather than a `Uint8Array` view over a shared heap.
|
|
26
|
+
- Defined `clone()` to copy the complete typed array, including unoccupied slots, while preserving occupancy and bucket type.
|
|
27
|
+
- Changed `swap()` to exchange the block objects associated with the supplied handles.
|
|
28
|
+
- Reworked `dump()` and `load()` around the version 2 CBOR envelope, which records bucket type, capacity, occupancy, and the occupied typed-array payload.
|
|
29
|
+
|
|
30
|
+
### Removed
|
|
31
|
+
- Shared `dataBus` and `controlBus` storage, block headers, and heap-pointer-based allocation.
|
|
32
|
+
- Shared-heap bump allocation, region recycling, and compaction.
|
|
33
|
+
- `gc()`, `recycleCount()`, `recycleBytes`, `allocatedBytes`, `unallocatedBytes`, and `fragmentationRatio`.
|
|
34
|
+
- Public reliance on internal heap, pointer, and header-management structures.
|
|
35
|
+
|
|
36
|
+
### Breaking changes
|
|
37
|
+
- Version 2.0.0 is not backward-compatible with 1.x and is not a drop-in replacement.
|
|
38
|
+
- Applications must convert `alloc()` arguments from element counts to byte sizes and review constructor arguments, capacity assumptions, and block lifecycle handling.
|
|
39
|
+
- Applications that use removed heap-management APIs or internal storage structures must be adapted to the block-oriented API.
|
|
40
|
+
- Version 1.x serialized data is not directly compatible with the version 2 CBOR format. Decode legacy data with the corresponding 1.x release and re-encode it using 2.0.0.
|
|
41
|
+
- See the [migration guide](docs/MIGRATION%20%E2%80%94%20balloc.en.md) for the required porting steps.
|
|
42
|
+
|
|
43
|
+
## [1.4.0] - 2026-11-23
|
|
44
|
+
|
|
45
|
+
### Added
|
|
46
|
+
- Added the `loadData(adapter)` method and the `FormatAdapter` ingestion contract.
|
|
47
|
+
|
|
48
|
+
### Changed
|
|
49
|
+
- Refactored `withView()`.
|
|
50
|
+
- Deprecated `assertNotBorrowed()`.
|
|
51
|
+
|
|
8
52
|
## [1.3.1] - 2026-11-19
|
|
9
53
|
|
|
10
54
|
### Added
|
package/README.MD
CHANGED
|
@@ -1,440 +1,293 @@
|
|
|
1
|
-
|
|
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*.
|
|
1
|
+
# Balloc
|
|
30
2
|
|
|
31
|
-
|
|
3
|
+
**Balloc** is a typed memory-block allocator for JavaScript. It stores each allocation in its own `ArrayBuffer`, exposes blocks through numeric handles, and tracks the number of occupied elements separately from physical capacity.
|
|
32
4
|
|
|
33
|
-
|
|
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
|
|
5
|
+
Version: **2.0.0**
|
|
45
6
|
|
|
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.
|
|
7
|
+
## Version 2.0.0: breaking change from 1.x
|
|
51
8
|
|
|
52
|
-
|
|
9
|
+
**Balloc 2.0.0 is an architectural redesign and is not backward-compatible with the 1.x series. It is not a drop-in upgrade.** Applications written for 1.x must be reviewed and adapted before upgrading.
|
|
53
10
|
|
|
54
|
-
|
|
55
|
-
Balloc
|
|
56
|
-
│
|
|
57
|
-
┌─────────┴─────────┐
|
|
58
|
-
│ │
|
|
59
|
-
dataBus controlBus
|
|
60
|
-
│ │
|
|
61
|
-
conteúdo headers
|
|
11
|
+
The memory-management model has changed fundamentally:
|
|
62
12
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
13
|
+
| Version 1.x | Version 2.0.0 |
|
|
14
|
+
|---|---|
|
|
15
|
+
| Shared heap managed by the allocator. | Each allocation owns an independent `ArrayBuffer`. |
|
|
16
|
+
| Bump allocation and region-oriented memory management. | Independent blocks addressed by numeric handles. |
|
|
17
|
+
| Allocation metadata and heap-oriented operations. | Per-block typed-array capacity and logical occupancy. |
|
|
18
|
+
| Region recycling and heap compaction/garbage-collection workflows. | Explicit block lifecycle through `alloc()`, `realloc()`, `shrink()`, and `free()`. |
|
|
66
19
|
|
|
20
|
+
The public programming model has changed along with the storage architecture. Do not assume that 1.x methods, internal structures, handles, allocation assumptions, or existing application code map directly to the 2.0.0 API. In particular, code coupled to the former shared heap, metadata, allocation regions, recycling, or compaction must be redesigned around independent blocks and the 2.0.0 public API.
|
|
67
21
|
|
|
68
|
-
|
|
22
|
+
**Upgrade guidance:** treat the move from 1.x to 2.0.0 as a major migration. Review all Balloc integration points and adapt allocation, access, lifetime, and serialization logic to the 2.0.0 contracts before deploying. A package-version change alone is not a sufficient migration.
|
|
69
23
|
|
|
70
|
-
|
|
24
|
+
## Migration from 1.x
|
|
71
25
|
|
|
72
|
-
|
|
73
|
-
┌───────────────────────┬───────────────────────────────┐
|
|
74
|
-
│ generation — 11 bits │ header address — 32 bits │
|
|
75
|
-
└───────────────────────┴───────────────────────────────┘
|
|
26
|
+
See the [Balloc 1.x to 2.0.0 migration guide](./docs/MIGRATION%20%E2%80%94%20balloc.en.md) before upgrading an existing application.
|
|
76
27
|
|
|
77
|
-
|
|
28
|
+
## Features
|
|
78
29
|
|
|
79
|
-
|
|
30
|
+
- Typed storage backed by JavaScript `TypedArray` instances.
|
|
31
|
+
- Numeric, monotonically increasing block handles. Handles are not reused after a block is freed.
|
|
32
|
+
- Separate physical capacity and logical occupancy.
|
|
33
|
+
- Stack, queue, and indexed access operations.
|
|
34
|
+
- Explicit block resizing with `realloc()` and `shrink()`.
|
|
35
|
+
- Scoped, zero-copy access through `withView()`.
|
|
36
|
+
- Block copying, appending, cloning, and swapping.
|
|
37
|
+
- CBOR serialization and restoration.
|
|
38
|
+
- Synchronous data-ingestion adapters through `loadData()`.
|
|
80
39
|
|
|
81
|
-
|
|
40
|
+
Each block owns an independent `ArrayBuffer`. Balloc does not use a shared bump-allocation heap, region recycling, or a heap-compaction routine.
|
|
82
41
|
|
|
83
42
|
## Installation
|
|
84
43
|
|
|
85
|
-
|
|
44
|
+
Node.js:
|
|
86
45
|
|
|
87
46
|
```bash
|
|
88
|
-
npm install
|
|
89
|
-
|
|
47
|
+
npm install balloc
|
|
90
48
|
```
|
|
91
49
|
|
|
92
|
-
|
|
50
|
+
Bun:
|
|
93
51
|
|
|
94
52
|
```bash
|
|
95
|
-
bun add
|
|
96
|
-
|
|
53
|
+
bun add balloc
|
|
97
54
|
```
|
|
98
55
|
|
|
99
|
-
|
|
56
|
+
Deno:
|
|
100
57
|
|
|
101
58
|
```bash
|
|
102
|
-
deno add npm:
|
|
103
|
-
|
|
59
|
+
deno add npm:balloc
|
|
104
60
|
```
|
|
105
61
|
|
|
106
|
-
|
|
62
|
+
Yarn:
|
|
107
63
|
|
|
108
64
|
```bash
|
|
109
|
-
yarn add
|
|
110
|
-
|
|
65
|
+
yarn add balloc
|
|
111
66
|
```
|
|
112
67
|
|
|
113
|
-
|
|
68
|
+
## Loading
|
|
114
69
|
|
|
115
|
-
|
|
116
|
-
[https://unpkg.com/Balloc](https://unpkg.com/Balloc)
|
|
70
|
+
### ES modules
|
|
117
71
|
|
|
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)
|
|
72
|
+
```js
|
|
73
|
+
import Balloc from "balloc";
|
|
128
74
|
|
|
75
|
+
const memory = new Balloc();
|
|
129
76
|
```
|
|
130
77
|
|
|
131
|
-
|
|
78
|
+
### CommonJS
|
|
132
79
|
|
|
133
|
-
```
|
|
134
|
-
|
|
80
|
+
```js
|
|
81
|
+
const { Balloc } = require("balloc");
|
|
135
82
|
|
|
83
|
+
const memory = new Balloc();
|
|
136
84
|
```
|
|
137
85
|
|
|
138
|
-
|
|
86
|
+
### Browser
|
|
139
87
|
|
|
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)
|
|
88
|
+
The package provides a browser bundle through the package CDN entry:
|
|
146
89
|
|
|
90
|
+
```html
|
|
91
|
+
<script src="https://unpkg.com/balloc"></script>
|
|
147
92
|
```
|
|
148
93
|
|
|
94
|
+
The bundle exposes the allocator as `window.Balloc`.
|
|
95
|
+
|
|
149
96
|
## Basic usage
|
|
150
97
|
|
|
151
|
-
|
|
98
|
+
The constructor accepts a maximum allocation size in bytes and an optional default element type.
|
|
152
99
|
|
|
153
100
|
```js
|
|
154
|
-
import Balloc from "
|
|
101
|
+
import Balloc from "balloc";
|
|
155
102
|
|
|
156
103
|
const memory = new Balloc();
|
|
157
|
-
|
|
158
|
-
const handler = memory.alloc(100);
|
|
104
|
+
const handler = memory.alloc(64, "32u");
|
|
159
105
|
|
|
160
106
|
memory.push(handler, 10);
|
|
161
107
|
memory.push(handler, 20);
|
|
162
108
|
memory.push(handler, 30);
|
|
163
109
|
|
|
164
|
-
console.log(memory.
|
|
165
|
-
console.log(memory.
|
|
166
|
-
|
|
167
|
-
console.log(memory.pop(handler));
|
|
168
|
-
console.log(memory.pop(handler));
|
|
110
|
+
console.log(memory.occupied(handler)); // 3
|
|
111
|
+
console.log(memory.length(handler)); // 16
|
|
112
|
+
console.log(memory.pick(handler)); // Uint32Array [10, 20, 30]
|
|
169
113
|
|
|
170
114
|
memory.free(handler);
|
|
171
|
-
|
|
172
115
|
```
|
|
173
116
|
|
|
174
|
-
|
|
117
|
+
The `alloc()` size is expressed in **bytes**. The resulting typed array contains as many elements as fit in that buffer. The default allocation size is 16 bytes and the default element type is `32u`.
|
|
118
|
+
|
|
119
|
+
## Constructor
|
|
175
120
|
|
|
176
121
|
```js
|
|
177
|
-
|
|
122
|
+
new Balloc(maxByteSize, bucketSize)
|
|
123
|
+
```
|
|
178
124
|
|
|
179
|
-
|
|
125
|
+
| Parameter | Type | Default | Description |
|
|
126
|
+
|---|---|---:|---|
|
|
127
|
+
| `maxByteSize` | `number` | `0x80000000` | Upper bound used to validate allocation sizes, in bytes. |
|
|
128
|
+
| `bucketSize` | `BucketType | null` | `null` | Optional default element type. When set, it overrides the type passed to `alloc()`. |
|
|
180
129
|
|
|
181
|
-
|
|
130
|
+
`maxByteSize` must be a safe integer in the range `[2, 0x80000000]`.
|
|
182
131
|
|
|
183
|
-
|
|
184
|
-
memory.push(handler, 20);
|
|
185
|
-
memory.push(handler, 30);
|
|
186
|
-
|
|
187
|
-
console.log(memory.length(handler));
|
|
188
|
-
console.log(memory.occupied(handler));
|
|
132
|
+
## Element types
|
|
189
133
|
|
|
190
|
-
|
|
191
|
-
console.log(memory.pop(handler));
|
|
134
|
+
The element type uses the format `[bit width][type]`:
|
|
192
135
|
|
|
193
|
-
|
|
136
|
+
| Signed integer | Unsigned integer | Floating point |
|
|
137
|
+
|---|---|---|
|
|
138
|
+
| `8i` | `8u` | — |
|
|
139
|
+
| `16i` | `16u` | `16f` |
|
|
140
|
+
| `32i` | `32u` | `32f` |
|
|
141
|
+
| `64i` | `64u` | `64f` |
|
|
194
142
|
|
|
195
|
-
|
|
143
|
+
These identifiers map to the corresponding JavaScript typed arrays: `Int8Array`, `Uint8Array`, `Int16Array`, `Uint16Array`, `Int32Array`, `Uint32Array`, `BigInt64Array`, `BigUint64Array`, `Float16Array`, `Float32Array`, and `Float64Array`.
|
|
196
144
|
|
|
197
|
-
|
|
145
|
+
The runtime must provide the corresponding typed-array constructor for the selected type. The 64-bit integer types use `bigint` values.
|
|
198
146
|
|
|
199
|
-
|
|
147
|
+
## Allocation and capacity
|
|
200
148
|
|
|
201
149
|
```js
|
|
202
|
-
|
|
203
|
-
|
|
150
|
+
const handler = memory.alloc(size, bucketType);
|
|
204
151
|
```
|
|
205
152
|
|
|
206
|
-
|
|
|
207
|
-
|
|
208
|
-
| `size` | `number` | `
|
|
209
|
-
| `
|
|
210
|
-
| `metadataSize` | `number` | `1024` | Initial `controlBus` capacity in bytes |
|
|
153
|
+
| Argument | Type | Default | Description |
|
|
154
|
+
|---|---|---:|---|
|
|
155
|
+
| `size` | `number` | `16` | Allocation size in bytes. Must be aligned to the selected element width. |
|
|
156
|
+
| `bucketType` | `BucketType` | `"32u"` | Element type used by the block, unless the constructor defines a default type. |
|
|
211
157
|
|
|
212
|
-
|
|
158
|
+
Each successful allocation receives a new numeric handle. Handles increase monotonically and are not reused after `free()`.
|
|
213
159
|
|
|
214
|
-
|
|
215
|
-
const memory = new Balloc();
|
|
160
|
+
Balloc distinguishes physical capacity from logical occupancy:
|
|
216
161
|
|
|
217
|
-
|
|
162
|
+
| Method | Meaning |
|
|
163
|
+
|---|---|
|
|
164
|
+
| `length(handler)` | Physical capacity in elements. |
|
|
165
|
+
| `occupied(handler)` | Number of occupied elements. |
|
|
166
|
+
| `byteSize(handler)` | Physical capacity in bytes. |
|
|
167
|
+
| `isFull(handler)` | Whether occupancy equals physical capacity. |
|
|
218
168
|
|
|
219
|
-
|
|
169
|
+
An allocation can have unused capacity. Operations that add elements update occupancy; reading or replacing an existing element does not.
|
|
220
170
|
|
|
221
|
-
|
|
222
|
-
const memory = new Balloc(65535, "32u");
|
|
171
|
+
### Resizing and release
|
|
223
172
|
|
|
224
|
-
|
|
173
|
+
| Method | Description |
|
|
174
|
+
|---|---|
|
|
175
|
+
| `realloc(handler, newSize)` | Replaces the block storage with a larger buffer. `newSize` is expressed in elements; when omitted, capacity doubles. |
|
|
176
|
+
| `shrink(handler)` | Replaces the buffer with the smallest power-of-two capacity that can hold the occupied elements, with a minimum of two elements. If that capacity is not smaller than the current capacity, no change is made. |
|
|
177
|
+
| `free(handler)` | Removes the block and invalidates its handle. |
|
|
225
178
|
|
|
226
|
-
|
|
179
|
+
Resizing copies the occupied elements to the new buffer. A block cannot be resized or freed while it is borrowed by `withView()`.
|
|
227
180
|
|
|
228
|
-
|
|
181
|
+
## Data operations
|
|
229
182
|
|
|
230
|
-
|
|
231
|
-
| --- | --- | --- |
|
|
232
|
-
| `8i` | `8u` | |
|
|
233
|
-
| `16i` | `16u` | |
|
|
234
|
-
| `32i` | `32u` | `32f` |
|
|
235
|
-
| `64i` | `64u` | `64f` |
|
|
183
|
+
### Stack
|
|
236
184
|
|
|
237
|
-
|
|
185
|
+
| Method | Description |
|
|
186
|
+
|---|---|
|
|
187
|
+
| `push(handler, value)` | Adds an element at the end. Returns `false` if the block is full. |
|
|
188
|
+
| `pop(handler)` | Removes and returns the last occupied element. Returns `undefined` when empty. |
|
|
189
|
+
| `peek(handler)` | Reads the last occupied element without removing it. |
|
|
238
190
|
|
|
239
|
-
|
|
191
|
+
### Queue
|
|
240
192
|
|
|
241
|
-
|
|
193
|
+
| Method | Description |
|
|
194
|
+
|---|---|
|
|
195
|
+
| `nudge(handler, value)` | Inserts an element at the beginning, shifting occupied elements toward the end. Returns `false` if full. |
|
|
196
|
+
| `pull(handler)` | Removes and returns the first element, shifting the remaining occupied elements toward the beginning. |
|
|
197
|
+
| `head(handler)` | Reads the first slot without changing occupancy. |
|
|
242
198
|
|
|
243
|
-
|
|
199
|
+
### Indexed access and search
|
|
244
200
|
|
|
245
|
-
|
|
246
|
-
|
|
201
|
+
| Method | Description |
|
|
202
|
+
|---|---|
|
|
203
|
+
| `get(handler, index)` | Reads an occupied element. Returns `undefined` for an index outside the occupied range. |
|
|
204
|
+
| `set(handler, index, value)` | Replaces an occupied element. Returns `false` when the index is outside the occupied range. |
|
|
205
|
+
| `has(handler, value)` | Checks whether a value occurs in the occupied range. |
|
|
206
|
+
| `indexOf(handler, value, fromIndex)` | Finds the first occurrence. |
|
|
207
|
+
| `lastIndexOf(handler, value, fromIndex)` | Finds the last occurrence. |
|
|
247
208
|
|
|
248
|
-
|
|
209
|
+
### Bulk operations
|
|
249
210
|
|
|
250
|
-
|
|
211
|
+
| Method | Description |
|
|
212
|
+
|---|---|
|
|
213
|
+
| `clear(handler, value = 0)` | Fills the entire block and resets occupancy to zero. |
|
|
214
|
+
| `fill(handler, value, start, end)` | Fills the specified range and updates occupancy to include its end. |
|
|
215
|
+
| `fillAll(handler, value)` | Fills every slot and marks the entire block as occupied. |
|
|
216
|
+
| `put(handler, data)` | Copies an `Array` or `TypedArray` to the beginning of the block, up to its capacity. |
|
|
217
|
+
| `pick(handler)` | Returns a copy of the occupied elements as a typed array. |
|
|
251
218
|
|
|
252
|
-
|
|
219
|
+
### Operations between blocks
|
|
253
220
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
capacity
|
|
221
|
+
| Method | Description |
|
|
222
|
+
|---|---|
|
|
223
|
+
| `copy(source, destination, slots)` | Copies occupied elements from one block to another, up to the destination capacity and optional slot limit. |
|
|
224
|
+
| `copyWithin(handler, target, start, length)` | Copies a range within the same block, supporting overlapping regions. |
|
|
225
|
+
| `append(target, source)` | Appends as many occupied source elements as fit in the target block. Returns the number appended. |
|
|
226
|
+
| `clone(handler)` | Creates an independent block containing a copy of the source block. |
|
|
227
|
+
| `swap(source, target)` | Swaps the block objects associated with two handles without changing the handles. |
|
|
257
228
|
|
|
258
|
-
```
|
|
259
229
|
|
|
260
|
-
|
|
230
|
+
### Data ingestion
|
|
261
231
|
|
|
262
|
-
|
|
263
|
-
length(handler) → physical capacity
|
|
264
|
-
occupied(handler) → occupied elements
|
|
265
|
-
byteSize(handler) → reserved physical bytes
|
|
232
|
+
`loadData(adapter)` delegates synchronous ingestion to an adapter with a `load(allocator)` method. The adapter receives the current Balloc instance, performs its allocations through the public API, and its return value is passed through unchanged.
|
|
266
233
|
|
|
234
|
+
```js
|
|
235
|
+
const handler = memory.loadData({
|
|
236
|
+
load(allocator) {
|
|
237
|
+
const block = allocator.alloc(4, "8u");
|
|
238
|
+
allocator.put(block, new Uint8Array([255, 0, 0, 255]));
|
|
239
|
+
return block;
|
|
240
|
+
}
|
|
241
|
+
});
|
|
267
242
|
```
|
|
268
243
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
## API
|
|
244
|
+
Balloc does not manage adapter transactions. If an adapter creates multiple blocks and a later operation fails, cleanup of those allocations is the adapter's responsibility.
|
|
272
245
|
|
|
273
|
-
|
|
246
|
+
### Memory views
|
|
274
247
|
|
|
275
|
-
|
|
276
|
-
alloc()
|
|
277
|
-
│
|
|
278
|
-
├── acesso / modificação
|
|
279
|
-
│
|
|
280
|
-
├── realloc()
|
|
281
|
-
├── shrink()
|
|
282
|
-
│
|
|
283
|
-
└── free()
|
|
284
|
-
│
|
|
285
|
-
└── recycle
|
|
248
|
+
`withView()` provides synchronous, zero-copy access to the occupied elements of one or more blocks. The callback receives typed-array subviews that share the blocks' storage.
|
|
286
249
|
|
|
250
|
+
```js
|
|
251
|
+
memory.withView(handler, view => {
|
|
252
|
+
view[0] = 42;
|
|
253
|
+
view[1] = 84;
|
|
254
|
+
});
|
|
287
255
|
```
|
|
288
256
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
* Stack
|
|
257
|
+
Multiple blocks can be borrowed in one call. The callback receives one view per handle, in argument order:
|
|
292
258
|
|
|
259
|
+
```js
|
|
260
|
+
memory.withView(source, target, (sourceView, targetView) => {
|
|
261
|
+
targetView.set(sourceView.subarray(0, targetView.length));
|
|
262
|
+
});
|
|
263
|
+
```
|
|
293
264
|
|
|
294
|
-
|
|
265
|
+
Borrowing is scoped to the synchronous callback. Balloc releases the borrow in a `finally` block, including when the callback throws. Do not retain, return, or use a borrowed view after the callback finishes. An asynchronous callback does not extend the borrow until its promise settles.
|
|
295
266
|
|
|
267
|
+
Operations that could invalidate or structurally modify a borrowed block are rejected while the callback is running.
|
|
296
268
|
|
|
297
|
-
|
|
269
|
+
## Serialization
|
|
298
270
|
|
|
271
|
+
`dump(handler)` serializes a block into a CBOR version 2 envelope. The envelope stores the bucket type, capacity, occupancy, and occupied payload.
|
|
299
272
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
| Idioma | Link |
|
|
303
|
-
|---|---|
|
|
304
|
-
| Arabic | [./docs/API — balloc.ar.md](./docs/API%20—%20balloc.ar.md) |
|
|
305
|
-
| Chinese | [./docs/API — balloc.zh.md](./docs/API%20—%20balloc.zh.md) |
|
|
306
|
-
| Spanish | [./docs/API — balloc.es.md](./docs/API%20—%20balloc.es.md) |
|
|
307
|
-
| English | [./docs/API — balloc.en.md](./docs/API%20—%20balloc.en.md) |
|
|
308
|
-
| Japanese | [./docs/API — balloc.jp.md](./docs/API%20—%20balloc.jp.md) |
|
|
309
|
-
| Portuguese | [./docs/API — balloc.pt.md](./docs/API%20—%20balloc.pt.md) |
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
The API methods are grouped by affinity into the following categories:
|
|
313
|
-
|
|
314
|
-
* Lifecycle
|
|
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
|
-
### Lifecycle
|
|
326
|
-
|
|
327
|
-
| API | Description |
|
|
328
|
-
| --- | --- |
|
|
329
|
-
| `alloc()` | Allocate a block |
|
|
330
|
-
| `realloc()` | Increase block capacity |
|
|
331
|
-
| `shrink()` | Reduce block capacity to fit occupied elements |
|
|
332
|
-
| `free()` | Free a block |
|
|
333
|
-
| `gc()` | Compact the data heap |
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
### Stack operations
|
|
337
|
-
|
|
338
|
-
| API | Description |
|
|
339
|
-
| --- | --- |
|
|
340
|
-
| `push()` | Add an element |
|
|
341
|
-
| `pop()` | Remove the last element |
|
|
342
|
-
| `peek()` | Read the last element |
|
|
343
|
-
|
|
344
|
-
### Queue operations
|
|
345
|
-
|
|
346
|
-
| API | Description |
|
|
347
|
-
| --- | --- |
|
|
348
|
-
| `nudge()` | Insert at the beginning |
|
|
349
|
-
| `pull()` | Remove the first element |
|
|
350
|
-
| `head()` | Read the first element |
|
|
351
|
-
|
|
352
|
-
### Array operations
|
|
353
|
-
|
|
354
|
-
| API | Description |
|
|
355
|
-
| --- | --- |
|
|
356
|
-
| `has()` | Search for a value |
|
|
357
|
-
| `indexOf()` | Search for a value (first match) |
|
|
358
|
-
| `lastIndexOf()` | Search for a value (last match) |
|
|
359
|
-
| `get()` | Read an element |
|
|
360
|
-
| `set()` | Write an element |
|
|
361
|
-
| `clear()` | Clear occupancy and fill the block |
|
|
362
|
-
| `fill()` | Fill a range |
|
|
363
|
-
| `fillAll()` | Fill and occupy the entire block |
|
|
364
|
-
| `put()` | Copy an array into a block |
|
|
365
|
-
| `pick()` | Copy occupied elements out of a block |
|
|
366
|
-
| `withView()` | Borrow a typed view over occupied elements |
|
|
367
|
-
|
|
368
|
-
### Block interchange
|
|
369
|
-
|
|
370
|
-
| API | Description |
|
|
371
|
-
| --- | --- |
|
|
372
|
-
| `copy()` | Copy between blocks |
|
|
373
|
-
| `copyWithin()` | Copy within a block |
|
|
374
|
-
| `append()` | Append one block to another |
|
|
375
|
-
| `clone()` | Create an independent copy of the block |
|
|
376
|
-
| `swap()` | Swap block regions |
|
|
377
|
-
|
|
378
|
-
### Serialization
|
|
379
|
-
|
|
380
|
-
| API | Description |
|
|
381
|
-
| --- | --- |
|
|
382
|
-
| `dump()` | Serialize the block contents as CBOR |
|
|
383
|
-
| `load()` | Restore a block from serialized data |
|
|
384
|
-
|
|
385
|
-
### Diagnostics
|
|
386
|
-
|
|
387
|
-
| API | Description |
|
|
388
|
-
| --- | --- |
|
|
389
|
-
| `byteSize()` | Physical block size in bytes |
|
|
390
|
-
| `occupied()` | Number of occupied elements |
|
|
391
|
-
| `length()` | Physical element capacity |
|
|
392
|
-
| `isFull()` | Check whether the block is completely occupied |
|
|
393
|
-
| `recycleCount()` | Number of recyclable regions |
|
|
394
|
-
| `recycleBytes` | Currently recyclable bytes |
|
|
395
|
-
| `allocatedBytes` | Bytes associated with non-recycled regions |
|
|
396
|
-
| `unallocatedBytes` | Space remaining after the bump pointer |
|
|
397
|
-
| `fragmentationRatio` | Recycled bytes / used heap area |
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
### Internal functions
|
|
401
|
-
|
|
402
|
-
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.
|
|
403
|
-
|
|
404
|
-
```text
|
|
405
|
-
Recirculation
|
|
406
|
-
├── recyclePush()
|
|
407
|
-
└── recyclePop()
|
|
408
|
-
|
|
409
|
-
Growth
|
|
410
|
-
├── grow()
|
|
411
|
-
└── headerGrow()
|
|
412
|
-
|
|
413
|
-
Validation / metadata
|
|
414
|
-
├── extractMetadata()
|
|
415
|
-
└── bakeCookie()
|
|
273
|
+
`load(serialized)` accepts an `ArrayBuffer` or `Uint8Array`, validates the envelope, and creates a new block. If the allocator was constructed with a fixed `bucketSize`, the serialized block must use the same type.
|
|
416
274
|
|
|
275
|
+
```js
|
|
276
|
+
const encoded = memory.dump(handler);
|
|
277
|
+
const restoredHandler = memory.load(encoded);
|
|
417
278
|
```
|
|
418
279
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
`recyclePop()` removes a free block from the recycling list and returns it.
|
|
422
|
-
|
|
423
|
-
`grow()` expands `dataBus` when the current region can no longer satisfy additional allocations.
|
|
424
|
-
|
|
425
|
-
`headerGrow()` expands `controlBus` when additional headers are required.
|
|
426
|
-
|
|
427
|
-
`extractMetadata()` decodes and validates a handle and extracts its associated metadata.
|
|
280
|
+
## API reference
|
|
428
281
|
|
|
429
|
-
|
|
282
|
+
The TypeScript declarations are available at [`types/en/balloc.d.mts`](./types/en/balloc.d.mts). The implementation is available at [`src/en/balloc.mjs`](./src/en/balloc.mjs).
|
|
430
283
|
|
|
431
|
-
##
|
|
284
|
+
## Donations / Support
|
|
432
285
|
|
|
433
286
|
If you like this project and want to support its development:
|
|
434
287
|
|
|
435
288
|
| Pix QR Code | Copy-and-Paste / Random Key |
|
|
436
289
|
| :---: | :--- |
|
|
437
|
-
|  | **Random Key
|
|
290
|
+
|  | **Random Key:** `e87d773a-0b97-4410-8fc3-f7ec04cce6a2` |
|
|
438
291
|
|
|
439
292
|
## License
|
|
440
293
|
|