nish 0.44.0 → 0.46.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.md +1 -1
- package/docs/AI.md +18 -16
- package/docs/INSTALL.md +3 -1
- package/package.json +5 -5
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1315 -222
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +171 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/collections.ts
CHANGED
|
@@ -25,10 +25,12 @@
|
|
|
25
25
|
* full 32-bit hash of each. The bucket table `slots` is open addressing with
|
|
26
26
|
* linear probing, and a bucket is one `u32`:
|
|
27
27
|
*
|
|
28
|
-
* bits
|
|
29
|
-
* bits
|
|
28
|
+
* the high bits the key's hash above the table's mask (the fingerprint)
|
|
29
|
+
* the mask bits the entry's index plus one
|
|
30
30
|
*
|
|
31
|
-
*
|
|
31
|
+
* The index field is as wide as the mask, `slots.length - 1`: a table holds
|
|
32
|
+
* at most three entries per four buckets, so every index plus one fits. An
|
|
33
|
+
* empty bucket is 0. A deleted entry's bucket is the mask plus one,
|
|
32
34
|
* fingerprint 1 and index field 0, which no live entry has; its stored hash is
|
|
33
35
|
* set to 0, which is why a computed hash of 0 is moved to 1. A probe reads the
|
|
34
36
|
* entry arrays only when a bucket's fingerprint matches, and compares the
|
|
@@ -53,16 +55,6 @@
|
|
|
53
55
|
* outside it.
|
|
54
56
|
*/
|
|
55
57
|
|
|
56
|
-
/**
|
|
57
|
-
* The most entries a table holds, dead ones included until a rebuild: 2^24 - 1,
|
|
58
|
-
* what the 24-bit index field holds as an index plus one. Node's own `Map` and
|
|
59
|
-
* `Set` hold one more, 2^24, and throw on the next insert.
|
|
60
|
-
*/
|
|
61
|
-
const INDEX_CAP: i32 = 16777215
|
|
62
|
-
|
|
63
|
-
/** The first bucket count. A power of two, as every bucket count is. */
|
|
64
|
-
const INITIAL_SLOTS: i32 = 8
|
|
65
|
-
|
|
66
58
|
/**
|
|
67
59
|
* The key's hash, never 0: SipHash-1-3 of a string's bytes under the process's
|
|
68
60
|
* random key, folded to 32 bits (docs/security/json.md, JSON-13); under a 2nd,
|
|
@@ -89,35 +81,54 @@ const sameKey = <K>(a: K, b: K): boolean => a === b
|
|
|
89
81
|
/** The first bucket for `h`: its low bits, folded with the high half. */
|
|
90
82
|
const homeBucket = (h: u32, mask: i32): i32 => toI32(h ^ (h >>> 16)) & mask
|
|
91
83
|
|
|
92
|
-
/**
|
|
93
|
-
|
|
84
|
+
/**
|
|
85
|
+
* The bucket word for entry `index` of hash `h` in a table of `mask + 1`
|
|
86
|
+
* buckets: the index plus one in the bits the mask covers, and the hash's bits
|
|
87
|
+
* above them as a fingerprint. The load bound keeps every index below the
|
|
88
|
+
* bucket count, so the index never reaches the fingerprint.
|
|
89
|
+
*/
|
|
90
|
+
const slotWord = (h: u32, mask: i32, index: i32): u32 => (h & ~toU32(mask)) | (toU32(index) + 1)
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A deleted entry's bucket: a fingerprint bit set and an index field of 0, so
|
|
94
|
+
* a probe walks past it, its entry index being -1.
|
|
95
|
+
*/
|
|
96
|
+
const tombstone = (mask: i32): u32 => toU32(mask) + 1
|
|
94
97
|
|
|
95
98
|
/** A probe that found entry `index`, pointed at by `bucket`. Never negative. */
|
|
96
99
|
const foundAt = (bucket: i32, index: i32): i64 => (toI64(bucket) << 32) | toI64(index)
|
|
97
100
|
|
|
98
101
|
/** A probe that stopped at the empty `bucket` for hash `h`. Always negative. */
|
|
99
|
-
const absentAt = (bucket: i32, h: u32): i64 =>
|
|
102
|
+
const absentAt = (bucket: i32, h: u32): i64 => ~((toI64(bucket) << 32) | toI64(h))
|
|
100
103
|
|
|
101
104
|
/**
|
|
102
105
|
* The one probe, for both classes: linear probing from `h`'s home bucket to
|
|
103
106
|
* the bucket pointing at `key`, or to the first empty one. A tombstone is
|
|
104
107
|
* walked past — its index field is 0, so its entry index is -1 and the range
|
|
105
108
|
* test turns it away — and the load bound keeps a quarter of the buckets
|
|
106
|
-
* empty, so the walk ends.
|
|
109
|
+
* empty, so the walk ends. A bucket past the end, which a mask as wide as
|
|
110
|
+
* the table never gives, answers absent there, which `insertAt` answers with
|
|
111
|
+
* a rebuild.
|
|
107
112
|
*/
|
|
108
113
|
const probeTable = <K>(slots: u32[], mask: i32, hashes: u32[], keys: K[], key: K): i64 => {
|
|
109
114
|
const h = hashKey(key)
|
|
110
|
-
const
|
|
111
|
-
|
|
115
|
+
const low = toU32(mask)
|
|
116
|
+
const fingerprint = h & ~low
|
|
117
|
+
let bucket = toU32(homeBucket(h, mask))
|
|
112
118
|
// The length is read in the condition rather than once: the key compare is
|
|
113
|
-
// a call, and a call ends every length fact the bounds proof holds.
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
// a call, and a call ends every length fact the bounds proof holds. The
|
|
120
|
+
// bucket is a `u32`, whose `+` wraps, so the call leaves nothing it needs
|
|
121
|
+
// proven. The load bound keeps a quarter of the buckets empty, so the walk
|
|
122
|
+
// ends at one; the test on `b` keeps a table with none from indexing past
|
|
123
|
+
// its end.
|
|
124
|
+
let b = toI32(bucket)
|
|
125
|
+
while (b >= 0 && b < toI32(slots.length)) {
|
|
126
|
+
const word = slots[b]
|
|
116
127
|
if (word === 0) {
|
|
117
|
-
return absentAt(
|
|
128
|
+
return absentAt(b, h)
|
|
118
129
|
}
|
|
119
|
-
if (word
|
|
120
|
-
const at = toI32(word &
|
|
130
|
+
if ((word & ~low) === fingerprint) {
|
|
131
|
+
const at = toI32((word & low) - 1)
|
|
121
132
|
if (
|
|
122
133
|
at >= 0 &&
|
|
123
134
|
at < toI32(hashes.length) &&
|
|
@@ -125,28 +136,33 @@ const probeTable = <K>(slots: u32[], mask: i32, hashes: u32[], keys: K[], key: K
|
|
|
125
136
|
at < toI32(keys.length) &&
|
|
126
137
|
sameKey(keys[at], key)
|
|
127
138
|
) {
|
|
128
|
-
return foundAt(
|
|
139
|
+
return foundAt(b, at)
|
|
129
140
|
}
|
|
130
141
|
}
|
|
131
|
-
bucket = (bucket + 1) &
|
|
142
|
+
bucket = (bucket + 1) & low
|
|
143
|
+
b = toI32(bucket)
|
|
132
144
|
}
|
|
133
|
-
|
|
145
|
+
return absentAt(toI32(slots.length), h)
|
|
134
146
|
}
|
|
135
147
|
|
|
136
148
|
/**
|
|
137
149
|
* Point the first empty bucket from `h`'s home at entry `index`. A rebuild's
|
|
138
150
|
* re-filing: every key is already known to be distinct, so no key is
|
|
139
|
-
* compared, and the stored hash is all it needs.
|
|
151
|
+
* compared, and the stored hash is all it needs. The load bound leaves a
|
|
152
|
+
* quarter of the buckets empty, so one pass finds one.
|
|
140
153
|
*/
|
|
141
154
|
const fileEntry = (slots: u32[], mask: i32, h: u32, index: i32): void => {
|
|
142
|
-
const word = slotWord(h, index)
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
155
|
+
const word = slotWord(h, mask, index)
|
|
156
|
+
const low = toU32(mask)
|
|
157
|
+
let bucket = toU32(homeBucket(h, mask))
|
|
158
|
+
let b = toI32(bucket)
|
|
159
|
+
while (b >= 0 && b < toI32(slots.length)) {
|
|
160
|
+
if (slots[b] === 0) {
|
|
161
|
+
slots[b] = word
|
|
147
162
|
return
|
|
148
163
|
}
|
|
149
|
-
bucket = (bucket + 1) &
|
|
164
|
+
bucket = (bucket + 1) & low
|
|
165
|
+
b = toI32(bucket)
|
|
150
166
|
}
|
|
151
167
|
}
|
|
152
168
|
|
|
@@ -160,7 +176,7 @@ const compactEntries = <T>(items: T[], hashes: u32[]): void => {
|
|
|
160
176
|
to++
|
|
161
177
|
}
|
|
162
178
|
}
|
|
163
|
-
while (toI32(items.length) > to) {
|
|
179
|
+
while (toI32(items.length) > to && items.length > 0) {
|
|
164
180
|
items.pop()
|
|
165
181
|
}
|
|
166
182
|
}
|
|
@@ -176,25 +192,49 @@ const compactHashes = (hashes: u32[]): void => {
|
|
|
176
192
|
to++
|
|
177
193
|
}
|
|
178
194
|
}
|
|
179
|
-
while (toI32(hashes.length) > to) {
|
|
195
|
+
while (toI32(hashes.length) > to && hashes.length > 0) {
|
|
180
196
|
hashes.pop()
|
|
181
197
|
}
|
|
182
198
|
}
|
|
183
199
|
|
|
184
200
|
/**
|
|
185
|
-
* The bucket table after a rebuild of `live` entries out of `used
|
|
186
|
-
* half dead compacts at the same size and clears the table in place
|
|
187
|
-
* array would leave the old one in the arena, once per compaction,
|
|
188
|
-
* table that churns forever cannot afford (§6.1). Otherwise it
|
|
189
|
-
* the table never shrinks.
|
|
201
|
+
* The bucket table after a rebuild of `live` entries out of `used`, cleared.
|
|
202
|
+
* More than half dead compacts at the same size and clears the table in place
|
|
203
|
+
* — a new array would leave the old one in the arena, once per compaction,
|
|
204
|
+
* which a table that churns forever cannot afford (§6.1). Otherwise it
|
|
205
|
+
* doubles, and the table never shrinks.
|
|
190
206
|
*/
|
|
191
207
|
const rebuiltSlots = (slots: u32[], live: i32, used: i32): u32[] => {
|
|
192
|
-
|
|
193
|
-
if (live * 2 < used) {
|
|
208
|
+
if (toI64(live) * 2 < toI64(used)) {
|
|
194
209
|
clearSlots(slots)
|
|
195
210
|
return slots
|
|
196
211
|
}
|
|
197
|
-
|
|
212
|
+
const n = toI32(slots.length)
|
|
213
|
+
if (n >= 0 && n < 32768) {
|
|
214
|
+
// A `u16` length is in range by its type; a larger table grows in place.
|
|
215
|
+
return new Array<u32>(toU16(n * 2))
|
|
216
|
+
}
|
|
217
|
+
widenSlots(slots, 2)
|
|
218
|
+
clearSlots(slots)
|
|
219
|
+
return slots
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Grow `slots` in place to `times` buckets for each it holds; the caller
|
|
224
|
+
* clears or re-files them. Only a table past 2^15 buckets grows this way; a
|
|
225
|
+
* smaller one is allocated fresh, whose `u16` length needs no check. It grows by
|
|
226
|
+
* `push`, so a table past the largest array, 2^31 - 1 elements, stops the
|
|
227
|
+
* program as out of memory, as any allocation that size does: that is a `Map`
|
|
228
|
+
* or `Set` of more than 2^29 entries. (Node's hold 2^24 and throw on the next
|
|
229
|
+
* insert.)
|
|
230
|
+
*/
|
|
231
|
+
const widenSlots = (slots: u32[], times: i32): void => {
|
|
232
|
+
const n = toI32(slots.length)
|
|
233
|
+
for (let k: i32 = 1; k < times; k++) {
|
|
234
|
+
for (let i: i32 = 0; i < n; i++) {
|
|
235
|
+
slots.push(0)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
198
238
|
}
|
|
199
239
|
|
|
200
240
|
/**
|
|
@@ -217,11 +257,11 @@ const refile = (slots: u32[], hashes: u32[]): void => {
|
|
|
217
257
|
* tombstone and the entry's stored hash 0. The key and value stay in the entry
|
|
218
258
|
* until a rebuild compacts them away (§6.1).
|
|
219
259
|
*/
|
|
220
|
-
const killEntry = (slots: u32[], hashes: u32[], found: i64): void => {
|
|
260
|
+
const killEntry = (slots: u32[], mask: i32, hashes: u32[], found: i64): void => {
|
|
221
261
|
const at = toI32(found)
|
|
222
262
|
const bucket = toI32(found >> 32)
|
|
223
263
|
if (bucket >= 0 && bucket < toI32(slots.length)) {
|
|
224
|
-
slots[bucket] =
|
|
264
|
+
slots[bucket] = tombstone(mask)
|
|
225
265
|
}
|
|
226
266
|
if (at >= 0 && at < toI32(hashes.length)) {
|
|
227
267
|
hashes[at] = 0
|
|
@@ -264,15 +304,21 @@ const truncate = <T>(items: T[]): void => {
|
|
|
264
304
|
|
|
265
305
|
/**
|
|
266
306
|
* Point bucket `bucket`, the empty one an absent probe stopped at, at the entry
|
|
267
|
-
* just appended
|
|
268
|
-
*
|
|
307
|
+
* just appended to `hashes`, and answer true; or answer false, and file
|
|
308
|
+
* nothing, when the entry needs a rebuild first: the bucket is past the end,
|
|
309
|
+
* or the entries, dead ones included, are past the load bound of three
|
|
310
|
+
* quarters of the buckets.
|
|
269
311
|
*/
|
|
270
|
-
const fileAppended = (slots: u32[], mask: i32, bucket: i32,
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
312
|
+
const fileAppended = (slots: u32[], mask: i32, bucket: i32, hashes: u32[]): boolean => {
|
|
313
|
+
const used = toI32(hashes.length)
|
|
314
|
+
const size = toI32(slots.length)
|
|
315
|
+
if (toI64(used) * 4 > toI64(size) * 3 || bucket < 0 || bucket >= size) {
|
|
316
|
+
return false
|
|
275
317
|
}
|
|
318
|
+
if (used > 0) {
|
|
319
|
+
slots[bucket] = slotWord(hashes[used - 1], mask, used - 1)
|
|
320
|
+
}
|
|
321
|
+
return true
|
|
276
322
|
}
|
|
277
323
|
|
|
278
324
|
/**
|
|
@@ -299,11 +345,11 @@ export class Map<K, V> {
|
|
|
299
345
|
* a rebuild doubles rather than compacts, and `clear` marks entries dead
|
|
300
346
|
* rather than truncating, so no entry moves under a walk's cursor (§6.2).
|
|
301
347
|
*/
|
|
302
|
-
private walks:
|
|
348
|
+
private walks: u32 = 0
|
|
303
349
|
|
|
304
350
|
/** An empty map. */
|
|
305
351
|
constructor() {
|
|
306
|
-
this.slots = new Array<u32>(
|
|
352
|
+
this.slots = new Array<u32>(8)
|
|
307
353
|
this.entryKeys = []
|
|
308
354
|
this.entryValues = []
|
|
309
355
|
this.entryHashes = []
|
|
@@ -360,9 +406,8 @@ export class Map<K, V> {
|
|
|
360
406
|
if (found < 0) {
|
|
361
407
|
return false
|
|
362
408
|
}
|
|
363
|
-
killEntry(this.slots, this.entryHashes, found)
|
|
364
|
-
this.
|
|
365
|
-
this.size = this.size - 1
|
|
409
|
+
killEntry(this.slots, this.mask, this.entryHashes, found)
|
|
410
|
+
this.dropOne()
|
|
366
411
|
return true
|
|
367
412
|
}
|
|
368
413
|
|
|
@@ -394,7 +439,7 @@ export class Map<K, V> {
|
|
|
394
439
|
* src/emit-map.ts.
|
|
395
440
|
*/
|
|
396
441
|
walkOpen(): void {
|
|
397
|
-
this.walks = this.walks + 1
|
|
442
|
+
this.walks = this.walks + 1 // a `u32`: no program nests 2^32 walks
|
|
398
443
|
}
|
|
399
444
|
|
|
400
445
|
/** @internal Called by name by the code the compiler writes for a `Map`, src/emit-map.ts. */
|
|
@@ -413,11 +458,11 @@ export class Map<K, V> {
|
|
|
413
458
|
* @internal Called by name by the code the compiler writes for a `Map`,
|
|
414
459
|
* src/emit-map.ts.
|
|
415
460
|
*/
|
|
416
|
-
keyAt(index: i32): K {
|
|
417
|
-
if (index
|
|
418
|
-
|
|
461
|
+
keyAt(index: i32, none: K): K {
|
|
462
|
+
if (index >= 0 && index < toI32(this.entryKeys.length)) {
|
|
463
|
+
return this.entryKeys[index]
|
|
419
464
|
}
|
|
420
|
-
return
|
|
465
|
+
return none
|
|
421
466
|
}
|
|
422
467
|
|
|
423
468
|
/**
|
|
@@ -426,11 +471,11 @@ export class Map<K, V> {
|
|
|
426
471
|
* @internal Called by name by the code the compiler writes for a `Map`,
|
|
427
472
|
* src/emit-map.ts.
|
|
428
473
|
*/
|
|
429
|
-
valueAt(index: i32): V {
|
|
430
|
-
if (index
|
|
431
|
-
|
|
474
|
+
valueAt(index: i32, none: V): V {
|
|
475
|
+
if (index >= 0 && index < toI32(this.entryValues.length)) {
|
|
476
|
+
return this.entryValues[index]
|
|
432
477
|
}
|
|
433
|
-
return
|
|
478
|
+
return none
|
|
434
479
|
}
|
|
435
480
|
|
|
436
481
|
/**
|
|
@@ -454,29 +499,42 @@ export class Map<K, V> {
|
|
|
454
499
|
*/
|
|
455
500
|
insertAt(absent: i64, key: K, value: V): void {
|
|
456
501
|
const packed = -1 - absent
|
|
457
|
-
let bucket = toI32(packed >> 32)
|
|
458
|
-
const h = toU32(packed)
|
|
459
|
-
if (toI32(this.entryKeys.length) >= INDEX_CAP) {
|
|
460
|
-
// Dead entries hold the cap: compact them away, then find the bucket
|
|
461
|
-
// again. A walk defers compaction, so under one the cap is full.
|
|
462
|
-
if (this.live >= INDEX_CAP || this.walks > 0) {
|
|
463
|
-
panic("Map maximum size exceeded")
|
|
464
|
-
}
|
|
465
|
-
this.rebuild()
|
|
466
|
-
bucket = -1
|
|
467
|
-
}
|
|
468
502
|
this.entryKeys.push(storedKey(key))
|
|
469
503
|
this.entryValues.push(value)
|
|
470
|
-
this.entryHashes.push(
|
|
471
|
-
this.
|
|
472
|
-
this.size = this.size + 1
|
|
504
|
+
this.entryHashes.push(toU32(packed))
|
|
505
|
+
this.addOne()
|
|
473
506
|
// Every entry takes a bucket, live or dead, until a rebuild, which files
|
|
474
507
|
// the new entry with the rest; otherwise it takes the bucket the probe found.
|
|
475
|
-
|
|
476
|
-
if (used * 4 > toI32(this.slots.length) * 3) {
|
|
508
|
+
if (!fileAppended(this.slots, this.mask, toI32(packed >> 32), this.entryHashes)) {
|
|
477
509
|
this.rebuild()
|
|
478
|
-
}
|
|
479
|
-
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Count one more live entry. The entries, dead ones included, are fewer
|
|
515
|
+
* than 2^31 - 1, the most an array holds, so neither count is ever at the
|
|
516
|
+
* bound the test names.
|
|
517
|
+
*/
|
|
518
|
+
private addOne(): void {
|
|
519
|
+
const live = this.live
|
|
520
|
+
if (live < 2147483647) {
|
|
521
|
+
this.live = live + 1
|
|
522
|
+
}
|
|
523
|
+
const size = this.size
|
|
524
|
+
if (size < 2147483647) {
|
|
525
|
+
this.size = size + 1
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/** Count one live entry fewer: `delete`'s, which found one, so neither count is 0. */
|
|
530
|
+
private dropOne(): void {
|
|
531
|
+
const live = this.live
|
|
532
|
+
if (live > 0) {
|
|
533
|
+
this.live = live - 1
|
|
534
|
+
}
|
|
535
|
+
const size = this.size
|
|
536
|
+
if (size > 0) {
|
|
537
|
+
this.size = size - 1
|
|
480
538
|
}
|
|
481
539
|
}
|
|
482
540
|
|
|
@@ -506,28 +564,59 @@ export class Map<K, V> {
|
|
|
506
564
|
* and re-files them from the stored hashes, so no entry moves and it is as
|
|
507
565
|
* safe under a walk as the doubling a walk already allows (§6.2). A count
|
|
508
566
|
* that is not positive, or not a number, does nothing, as `reserve` does
|
|
509
|
-
* under Node
|
|
567
|
+
* under Node. The table grows to at most 2^30 buckets, the largest power of
|
|
568
|
+
* two an array holds; filling one that size runs out of memory first.
|
|
510
569
|
*
|
|
511
570
|
* @internal Called by name by the code the compiler writes for `reserve` from
|
|
512
571
|
* `nish/map`, src/emit-map.ts.
|
|
513
572
|
*/
|
|
514
573
|
reserveSlots(n: number): void {
|
|
515
|
-
|
|
574
|
+
const want = toF64(n)
|
|
575
|
+
if (!(want > 0)) {
|
|
516
576
|
return
|
|
517
577
|
}
|
|
518
|
-
|
|
519
|
-
const have = toI32(this.slots.length)
|
|
578
|
+
// In `f64`, which has no overflow to prove away.
|
|
579
|
+
const have = toF64(toI32(this.slots.length))
|
|
520
580
|
let size = have
|
|
521
|
-
while (want * 4 > size * 3) {
|
|
581
|
+
while (want * 4 > size * 3 && size < 1073741824) {
|
|
522
582
|
size = size * 2
|
|
523
583
|
}
|
|
524
|
-
if (size > have) {
|
|
525
|
-
const
|
|
526
|
-
|
|
527
|
-
|
|
584
|
+
if (size > have && have > 0) {
|
|
585
|
+
const wanted = toI32(size)
|
|
586
|
+
if (wanted > 0 && wanted <= 65535) {
|
|
587
|
+
const slots = new Array<u32>(toU16(wanted))
|
|
588
|
+
this.slots = slots
|
|
589
|
+
this.mask = wanted - 1
|
|
590
|
+
refile(slots, this.entryHashes)
|
|
591
|
+
return
|
|
592
|
+
}
|
|
593
|
+
const slots = this.slots
|
|
594
|
+
widenSlots(slots, toI32(size / have))
|
|
595
|
+
clearSlots(slots)
|
|
596
|
+
this.mask = toI32(slots.length) - 1
|
|
528
597
|
refile(slots, this.entryHashes)
|
|
529
598
|
}
|
|
530
599
|
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* The entry keys, dead ones included, for `forEachEntry`'s walk to index
|
|
603
|
+
* with what `walkNext` found. Read again after every call it makes, since
|
|
604
|
+
* `clear` replaces them.
|
|
605
|
+
*
|
|
606
|
+
* @internal Called by `forEachEntry`, `forEachValue` and `forEachElement`.
|
|
607
|
+
*/
|
|
608
|
+
walkKeys(): K[] {
|
|
609
|
+
return this.entryKeys
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* The entry values, as `walkKeys`.
|
|
614
|
+
*
|
|
615
|
+
* @internal Called by `forEachEntry` and `forEachValue`.
|
|
616
|
+
*/
|
|
617
|
+
walkValues(): V[] {
|
|
618
|
+
return this.entryValues
|
|
619
|
+
}
|
|
531
620
|
}
|
|
532
621
|
|
|
533
622
|
/** A set of keys in insertion order: `Map`'s table with no values, and the same probe. */
|
|
@@ -541,11 +630,11 @@ export class Set<T> {
|
|
|
541
630
|
private entryKeys: T[]
|
|
542
631
|
private entryHashes: u32[]
|
|
543
632
|
/** `Map`'s walk count: how many `for...of` loops are walking the table now. */
|
|
544
|
-
private walks:
|
|
633
|
+
private walks: u32 = 0
|
|
545
634
|
|
|
546
635
|
/** An empty set. */
|
|
547
636
|
constructor() {
|
|
548
|
-
this.slots = new Array<u32>(
|
|
637
|
+
this.slots = new Array<u32>(8)
|
|
549
638
|
this.entryKeys = []
|
|
550
639
|
this.entryHashes = []
|
|
551
640
|
}
|
|
@@ -591,9 +680,8 @@ export class Set<T> {
|
|
|
591
680
|
if (found < 0) {
|
|
592
681
|
return false
|
|
593
682
|
}
|
|
594
|
-
killEntry(this.slots, this.entryHashes, found)
|
|
595
|
-
this.
|
|
596
|
-
this.size = this.size - 1
|
|
683
|
+
killEntry(this.slots, this.mask, this.entryHashes, found)
|
|
684
|
+
this.dropOne()
|
|
597
685
|
return true
|
|
598
686
|
}
|
|
599
687
|
|
|
@@ -618,7 +706,7 @@ export class Set<T> {
|
|
|
618
706
|
* src/emit-map.ts.
|
|
619
707
|
*/
|
|
620
708
|
walkOpen(): void {
|
|
621
|
-
this.walks = this.walks + 1
|
|
709
|
+
this.walks = this.walks + 1 // a `u32`: no program nests 2^32 walks
|
|
622
710
|
}
|
|
623
711
|
|
|
624
712
|
/** @internal Called by name by the code the compiler writes for a `Set`, src/emit-map.ts. */
|
|
@@ -632,36 +720,47 @@ export class Set<T> {
|
|
|
632
720
|
}
|
|
633
721
|
|
|
634
722
|
/** @internal Called by name by the code the compiler writes for a `Set`, src/emit-map.ts. */
|
|
635
|
-
keyAt(index: i32): T {
|
|
636
|
-
if (index
|
|
637
|
-
|
|
723
|
+
keyAt(index: i32, none: T): T {
|
|
724
|
+
if (index >= 0 && index < toI32(this.entryKeys.length)) {
|
|
725
|
+
return this.entryKeys[index]
|
|
638
726
|
}
|
|
639
|
-
return
|
|
727
|
+
return none
|
|
640
728
|
}
|
|
641
729
|
|
|
642
730
|
/** @internal Called by name by the code the compiler writes for a `Set`, src/emit-map.ts. */
|
|
643
731
|
insertAt(absent: i64, key: T): void {
|
|
644
732
|
const packed = -1 - absent
|
|
645
|
-
let bucket = toI32(packed >> 32)
|
|
646
|
-
const h = toU32(packed)
|
|
647
|
-
if (toI32(this.entryKeys.length) >= INDEX_CAP) {
|
|
648
|
-
if (this.live >= INDEX_CAP || this.walks > 0) {
|
|
649
|
-
panic("Set maximum size exceeded")
|
|
650
|
-
}
|
|
651
|
-
this.rebuild()
|
|
652
|
-
bucket = -1
|
|
653
|
-
}
|
|
654
733
|
this.entryKeys.push(storedKey(key))
|
|
655
|
-
this.entryHashes.push(
|
|
656
|
-
this.
|
|
657
|
-
this.size = this.size + 1
|
|
734
|
+
this.entryHashes.push(toU32(packed))
|
|
735
|
+
this.addOne()
|
|
658
736
|
// Every entry takes a bucket, live or dead, until a rebuild, which files
|
|
659
737
|
// the new entry with the rest; otherwise it takes the bucket the probe found.
|
|
660
|
-
|
|
661
|
-
if (used * 4 > toI32(this.slots.length) * 3) {
|
|
738
|
+
if (!fileAppended(this.slots, this.mask, toI32(packed >> 32), this.entryHashes)) {
|
|
662
739
|
this.rebuild()
|
|
663
|
-
}
|
|
664
|
-
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/** `Map`'s count of one more live entry. */
|
|
744
|
+
private addOne(): void {
|
|
745
|
+
const live = this.live
|
|
746
|
+
if (live < 2147483647) {
|
|
747
|
+
this.live = live + 1
|
|
748
|
+
}
|
|
749
|
+
const size = this.size
|
|
750
|
+
if (size < 2147483647) {
|
|
751
|
+
this.size = size + 1
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/** `Map`'s count of one live entry fewer. */
|
|
756
|
+
private dropOne(): void {
|
|
757
|
+
const live = this.live
|
|
758
|
+
if (live > 0) {
|
|
759
|
+
this.live = live - 1
|
|
760
|
+
}
|
|
761
|
+
const size = this.size
|
|
762
|
+
if (size > 0) {
|
|
763
|
+
this.size = size - 1
|
|
665
764
|
}
|
|
666
765
|
}
|
|
667
766
|
|
|
@@ -677,6 +776,15 @@ export class Set<T> {
|
|
|
677
776
|
this.mask = toI32(slots.length) - 1
|
|
678
777
|
refile(slots, this.entryHashes)
|
|
679
778
|
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* The entry keys, dead ones included, as `Map.walkKeys`.
|
|
782
|
+
*
|
|
783
|
+
* @internal Called by `forEachElement`.
|
|
784
|
+
*/
|
|
785
|
+
walkKeys(): T[] {
|
|
786
|
+
return this.entryKeys
|
|
787
|
+
}
|
|
680
788
|
}
|
|
681
789
|
|
|
682
790
|
/**
|
|
@@ -702,8 +810,12 @@ const storedKey = <K>(key: K): K => key
|
|
|
702
810
|
*/
|
|
703
811
|
export const forEachEntry = <K, V>(m: Map<K, V>, f: (value: V, key: K) => void): void => {
|
|
704
812
|
m.walkOpen()
|
|
705
|
-
for (let i: i32 = m.walkNext(0); i >= 0; i = m.walkNext(i + 1)) {
|
|
706
|
-
|
|
813
|
+
for (let i: i32 = m.walkNext(0); i >= 0; i = i < 2147483647 ? m.walkNext(i + 1) : -1) {
|
|
814
|
+
const keys = m.walkKeys()
|
|
815
|
+
const values = m.walkValues()
|
|
816
|
+
if (i < keys.length && i < values.length) {
|
|
817
|
+
f(values[i], keys[i])
|
|
818
|
+
}
|
|
707
819
|
}
|
|
708
820
|
m.walkClose()
|
|
709
821
|
}
|
|
@@ -718,8 +830,11 @@ export const forEachEntry = <K, V>(m: Map<K, V>, f: (value: V, key: K) => void):
|
|
|
718
830
|
*/
|
|
719
831
|
export const forEachValue = <K, V>(m: Map<K, V>, f: (value: V) => void): void => {
|
|
720
832
|
m.walkOpen()
|
|
721
|
-
for (let i: i32 = m.walkNext(0); i >= 0; i = m.walkNext(i + 1)) {
|
|
722
|
-
|
|
833
|
+
for (let i: i32 = m.walkNext(0); i >= 0; i = i < 2147483647 ? m.walkNext(i + 1) : -1) {
|
|
834
|
+
const values = m.walkValues()
|
|
835
|
+
if (i < values.length) {
|
|
836
|
+
f(values[i])
|
|
837
|
+
}
|
|
723
838
|
}
|
|
724
839
|
m.walkClose()
|
|
725
840
|
}
|
|
@@ -734,8 +849,11 @@ export const forEachValue = <K, V>(m: Map<K, V>, f: (value: V) => void): void =>
|
|
|
734
849
|
*/
|
|
735
850
|
export const forEachElement = <T>(s: Set<T>, f: (value: T) => void): void => {
|
|
736
851
|
s.walkOpen()
|
|
737
|
-
for (let i: i32 = s.walkNext(0); i >= 0; i = s.walkNext(i + 1)) {
|
|
738
|
-
|
|
852
|
+
for (let i: i32 = s.walkNext(0); i >= 0; i = i < 2147483647 ? s.walkNext(i + 1) : -1) {
|
|
853
|
+
const keys = s.walkKeys()
|
|
854
|
+
if (i < keys.length) {
|
|
855
|
+
f(keys[i])
|
|
856
|
+
}
|
|
739
857
|
}
|
|
740
858
|
s.walkClose()
|
|
741
859
|
}
|