balloc 1.0.1 → 1.3.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 +29 -0
- package/README.MD +22 -14
- package/README.ar.md +7 -3
- package/README.en.md +7 -3
- package/README.es.md +7 -3
- package/README.jp.md +7 -3
- package/README.pt.md +6 -6
- package/README.zh.md +7 -3
- package/docs/API /342/200/224 balloc.ar.md" +113 -25
- package/docs/API /342/200/224 balloc.en.md" +153 -15
- package/docs/API /342/200/224 balloc.es.md" +100 -11
- package/docs/API /342/200/224 balloc.jp.md" +137 -16
- package/docs/API /342/200/224 balloc.pt.md" +150 -25
- package/docs/API /342/200/224 balloc.zh.md" +108 -8
- package/package.json +22 -8
- package/src/ar/balloc.cjs +502 -306
- package/src/ar/balloc.js +1 -1
- package/src/ar/balloc.mjs +617 -392
- package/src/en/balloc.cjs +428 -218
- package/src/en/balloc.js +1 -1
- package/src/en/balloc.mjs +542 -303
- package/src/es/balloc.cjs +409 -230
- package/src/es/balloc.js +1 -1
- package/src/es/balloc.mjs +540 -333
- package/src/jp/balloc.cjs +437 -274
- package/src/jp/balloc.js +1 -1
- package/src/jp/balloc.mjs +551 -359
- package/src/pt/balloc.cjs +238 -58
- package/src/pt/balloc.js +1 -1
- package/src/pt/balloc.mjs +353 -144
- package/src/zh/balloc.cjs +358 -194
- package/src/zh/balloc.js +1 -1
- package/src/zh/balloc.mjs +475 -283
- package/types/ar/balloc.d.mts +388 -277
- package/types/en/balloc.d.mts +329 -204
- package/types/es/balloc.d.mts +302 -208
- package/types/jp/balloc.d.mts +330 -252
- package/types/pt/balloc.d.mts +138 -43
- package/types/zh/balloc.d.mts +262 -183
- package/img/pix.png +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
您也可以获取其他语言版本的文档:
|
|
2
2
|
| 语言 | 链接 |
|
|
3
3
|
|---|---|
|
|
4
|
-
| 阿拉伯语 | [API — balloc.ar.md](./
|
|
5
|
-
| 中文 | [API — balloc.zh.md](./
|
|
6
|
-
| 西班牙语 | [API — balloc.es.md](./
|
|
7
|
-
| 英语 | [API — balloc.en.md](./
|
|
8
|
-
| 日语 | [API — balloc.jp.md](./
|
|
9
|
-
| 葡萄牙语 | [API — balloc.pt.md](./
|
|
4
|
+
| 阿拉伯语 | [API — balloc.ar.md](./API%20—%20balloc.ar.md) |
|
|
5
|
+
| 中文 | [API — balloc.zh.md](./API%20—%20balloc.zh.md) |
|
|
6
|
+
| 西班牙语 | [API — balloc.es.md](./API%20—%20balloc.es.md) |
|
|
7
|
+
| 英语 | [API — balloc.en.md](./API%20—%20balloc.en.md) |
|
|
8
|
+
| 日语 | [API — balloc.jp.md](./API%20—%20balloc.jp.md) |
|
|
9
|
+
| 葡萄牙语 | [API — balloc.pt.md](./API%20—%20balloc.pt.md) |
|
|
10
10
|
|
|
11
11
|
# Balloc.js
|
|
12
12
|
|
|
@@ -23,11 +23,14 @@
|
|
|
23
23
|
Balloc
|
|
24
24
|
├── 实例化
|
|
25
25
|
│ └── constructor()
|
|
26
|
+
│
|
|
26
27
|
├── 数据块生命周期
|
|
27
28
|
│ ├── alloc()
|
|
28
29
|
│ ├── realloc()
|
|
30
|
+
│ ├── shrink()
|
|
29
31
|
│ ├── free()
|
|
30
32
|
│ └── gc()
|
|
33
|
+
│
|
|
31
34
|
├── 线性结构
|
|
32
35
|
│ ├── Stack
|
|
33
36
|
│ │ ├── push()
|
|
@@ -37,6 +40,7 @@ Balloc
|
|
|
37
40
|
│ ├── nudge()
|
|
38
41
|
│ ├── pull()
|
|
39
42
|
│ └── head()
|
|
43
|
+
│
|
|
40
44
|
├── 内容访问
|
|
41
45
|
│ ├── has()
|
|
42
46
|
│ ├── get()
|
|
@@ -45,26 +49,35 @@ Balloc
|
|
|
45
49
|
│ ├── fill()
|
|
46
50
|
│ ├── fillAll()
|
|
47
51
|
│ ├── put()
|
|
48
|
-
│
|
|
52
|
+
│ ├── pick()
|
|
53
|
+
│ ├── indexOf()
|
|
54
|
+
│ ├── lastIndexOf()
|
|
55
|
+
│ └── withView()
|
|
56
|
+
│
|
|
49
57
|
├── 容量与占用量
|
|
50
58
|
│ ├── byteSize()
|
|
51
59
|
│ ├── occupied()
|
|
52
|
-
│
|
|
60
|
+
│ ├── length()
|
|
61
|
+
│ └── isFull()
|
|
62
|
+
│
|
|
53
63
|
├── 数据块间操作
|
|
54
64
|
│ ├── copy()
|
|
55
65
|
│ ├── copyWithin()
|
|
56
66
|
│ ├── append()
|
|
57
67
|
│ ├── clone()
|
|
58
68
|
│ └── swap()
|
|
69
|
+
│
|
|
59
70
|
├── 序列化
|
|
60
71
|
│ ├── dump()
|
|
61
72
|
│ └── load()
|
|
73
|
+
│
|
|
62
74
|
├── 诊断与使用情况
|
|
63
75
|
│ ├── recycleCount()
|
|
64
76
|
│ ├── recycleBytes
|
|
65
77
|
│ ├── allocatedBytes
|
|
66
78
|
│ ├── unallocatedBytes
|
|
67
79
|
│ └── fragmentationRatio
|
|
80
|
+
│
|
|
68
81
|
└── 内部使用
|
|
69
82
|
├── 回收
|
|
70
83
|
│ ├── recyclePush()
|
|
@@ -111,6 +124,45 @@ byteSize(handler) → 实际预留的物理字节数
|
|
|
111
124
|
|
|
112
125
|
---
|
|
113
126
|
|
|
127
|
+
## 内存借用
|
|
128
|
+
|
|
129
|
+
Balloc 支持对一个或多个数据块的已占用数据进行有作用域限制的零拷贝访问。回调函数接收直接映射到内部存储的类型化数组视图,因此无需分配副本即可读取和写入。
|
|
130
|
+
|
|
131
|
+
借用仅在同步回调执行期间有效。回调运行时,Balloc 会锁定被借用的数据块,阻止可能使视图失效或改变数据块结构的操作。即使回调抛出异常,锁也会在 `finally` 块中释放。
|
|
132
|
+
|
|
133
|
+
### 单个数据块
|
|
134
|
+
|
|
135
|
+
传入一个 handler,后跟回调函数。回调接收的视图仅包含已占用元素,不包括未使用的容量。
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
const count = memory.withView(handler, view => {
|
|
139
|
+
view[0] = 42;
|
|
140
|
+
return view.length;
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
通过视图进行的写入会立即影响数据块,`withView()` 返回回调函数的返回值。
|
|
145
|
+
|
|
146
|
+
### 多个数据块
|
|
147
|
+
|
|
148
|
+
在回调函数之前传入两个或更多 handler。回调按相同顺序接收对应的视图:
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
memory.withView(source, target, (sourceView, targetView) => {
|
|
152
|
+
targetView.set(sourceView.subarray(0, targetView.length));
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
每个 handler 都必须有效、彼此不同且当前未被借用。Balloc 会在调用回调前获取所有锁,并在回调结束时释放。
|
|
157
|
+
|
|
158
|
+
### 借用约束
|
|
159
|
+
|
|
160
|
+
回调结束后,不得保留、返回或继续使用借用视图及其派生子视图。如果回调返回 Promise,借用会立即结束;因此所有视图访问都必须同步完成。
|
|
161
|
+
|
|
162
|
+
数据块处于借用状态时,修改该数据块的操作会以 `Error` 拒绝,包括 `push()`、`pop()`、`nudge()`、`pull()`、`set()`、`clear()`、`fill()`、`fillAll()`、`put()`、`realloc()`、`shrink()`、`free()` 和 `copyWithin()`。如果被借用的数据块是目标块,则拒绝 `copy()` 和 `append()`;任一数据块被借用时,拒绝 `swap()`。只读操作仍可用,且 `copy()` 和 `append()` 允许源数据块处于借用状态。
|
|
163
|
+
|
|
164
|
+
只要存在活动视图,就会拒绝替换或重新映射 `dataBus` 的操作,包括 `gc()` 以及由 `alloc()` 或 `realloc()` 触发的扩容。
|
|
165
|
+
|
|
114
166
|
# 1. 实例化
|
|
115
167
|
|
|
116
168
|
## `new Balloc(size, bucketSize, metadataSize)`
|
|
@@ -259,6 +311,30 @@ memory.realloc(handler);
|
|
|
259
311
|
|
|
260
312
|
---
|
|
261
313
|
|
|
314
|
+
## `shrink(handler)`
|
|
315
|
+
|
|
316
|
+
将数据块的物理容量缩小到能够容纳当前已占用元素的最小受支持的 2 的幂。最小容量为两个元素。如果当前容量已经足够,则不执行任何操作。
|
|
317
|
+
|
|
318
|
+
数据仍保留在原地址,且由于 header 保持不变,handler 仍然有效;只有容量字段会改变。如果数据块位于 `dataBus` 顶部,`dataPointer` 向后移动,释放空间直接归还给堆。否则,释放的尾部会被拆分为若干个 2 的幂大小的区域,并加入回收列表。
|
|
319
|
+
|
|
320
|
+
`shrink()` 不会改变占用量,也不会移动元素。
|
|
321
|
+
|
|
322
|
+
### 参数
|
|
323
|
+
|
|
324
|
+
| 参数 | 类型 | 说明 |
|
|
325
|
+
|---|---|---|
|
|
326
|
+
| `handler` | `number` | 数据块句柄。 |
|
|
327
|
+
|
|
328
|
+
### 返回值
|
|
329
|
+
|
|
330
|
+
`void`
|
|
331
|
+
|
|
332
|
+
### 异常
|
|
333
|
+
|
|
334
|
+
`Error` — handler 无效或已损坏,或者数据块正通过 `withView()` 处于借用状态。
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
262
338
|
## `free(handler)`
|
|
263
339
|
|
|
264
340
|
释放数据块。
|
|
@@ -454,6 +530,20 @@ boolean
|
|
|
454
530
|
|
|
455
531
|
---
|
|
456
532
|
|
|
533
|
+
## `indexOf(handler, searchElement, fromIndex = 0)`
|
|
534
|
+
|
|
535
|
+
按索引升序查找值的首次出现位置。搜索范围仅限已占用元素,不包括未使用的物理容量。
|
|
536
|
+
|
|
537
|
+
## `lastIndexOf(handler, searchElement, fromIndex = -1)`
|
|
538
|
+
|
|
539
|
+
按索引降序查找值的最后出现位置。搜索范围仅限已占用元素,不包括未使用的物理容量。
|
|
540
|
+
|
|
541
|
+
## `withView(...args)`
|
|
542
|
+
|
|
543
|
+
在同步回调期间借用一个或多个数据块。传入一个或多个 handler,最后传入回调;回调按相同顺序接收视图。`...args` 的类型为 `number[] + callback`。返回值为回调函数的返回值。缺少 handler 或最后一个参数不是函数时抛出 `TypeError`;handler 无效、重复或已被借用时抛出 `Error`。
|
|
544
|
+
|
|
545
|
+
---
|
|
546
|
+
|
|
457
547
|
## `get(handler, index)`
|
|
458
548
|
|
|
459
549
|
获取指定索引处的元素。
|
|
@@ -638,6 +728,11 @@ const count = memory.occupied(handler);
|
|
|
638
728
|
|
|
639
729
|
## `length(handler)`
|
|
640
730
|
|
|
731
|
+
## `isFull(handler)`
|
|
732
|
+
|
|
733
|
+
检查数据块是否已完全占用. 当已占用元素数量等于数据块的物理容量时返回 `true`,否则返回 `false`。
|
|
734
|
+
|
|
735
|
+
|
|
641
736
|
返回可用物理槽位的总数。
|
|
642
737
|
|
|
643
738
|
容量与占用量相互独立:
|
|
@@ -1123,6 +1218,7 @@ generation 参与 cookie 校验。因此,如果句柄无效或其 header 已
|
|
|
1123
1218
|
| 实例化 | `constructor()` | `Balloc` |
|
|
1124
1219
|
| 生命周期 | `alloc()` | `number` |
|
|
1125
1220
|
| 生命周期 | `realloc()` | `void` |
|
|
1221
|
+
| 生命周期 | `shrink()` | `void` |
|
|
1126
1222
|
| 生命周期 | `free()` | `void` |
|
|
1127
1223
|
| 生命周期 | `gc()` | `void` |
|
|
1128
1224
|
| Stack | `push()` | `boolean` |
|
|
@@ -1132,6 +1228,9 @@ generation 参与 cookie 校验。因此,如果句柄无效或其 header 已
|
|
|
1132
1228
|
| Queue | `pull()` | `number \| undefined` |
|
|
1133
1229
|
| Queue | `head()` | `number \| undefined` |
|
|
1134
1230
|
| 访问 | `has()` | `boolean` |
|
|
1231
|
+
| 访问 | `indexOf()` | `number` |
|
|
1232
|
+
| 访问 | `lastIndexOf()` | `number` |
|
|
1233
|
+
| 访问 | `withView()` | 回调函数的返回值 |
|
|
1135
1234
|
| 访问 | `get()` | `number \| undefined` |
|
|
1136
1235
|
| 访问 | `set()` | `boolean` |
|
|
1137
1236
|
| 访问 | `clear()` | `void` |
|
|
@@ -1142,6 +1241,7 @@ generation 参与 cookie 校验。因此,如果句柄无效或其 header 已
|
|
|
1142
1241
|
| 容量 | `byteSize()` | `number` |
|
|
1143
1242
|
| 容量 | `occupied()` | `number` |
|
|
1144
1243
|
| 容量 | `length()` | `number` |
|
|
1244
|
+
| 容量 | `isFull()` | `boolean` |
|
|
1145
1245
|
| 数据块 | `copy()` | `Uint8Array` |
|
|
1146
1246
|
| 数据块 | `copyWithin()` | `Uint8Array` |
|
|
1147
1247
|
| 数据块 | `append()` | `number` |
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "balloc",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"homepage": "https://github.com/AugustoVasques75/balloc",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/AugustoVasques75/balloc.git"
|
|
8
|
+
},
|
|
5
9
|
"description": "Bump allocation memory allocator",
|
|
6
10
|
"main": "./src/en/balloc.mjs",
|
|
7
11
|
"unpkg": "./src/en/balloc.js",
|
|
@@ -10,11 +14,17 @@
|
|
|
10
14
|
"type": "module",
|
|
11
15
|
"files": [
|
|
12
16
|
"docs/",
|
|
13
|
-
"img/",
|
|
14
17
|
"src/",
|
|
15
18
|
"types/",
|
|
16
19
|
"LICENSE",
|
|
17
|
-
"README.
|
|
20
|
+
"README.MD",
|
|
21
|
+
"README.ar.md",
|
|
22
|
+
"README.en.md",
|
|
23
|
+
"README.es.md",
|
|
24
|
+
"README.pt.md",
|
|
25
|
+
"README.jp.md",
|
|
26
|
+
"README.zh.md",
|
|
27
|
+
"CHANGELOG.md"
|
|
18
28
|
],
|
|
19
29
|
"exports": {
|
|
20
30
|
".": {
|
|
@@ -66,13 +76,15 @@
|
|
|
66
76
|
"build": "npm run build:bundle && npm run build:types",
|
|
67
77
|
"clear:images": "docker rmi $(docker images \"balloc-test\" -q)",
|
|
68
78
|
"pack:local": "npm pack --pack-destination ./dist",
|
|
69
|
-
"test": "
|
|
70
|
-
"test:
|
|
79
|
+
"test": "vitest run",
|
|
80
|
+
"test:watch": "vitest"
|
|
71
81
|
},
|
|
72
82
|
"keywords": [
|
|
73
83
|
"memory",
|
|
74
84
|
"CBOR",
|
|
75
85
|
"allocator",
|
|
86
|
+
"arena",
|
|
87
|
+
"array-arena",
|
|
76
88
|
"bump-allocation",
|
|
77
89
|
"bump-allocator",
|
|
78
90
|
"low-level",
|
|
@@ -84,14 +96,16 @@
|
|
|
84
96
|
"cross-platform",
|
|
85
97
|
"endianness",
|
|
86
98
|
"malloc",
|
|
87
|
-
"data-structures"
|
|
99
|
+
"data-structures",
|
|
100
|
+
"SoA",
|
|
101
|
+
"AoS"
|
|
88
102
|
],
|
|
89
103
|
"author": "Augusto Vasques",
|
|
90
104
|
"license": "MIT",
|
|
91
105
|
"devDependencies": {
|
|
92
|
-
"jest": "^30.5.2",
|
|
93
106
|
"tsup": "^8.5.1",
|
|
94
|
-
"typescript": "^7.0.2"
|
|
107
|
+
"typescript": "^7.0.2",
|
|
108
|
+
"vitest": "^4.1.12"
|
|
95
109
|
},
|
|
96
110
|
"overrides": {
|
|
97
111
|
"esbuild": "^0.28.1",
|