@zakkster/lite-bisect 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Bisect.d.ts +243 -0
- package/Bisect.js +450 -0
- package/CHANGELOG.md +125 -0
- package/LICENSE.txt +21 -0
- package/README.md +365 -2
- package/llms.txt +105 -0
- package/package.json +67 -4
package/README.md
CHANGED
|
@@ -1,3 +1,366 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zakkster/lite-bisect
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Zero-GC binary search kernels for sorted typed arrays. Lower / upper bound (branchless and branchy), a galloping hint search, segment and nearest -- one monomorphic copy of every kernel per array type (Float64, Float32, Uint32, Uint16, Int32). No throw on the hot path; zero allocation where the kernel inlines into its caller or every number argument is a Smi; a contract you can read in one screen.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@zakkster/lite-bisect)
|
|
6
|
+
[](https://github.com/sponsors/PeshoVurtoleta)
|
|
7
|
+

|
|
8
|
+

|
|
9
|
+
[](https://bundlephobia.com/result?p=@zakkster/lite-bisect)
|
|
10
|
+
[](https://www.npmjs.com/package/@zakkster/lite-bisect)
|
|
11
|
+
[](https://www.npmjs.com/package/@zakkster/lite-bisect)
|
|
12
|
+

|
|
13
|
+

|
|
14
|
+
[](./LICENSE.txt)
|
|
15
|
+
|
|
16
|
+
## The sorted-array search the ecosystem was missing
|
|
17
|
+
|
|
18
|
+
Every hot loop that maps a value to a slot -- a keyframe track, a LUT, a spatial strip, a timeline -- ends up hand-rolling the same binary search, once per array type, with a different edge-case bug each time. `lite-bisect` is that search written once, per type, with one stated contract: what a NaN query returns, what a tie returns, what an empty range returns, and what the caller owes the kernel.
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @zakkster/lite-bisect
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { lowerBoundBranchlessF64, upperBoundBranchlessF64, segmentF64, nearestF64 } from "@zakkster/lite-bisect";
|
|
26
|
+
|
|
27
|
+
// A sorted, non-decreasing Float64Array (no NaN inside). The range [lo, hi) is [0, 5).
|
|
28
|
+
const a = new Float64Array([1, 3, 3, 5, 8]);
|
|
29
|
+
|
|
30
|
+
lowerBoundBranchlessF64(a, 0, 5, 3); // -> 1
|
|
31
|
+
upperBoundBranchlessF64(a, 0, 5, 3); // -> 3
|
|
32
|
+
segmentF64(a, 0, 5, 4); // -> 2
|
|
33
|
+
nearestF64(a, 0, 5, 4); // -> 2
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Five array types, one contract, zero allocation where the kernel inlines into its caller. The kernels trust you: they do no range check and never throw. Validate once, off the hot path, with `assertRange` / `assertSorted`.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Table of contents
|
|
41
|
+
|
|
42
|
+
- [Why this exists](#why-this-exists)
|
|
43
|
+
- [What you get](#what-you-get)
|
|
44
|
+
- [Which call do I use](#which-call-do-i-use)
|
|
45
|
+
- [Deep dive: the kernels](#deep-dive-the-kernels)
|
|
46
|
+
- [API reference](#api-reference)
|
|
47
|
+
- [Composability](#composability)
|
|
48
|
+
- [Zero-GC design notes](#zero-gc-design-notes)
|
|
49
|
+
- [Design decisions worth knowing](#design-decisions-worth-knowing)
|
|
50
|
+
- [Testing](#testing)
|
|
51
|
+
- [What this is not](#what-this-is-not)
|
|
52
|
+
- [Ecosystem](#ecosystem)
|
|
53
|
+
- [License](#license)
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Why this exists
|
|
58
|
+
|
|
59
|
+
- **One search, written once per type.** A kernel that serves several array types from one call site goes megamorphic. So each type gets its own copy (generated from one template, checked byte-for-byte by `npm run gen:check`), and you call the per-type kernel by name.
|
|
60
|
+
- **A contract, not a convention.** NaN query -> `lo`. Tie in `nearest` -> the LOWER index. Empty range -> `lo`. Segment before the first key -> `lo - 1`. Each rule is in the header of `Bisect.js`, in the `.d.ts` JSDoc, and below.
|
|
61
|
+
- **Fail closed where it is cheap, trust where it is hot.** The hot kernels carry no range check (a guard that never fires still costs its bytes). The cold `assertRange` / `assertSorted` throw `RangeError`; call them once, at setup.
|
|
62
|
+
- **Claims that carry their evidence.** Every statement about inlining or allocation here is traced or gated, not assumed (see [Zero-GC design notes](#zero-gc-design-notes)).
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## What you get
|
|
67
|
+
|
|
68
|
+
- **37 exports.** 7 kernel families x 5 array types = 35 kernels, plus 2 cold asserts. No classes, no state, no constants.
|
|
69
|
+
- **Families:** `lowerBoundBranchless*`, `upperBoundBranchless*`, `lowerBoundBranchy*`, `upperBoundBranchy*`, `lowerBoundHint*`, `segment*`, `nearest*`.
|
|
70
|
+
- **Suffixes:** `F64` (Float64Array), `F32` (Float32Array), `U32` (Uint32Array), `U16` (Uint16Array), `I32` (Int32Array).
|
|
71
|
+
- **Cold asserts:** `assertRange(a, lo, hi)`, `assertSorted(a, lo, hi)`.
|
|
72
|
+
- **Hand-written TypeScript declarations** (`Bisect.d.ts`), checked by `tsc` 7.0.2 and by a parity test against the runtime exports.
|
|
73
|
+
- **Zero runtime dependencies**, `sideEffects: false`, single ESM file, Node >= 22.
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Which call do I use
|
|
78
|
+
|
|
79
|
+
LEDGER and RESEARCH ids refer to LEDGER.md and RESEARCH.md in the source repository; they are not in the npm tarball.
|
|
80
|
+
|
|
81
|
+
| Call | When | Evidence (LEDGER / RESEARCH id) | Tier |
|
|
82
|
+
|---|---|---|---|
|
|
83
|
+
| `lowerBoundBranchless*` / `upperBoundBranchless*` | The default for a read-only search. Read-only on F64 at n 16 / 256 / 4096 / 65,536 `lowerBoundBranchlessF64` read faster than `lowerBoundBranchyF64` on both gated CPUs (STATISTICAL; time ratio 0.373..0.553). | C7: TurboFan inlines the 103 B body; Maglev does not inline it at its default 100 B cap. Law 3: traced, not expected. ab.yml run 43145179770 (lanes ro:16..ro:65536). | STATISTICAL (speed: gated release run 43145179770, AMD EPYC 9V74 x86-64 + Apple M1 (Virtual) arm64; F64, n <= 65,536) + DETERMINISTIC (inline pin, traced on the pinned Node; see below) |
|
|
84
|
+
| `lowerBoundBranchy*` / `upperBoundBranchy*` | The search result feeds a write (an insert position, a splice index). On F64 at n 16 and 128, with the result feeding `copyWithin`, `lowerBoundBranchlessF64` read slower than `lowerBoundBranchyF64` on both gated CPUs (STATISTICAL; time ratio 1.128..1.633); n > 128 is not gated. | I1 (inherited) reproduced here: ab.yml run 43145179770 (lanes fw:16, fw:128). | STATISTICAL (gated release run 43145179770, both CPUs; F64, n <= 128) |
|
|
85
|
+
| `lowerBoundHint*` | Coherent query streams: each query lands near the previous answer (the hint). On F64 with a coherent walk (n 4096 and 65,536, next answer 1 or 4 slots away) `lowerBoundHint*` read faster than `lowerBoundBranchless*` on both gated CPUs (STATISTICAL; time ratio 0.352..0.572). On uniform (incoherent) queries at n 256 / 4096 / 65,536 it read slower (STATISTICAL; time ratio 1.910..2.762), and at n 256 with the next answer 4 slots away it read slower (STATISTICAL; time ratio 1.217 on AMD EPYC 9V74, 1.311 on Apple M1 (Virtual)). | C5, gated lanes only: ab.yml run 43145179770 (13 / 13 gated lanes as expected on both CPUs; its C5 rule reads DIFFERS at the REPORTED lane walk:256:1; LEDGER B19 says why that 2026-11-12 run stands). | STATISTICAL (gated release run 43145179770, AMD EPYC 9V74 x86-64 + Apple M1 (Virtual) arm64; F64, n <= 65,536) |
|
|
86
|
+
| `segment*` / `nearest*` | "Which interval holds x" (`segment`) / "which key is closest to x" (`nearest`). | Built on `upperBoundBranchless*` / `lowerBoundBranchless*`; contract rows in the oracle and boundary tests. | DETERMINISTIC (oracle, boundary, mutants) |
|
|
87
|
+
| `assertRange` / `assertSorted` | Setup, tests, debug builds. Never inside a hot loop. | Cold by design; no kernel calls them. | DETERMINISTIC (boundary tests) |
|
|
88
|
+
|
|
89
|
+
Run provenance: ab.yml run 43145179770, 2026-11-12, commit 679dcad, node v26.8.2, K = 11 interleaved rounds, C = 3 compilations, A/A twin inside 0.95..1.05, load1 1.02 (AMD EPYC 9V74) and 24.11 (Apple M1 (Virtual)) at start, median time ratio candidate / base. Not measured: the upperBound* kernels, the F32 / U32 / U16 / I32 copies, n > 65,536, non-power-of-two n, Maglev-tier callers.
|
|
90
|
+
|
|
91
|
+
There is no selector and no per-call front that picks a kernel for you: a per-call lookup makes the call site polymorphic and blocks inlining (LEDGER A3); the selector is parked and the front refused (LEDGER A5). You choose, by name.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## Deep dive: the kernels
|
|
96
|
+
|
|
97
|
+
<details>
|
|
98
|
+
<summary><strong>Contract, kernel by kernel</strong></summary>
|
|
99
|
+
|
|
100
|
+
Every kernel takes `(a, lo, hi, x)`; the hint kernel takes a fifth argument `h`.
|
|
101
|
+
|
|
102
|
+
- `a` -- a sorted, non-decreasing typed array of the suffix's type, no NaN inside.
|
|
103
|
+
- `[lo, hi)` -- the half-open range searched: integers, `0 <= lo <= hi <= a.length`, `hi - lo <= 2^30 - 1`.
|
|
104
|
+
- **lower bound** -- the first `i` in `[lo, hi)` with `a[i] >= x`, else `hi`.
|
|
105
|
+
- **upper bound** -- the first `i` in `[lo, hi)` with `a[i] > x`, else `hi`.
|
|
106
|
+
- **hint** -- `lowerBoundHint*(a, lo, hi, x, h)`: the lower bound, galloping out from `h`. `h` is an integer index or NaN (NaN -> `lo`; outside `[lo, hi]` it is clamped). A non-integer `h` is a caller error that may return a wrong index or never return.
|
|
107
|
+
- **segment** -- `upper - 1`: the last `i` with `a[i] <= x`; `lo - 1` when `x < a[lo]`, on a NaN `x`, and on an empty range.
|
|
108
|
+
- **nearest** -- the index closest to `x`. A tie between the last value below `x` and the first above goes to the LOWER index. An exact match wins (the first `i` with `a[i] === x`). Within a run of equal values, the index next to `x`. An empty range -> `lo`. NaN -> `lo`.
|
|
109
|
+
- **NaN query** -- compares false everywhere: every lower / upper / hint kernel returns `lo`; `segment` returns `lo - 1`; `nearest` returns `lo`.
|
|
110
|
+
|
|
111
|
+
```js
|
|
112
|
+
import { lowerBoundBranchlessF64, lowerBoundBranchyF64, lowerBoundHintF64, segmentF64, nearestF64 } from "@zakkster/lite-bisect";
|
|
113
|
+
|
|
114
|
+
const a = new Float64Array([1, 3, 3, 5, 8]);
|
|
115
|
+
|
|
116
|
+
lowerBoundBranchlessF64(a, 0, 5, 4); // -> 3
|
|
117
|
+
lowerBoundBranchlessF64(a, 0, 5, 9); // -> 5
|
|
118
|
+
lowerBoundBranchlessF64(a, 0, 5, NaN); // -> 0
|
|
119
|
+
lowerBoundBranchlessF64(a, 2, 5, NaN); // -> 2
|
|
120
|
+
lowerBoundBranchyF64(a, 0, 5, 3); // -> 1
|
|
121
|
+
|
|
122
|
+
segmentF64(a, 0, 5, 3); // -> 2
|
|
123
|
+
segmentF64(a, 0, 5, 0); // -> -1
|
|
124
|
+
segmentF64(a, 0, 5, NaN); // -> -1
|
|
125
|
+
segmentF64(a, 2, 2, 4); // -> 1
|
|
126
|
+
|
|
127
|
+
nearestF64(a, 0, 5, 2); // -> 0
|
|
128
|
+
nearestF64(a, 0, 5, 3); // -> 1
|
|
129
|
+
nearestF64(a, 0, 5, 6.5); // -> 3
|
|
130
|
+
nearestF64(a, 0, 5, 100); // -> 4
|
|
131
|
+
nearestF64(a, 2, 2, 4); // -> 2
|
|
132
|
+
|
|
133
|
+
lowerBoundHintF64(a, 0, 5, 3, 4); // -> 1
|
|
134
|
+
lowerBoundHintF64(a, 0, 5, 8, 0); // -> 4
|
|
135
|
+
lowerBoundHintF64(a, 0, 5, 5, NaN); // -> 3
|
|
136
|
+
lowerBoundHintF64(a, 0, 5, 5, 99); // -> 3
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The tie rule in words: in `[1, 3, 3, 5, 8]`, the value 4 sits exactly between 3 (index 2) and 5 (index 3), so `nearest` returns the lower index 2; 2 sits between 1 and 3 and returns 0; 6.5 sits between 5 and 8 and returns 3.
|
|
140
|
+
|
|
141
|
+
The other four types behave identically; only the array type and the value domain differ:
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
import { lowerBoundBranchlessU16, upperBoundBranchlessI32, nearestI32, lowerBoundBranchlessF32, segmentU32 } from "@zakkster/lite-bisect";
|
|
145
|
+
|
|
146
|
+
lowerBoundBranchlessU16(new Uint16Array([10, 20, 30]), 0, 3, 25); // -> 2
|
|
147
|
+
upperBoundBranchlessI32(new Int32Array([-5, 0, 7]), 0, 3, 0); // -> 2
|
|
148
|
+
nearestI32(new Int32Array([-5, 0, 7]), 0, 3, -1); // -> 1
|
|
149
|
+
lowerBoundBranchlessF32(new Float32Array([0.5, 1.5, 2.5]), 0, 3, 1.5); // -> 1
|
|
150
|
+
segmentU32(new Uint32Array([1, 4294967295]), 0, 2, 4294967295); // -> 1
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
</details>
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## API reference
|
|
158
|
+
|
|
159
|
+
All 37 exports. Every kernel returns a `number` (an index). `*` is one of `F64`, `F32`, `U32`, `U16`, `I32`; the array type `A` is `Float64Array`, `Float32Array`, `Uint32Array`, `Uint16Array`, `Int32Array` respectively.
|
|
160
|
+
|
|
161
|
+
| Family | Signature (per suffix) | Returns |
|
|
162
|
+
|---|---|---|
|
|
163
|
+
| `lowerBoundBranchless*` | `(a: A, lo, hi, x) => number` | first `i` in `[lo, hi)` with `a[i] >= x`, else `hi`; branchless |
|
|
164
|
+
| `upperBoundBranchless*` | `(a: A, lo, hi, x) => number` | first `i` in `[lo, hi)` with `a[i] > x`, else `hi`; branchless |
|
|
165
|
+
| `lowerBoundBranchy*` | `(a: A, lo, hi, x) => number` | same as lower, branchy loop |
|
|
166
|
+
| `upperBoundBranchy*` | `(a: A, lo, hi, x) => number` | same as upper, branchy loop |
|
|
167
|
+
| `lowerBoundHint*` | `(a: A, lo, hi, x, h) => number` | lower bound, galloping from the hint `h` |
|
|
168
|
+
| `segment*` | `(a: A, lo, hi, x) => number` | last `i` with `a[i] <= x`; `lo - 1` if none, on NaN, or on an empty range |
|
|
169
|
+
| `nearest*` | `(a: A, lo, hi, x) => number` | index closest to `x`; tie -> lower; empty / NaN -> `lo` |
|
|
170
|
+
|
|
171
|
+
The 35 kernel names, in full:
|
|
172
|
+
|
|
173
|
+
- Float64: `lowerBoundBranchlessF64`, `upperBoundBranchlessF64`, `lowerBoundBranchyF64`, `upperBoundBranchyF64`, `lowerBoundHintF64`, `segmentF64`, `nearestF64`
|
|
174
|
+
- Float32: `lowerBoundBranchlessF32`, `upperBoundBranchlessF32`, `lowerBoundBranchyF32`, `upperBoundBranchyF32`, `lowerBoundHintF32`, `segmentF32`, `nearestF32`
|
|
175
|
+
- Uint32: `lowerBoundBranchlessU32`, `upperBoundBranchlessU32`, `lowerBoundBranchyU32`, `upperBoundBranchyU32`, `lowerBoundHintU32`, `segmentU32`, `nearestU32`
|
|
176
|
+
- Uint16: `lowerBoundBranchlessU16`, `upperBoundBranchlessU16`, `lowerBoundBranchyU16`, `upperBoundBranchyU16`, `lowerBoundHintU16`, `segmentU16`, `nearestU16`
|
|
177
|
+
- Int32: `lowerBoundBranchlessI32`, `upperBoundBranchlessI32`, `lowerBoundBranchyI32`, `upperBoundBranchyI32`, `lowerBoundHintI32`, `segmentI32`, `nearestI32`
|
|
178
|
+
|
|
179
|
+
Cold checks (one copy each, called by no kernel):
|
|
180
|
+
|
|
181
|
+
| Export | Signature | Behavior |
|
|
182
|
+
|---|---|---|
|
|
183
|
+
| `assertRange` | `(a: BisectArray, lo: number, hi: number) => void` | `RangeError` unless `a.length` is a number and `a.length`, `lo`, `hi` are safe integers, `0 <= lo <= hi <= a.length`, `hi - lo <= 2^30 - 1`. Reads only `a.length`. |
|
|
184
|
+
| `assertSorted` | `(a: BisectArray, lo: number, hi: number) => void` | `assertRange`, then `RangeError` on a NaN in `[lo, hi)` or a descent (`a[i] < a[i - 1]`). |
|
|
185
|
+
|
|
186
|
+
`BisectArray` is the type `Float64Array | Float32Array | Uint32Array | Uint16Array | Int32Array`.
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
import { assertRange, assertSorted } from "@zakkster/lite-bisect";
|
|
190
|
+
|
|
191
|
+
const a = new Float64Array([1, 3, 3, 5, 8]);
|
|
192
|
+
|
|
193
|
+
assertRange(a, 0, 5); // -> undefined
|
|
194
|
+
assertRange(a, 0, 6); // -> throws RangeError
|
|
195
|
+
assertRange(a, 3, 2); // -> throws RangeError
|
|
196
|
+
assertSorted(a, 0, 5); // -> undefined
|
|
197
|
+
assertSorted(new Float64Array([2, 1]), 0, 2); // -> throws RangeError
|
|
198
|
+
assertSorted(new Float64Array([1, NaN]), 0, 2); // -> throws RangeError
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**Constants:** none. The package exports no constants.
|
|
202
|
+
|
|
203
|
+
**The contract, in one place**
|
|
204
|
+
|
|
205
|
+
- Ranges are integers with `0 <= lo <= hi <= a.length` and `hi - lo <= 2^30 - 1`.
|
|
206
|
+
- The hot kernels trust the caller. A range outside the contract, an unsorted array, a NaN inside the array, or a non-integer hint may return a wrong index or never return.
|
|
207
|
+
- A NaN query returns `lo` from every lower / upper / hint kernel, `lo - 1` from `segment`, and `lo` from `nearest`.
|
|
208
|
+
- `nearest`: a tie goes to the LOWER index; an exact match wins; within a run of equal values the index next to `x`; an empty range returns `lo`.
|
|
209
|
+
- The hint `h` is an integer index or NaN. A non-integer `h` is a caller error.
|
|
210
|
+
- Node >= 22.
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
## Composability
|
|
215
|
+
|
|
216
|
+
A keyframe track: sorted times in a `Float64Array`, values in a parallel `Float64Array`. Validate once, then sample inside the frame loop with `segmentF64` and a lerp. Allocation follows the rule in [Zero-GC design notes](#zero-gc-design-notes): none once segmentF64 inlines into sample and sample into the frame loop; sample takes and returns doubles, so a call V8 does not inline boxes.
|
|
217
|
+
|
|
218
|
+
```js
|
|
219
|
+
import { assertSorted, segmentF64 } from "@zakkster/lite-bisect";
|
|
220
|
+
|
|
221
|
+
const times = new Float64Array([0, 10, 20, 40]);
|
|
222
|
+
const vals = new Float64Array([0, 100, 50, 90]);
|
|
223
|
+
const n = times.length;
|
|
224
|
+
|
|
225
|
+
// Setup, cold: validate once.
|
|
226
|
+
assertSorted(times, 0, n); // -> undefined
|
|
227
|
+
|
|
228
|
+
// Hot: one segment search, one lerp. Clamps outside [times[0], times[n - 1]].
|
|
229
|
+
function sample(t) {
|
|
230
|
+
const i = segmentF64(times, 0, n, t);
|
|
231
|
+
if (i < 0) return vals[0];
|
|
232
|
+
if (i >= n - 1) return vals[n - 1];
|
|
233
|
+
const u = (t - times[i]) / (times[i + 1] - times[i]);
|
|
234
|
+
return vals[i] + (vals[i + 1] - vals[i]) * u;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
sample(5); // -> 50
|
|
238
|
+
sample(30); // -> 70
|
|
239
|
+
sample(-3); // -> 0
|
|
240
|
+
sample(99); // -> 90
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
`sample` needs a segment (`upper - 1`, the last `i` with `times[i] <= t`). `lowerBoundHint*` answers a lower bound (the first `i` with `times[i] >= t`), not a segment, so it is not a drop-in hint for `segmentF64`; there is no `segmentHint*` export (a post-1.0 item, [ROADMAP](https://github.com/PeshoVurtoleta/lite-bisect/blob/main/ROADMAP.md) 8a item 11). On `times` above (t = 5) and on a track with a duplicate key (`[0, 10, 10, 20]`, t = 10) the two differ:
|
|
244
|
+
|
|
245
|
+
```js
|
|
246
|
+
import { segmentF64, lowerBoundHintF64 } from "@zakkster/lite-bisect";
|
|
247
|
+
|
|
248
|
+
const times = new Float64Array([0, 10, 20, 40]);
|
|
249
|
+
segmentF64(times, 0, 4, 5); // -> 0
|
|
250
|
+
lowerBoundHintF64(times, 0, 4, 5, 0); // -> 1
|
|
251
|
+
segmentF64(new Float64Array([0, 10, 10, 20]), 0, 4, 10); // -> 2
|
|
252
|
+
lowerBoundHintF64(new Float64Array([0, 10, 10, 20]), 0, 4, 10, 0); // -> 1
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Where a lower bound is what your caller needs and the stream is monotone, the previous answer is a good hint; whether `lowerBoundHint*` beats the plain kernel on your stream is a measurement for your caller (see the table above: STATISTICAL on F64, n <= 65,536, two CPUs; your stream, CPU and array type are still a measurement).
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## Zero-GC design notes
|
|
260
|
+
|
|
261
|
+
<details>
|
|
262
|
+
<summary><strong>Allocation table, inlining, and gated numbers</strong></summary>
|
|
263
|
+
|
|
264
|
+
The sentence this package stands behind, on one line:
|
|
265
|
+
|
|
266
|
+
zero allocation where the kernel inlines into its caller, or every number argument (the query; the hint of lowerBoundHint*; lo / hi) is a Smi; a non-Smi number computed at run time or read from a typed array (a fractional query, a NaN hint read from a Float64Array, -0 for lo / hi) and passed across a call V8 does not inline is boxed by the caller: one 16 B HeapNumber per such argument on Node. For no hint pass lo or the literal NaN: neither boxes.
|
|
267
|
+
|
|
268
|
+
| Situation | Allocation |
|
|
269
|
+
|---|---|
|
|
270
|
+
| Kernel inlined into its caller (any query) | the floor |
|
|
271
|
+
| Kernel called across a real call, every number argument a Smi (query; h; lo / hi), or h the literal NaN | the floor |
|
|
272
|
+
| Kernel called across a real call V8 does not inline, a non-Smi query computed at run time or read from a typed array on Node (fractional, NaN, -0, or outside [-2^31, 2^31)), every other number argument a Smi | one 16 B HeapNumber per non-Smi argument, here one, made by the caller (LEDGER C2, C9, C12); pointer-compressed V8 (Chrome / Electron: also [2^30, 2^31)) is unmeasured here |
|
|
273
|
+
| `lowerBoundHint*` called with a NaN hint read from a Float64Array and a Smi query, across a call V8 does not inline | one 16 B HeapNumber for the hint (one non-Smi argument), made by the CALLER (LEDGER C9); the literal `NaN` or `h = lo` is the floor |
|
|
274
|
+
| Two non-Smi arguments across a call V8 does not inline (e.g. `lowerBoundHint*` with a fractional query and a NaN hint, both read from Float64Arrays) | 2 x 16 B: one HeapNumber per non-Smi argument, made by the caller (LEDGER C12); gated by the `fracnanhint` lane of the alloc gate (run B 28..36 B/op, run A the floor) |
|
|
275
|
+
| `assertRange` / `assertSorted` | cold: the thrown `RangeError` and its message string (only on a failure) |
|
|
276
|
+
|
|
277
|
+
Where TurboFan inlines a kernel (traced on the pinned Node, LEDGER C7), the non-Smi query never crosses a real call, so there is no box. Where it does not -- an un-optimized caller, or a call site V8 declines to inline -- there is. Pass a Smi query, and h = lo (or the literal NaN) instead of a NaN read at run time, when a kernel sits across such a call.
|
|
278
|
+
|
|
279
|
+
A wrapper is not a shield: segment* (25 B) fits Maglev's 100 B cap but its inner 103 B branchless call does not (LEDGER C1), and nearest* (189 B) is expected not to be inlined by Maglev at all (189 B >= its 100 B cap; untraced; TurboFan inlines it), so a Maglev-tier caller with a non-Smi query is expected to box 16 B per call (SPEC R1; expected, not traced here).
|
|
280
|
+
|
|
281
|
+
Inlining facts (LEDGER C1, C7): the branchless lower / upper bodies are 103 B of bytecode; Maglev does not inline them at its default 100 B cap, TurboFan does. The branchy pair is 55 B. This is a property of the pinned V8, never assumed: the 103 B count and the TurboFan pin are gated below; Maglev's refusal was traced in P0, not gated here (law 3: EXPECTED inlining is not TRACED).
|
|
282
|
+
|
|
283
|
+
Gated numbers (each is ONE run, quoted as a measurement, never as a law):
|
|
284
|
+
|
|
285
|
+
| Gate | Result | Source and provenance |
|
|
286
|
+
|---|---|---|
|
|
287
|
+
| Oracle | 5 types x 4 seeds plus a rounded-tie phase (>= 1000 rounded ties per seed on F64 / F32 / I32), exact BigInt reference, 0 failures; an independent exact nearest test (100,000 draws per type) | `npm test` in the same log as the Mutants row |
|
|
288
|
+
| Mutants | 106 rows (5 per kernel x type + 3 rounded-tie + 3 infinity rows), every row KILLED, control PASS | the `npm test` step of `.handoff/release-1.0.0/verify2.log` (`npm run verify`, exit 0, 2026-11-23T08:33:08Z..08:35:53Z, node v26.8.2, darwin arm64, load1 2.10 at start, host QUIET by `quiet-lock`, no other node at 10% CPU or more, one run); the live row "every row KILLED (killed === rows)" |
|
|
289
|
+
| Coverage (c8) | 100% lines (450 / 450), 100% branches (241 / 241), 37 / 37 functions | same log (the `npm run coverage` step; the coverage glob includes the docs test) |
|
|
290
|
+
| Torture, phase 1 | 4096 cycles, 143360 / 143360 checks, size 0, alive 0, orphans 0, leaks 0 | same log (the `npm run torture` step, pinned flags) |
|
|
291
|
+
| Torture, phase 2 | 35 / 35 lanes x 200000 ops per lane; retained B/op min 0.001, max 0.043; steady gc major 0, minor 0 (both gated) | same log |
|
|
292
|
+
| TurboFan inline pin | every pinned edge inlined (traced per kernel x type); the M4 size control passes on the pinned Node (35 edges over the limit, nearest* now among them) | `npm test` in the same log as the Mutants row; the M4 control skips (never passes) on any other Node |
|
|
293
|
+
| Alloc gate, run A (inlined) | 155 / 155 lanes read 0.00 B/op in every window 1..5 (empty lane 0.00; tool floor 0.00) | `.handoff/release-1.0.0/gates.log` (`npm run gates`, 2026-11-23T08:30:06Z..08:31:47Z, node v26.8.2, darwin arm64, load1 6.47 at start, host PRELIMINARY by `quiet-lock` (B/op counts, no timing), no other node at 10% CPU or more, one run, 21 / 21 pass) |
|
|
294
|
+
| Alloc gate, run B (not inlined) | non-Smi query lanes (105) and the NaN-hint-read lanes (5) read 16.00 B/op in every window 1..5 (one HeapNumber); the two-argument `fracnanhint` lanes (5) 32.00 B/op (two); Smi lanes (35) 0.00 B/op; literal-NaN-hint lanes (5) 0.00 B/op; empty lane 0.00 | same log as run A |
|
|
295
|
+
| Bytecode gate | branchless 103 B, branchy 55 B, hint 228 B, segment 25 B, nearest 189 B, five copies equal | same log as run A |
|
|
296
|
+
|
|
297
|
+
The alloc and bytecode rows above were read with `npm run gates` on the installed `@zakkster/lite-utils` 1.1.0 (`alloc-per-op.mjs` sha256 610bbfa8c44ea947..., `bytecode-budget.sh` sha256 deeee04c9bfc46c7...). The gates run on the pinned Node v26.8.2 only; a run on any other Node is NO VERDICT, never a pass. `npm run gates` takes the tools from the devDependency `@zakkster/lite-utils` (^1.1.0, the first release that ships `alloc-per-op.mjs`) and runs in hosted CI (the `gates` job of `.github/workflows/ci.yml`, ubuntu-latest x86 and macos-latest arm64); `npm run gates:local` takes them from a LiteUtils checkout (`LITE_UTILS_DIR`, which overrides the installed package when set). A LiteUtils version outside ^1.1.0, or a missing tool, is NO VERDICT. Tier: DETERMINISTIC; not in `npm test`.
|
|
298
|
+
|
|
299
|
+
</details>
|
|
300
|
+
|
|
301
|
+
---
|
|
302
|
+
|
|
303
|
+
## Design decisions worth knowing
|
|
304
|
+
|
|
305
|
+
- **A per-type kernel, by name.** One call site fed several array types goes megamorphic (LEDGER C3), so there is no `lowerBound(a, ...)` that accepts any type. Call `lowerBoundBranchlessF64` for a Float64Array.
|
|
306
|
+
- **No length switch, no linear-scan fallback.** The draft's `lowerBound` (a length switch), `LINEAR_MAX` and `lowerBoundLinear` did not ship: the switch never read faster and read slower at every small n measured (LEDGER A4, C4; PRELIMINARY: one Apple M4 Pro, one strict run, P0 2026-11-10; no number is claimed).
|
|
307
|
+
- **No selector, no per-call front.** A per-call lookup blocks inlining (LEDGER A3); the setup-time selector is parked (its evidence is arm64-only) and the per-call type front is refused (LEDGER A5). No Eytzinger layout either (LEDGER A6, parked: unmeasured here).
|
|
308
|
+
- **The branchless body stays at 103 B.** A 92 B body inlines under Maglev but read slower under TurboFan at every fixed length measured (PRELIMINARY: one Apple M4 Pro, one strict run, P0 2026-11-10; no number is claimed); the maintainer ruled to keep the 103 B body (LEDGER C7).
|
|
309
|
+
- **Trust on the hot path, assert on the cold one.** Range checks cost bytes in every call; the asserts are separate functions no kernel calls.
|
|
310
|
+
- **NaN fails closed by construction.** The comparisons are written so a NaN query compares false everywhere and returns `lo`.
|
|
311
|
+
- **Hint `h` is a caller error when it is not an integer or NaN.** It may return a wrong index or never return. This is documented, not guarded (P1-R9).
|
|
312
|
+
- **Range capped at `hi - lo <= 2^30 - 1`** (P1-R8).
|
|
313
|
+
|
|
314
|
+
### Known limits
|
|
315
|
+
|
|
316
|
+
- **A NaN hint read at run time boxes in the caller (AU-R2 / AU-R3, LEDGER C9).** `lowerBoundHint*` itself allocates nothing for it; the caller's HeapNumber is made when a NaN read from a Float64Array crosses a call V8 does not inline: 16.03 B/op (lane `smiq_nan_from_f64`) against 0.03 B/op for the literal `NaN` (lane `smiq_nan_literal`). Reproduce with `audit/2026-11-11-bisect/cross-fable/cross-repro.mjs --d4 <Bisect.js> <lane>` (flags in its header). Measured 2026-11-11, darwin arm64, node v26.8.2, 2 runs, load1 1.5 / 2.0, by the audit's cross-check probe (a local audit artifact, not published: neither in the npm tarball nor tracked in the repository). For no hint pass `lo` or the literal `NaN`.
|
|
317
|
+
|
|
318
|
+
---
|
|
319
|
+
|
|
320
|
+
## Testing
|
|
321
|
+
|
|
322
|
+
`npm test` runs 376 tests, 376 pass, 0 fail (the `npm test` step of `.handoff/release-1.0.0/verify2.log`, the run named in the Mutants row above). They cover: a naive-reference oracle per type (4 seeds each), a boundary table (every kernel x type), the contract rows for `segment` / `nearest` / the asserts, mutant kills, golden parity of the generated kernels, the TurboFan inline pin, the types check, doc truth (snippets, Q-BOX, banned tokens; `test/Bisect.docs.test.mjs`), and tree-shaking (`test/Bisect.shake.test.mjs`).
|
|
323
|
+
|
|
324
|
+
Tree-shaking is gated under esbuild 0.28 only (`test/Bisect.shake.test.mjs`, deterministic, part of `npm test`). A bundle that imports one kernel contains that kernel, the kernels it calls (lowerBoundHint* and nearest* call lowerBoundBranchless*, segment* calls upperBoundBranchless*, assertSorted calls assertRange), and no other top-level code. A bare side-effect import of Bisect.js (the gate imports it by path) emits no kernel, and esbuild reports that it honours `"sideEffects": false`. webpack, rollup and terser are not tested.
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
npm test # oracle, boundary, mutants, golden parity, gen check, inline pin, types, doc truth, tree-shaking
|
|
328
|
+
npm run torture # node --expose-gc --no-concurrent-recompilation --single-threaded-gc --min-semi-space-size=1 --max-semi-space-size=1 test/torture.mjs : leak + retained-heap + steady-minor gate, and NO VERDICT without --single-threaded-gc (concurrent sweeping made the retained term flaky under load), and NO VERDICT (exit 2) when a lane, or a control that is not flagged, is still undrained after a 2000 ms re-settle and nothing FAILs (a FAIL exits 1)
|
|
329
|
+
npm run coverage # c8, 100% lines and branches required
|
|
330
|
+
npm run test:types # tsc in node16 / nodenext / bundler resolution + export parity
|
|
331
|
+
npm run gen:check # the generated kernel region matches its template
|
|
332
|
+
npm run gates # alloc + bytecode gates (installed @zakkster/lite-utils ^1.1.0; pinned Node v26.8.2 only; ci.yml gates job)
|
|
333
|
+
npm run gates:local # the same gates on a LiteUtils checkout (LITE_UTILS_DIR=../LiteUtils)
|
|
334
|
+
npm run size # npm pack file list + size budgets (test/gates/size-budget.json); not in npm test
|
|
335
|
+
npm run verify # test + torture + coverage + size + gates:local (fail-fast)
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
Statistical claims: the hint versus branchless and branchless versus branchy speed words in the choice table are gated once per release by the hosted A/B (`bench/ab-gate.mjs`, run by `.github/workflows/ab.yml`, ubuntu-latest + macos-latest, dispatch-only; in the source repository, not the tarball). 1.0.0 is gated by run 43145179770 (VERDICT PASS gated=13/13 on both CPUs, F64 only; Tier: STATISTICAL). The length switch and the 92 B body are not gated (PRELIMINARY, see Design decisions).
|
|
339
|
+
|
|
340
|
+
---
|
|
341
|
+
|
|
342
|
+
## What this is not
|
|
343
|
+
|
|
344
|
+
- **Not a general-purpose search.** Typed arrays only, sorted non-decreasing, no NaN inside. No plain `Array`, no comparator callback, no objects.
|
|
345
|
+
- **Not a sorting library.** It never reorders anything.
|
|
346
|
+
- **Not a validator on the hot path.** The kernels trust the caller; `assertRange` / `assertSorted` are the cold checks.
|
|
347
|
+
- **Not a polymorphic front.** There is no `lowerBound(a, ...)` that picks a kernel from the array type or the length; you call the per-type kernel by name.
|
|
348
|
+
- **No benchmark table.** The only STATISTICAL speed words are the choice table's rows (F64, two CPUs, one gated release run); the two Design decisions speed words (length switch, 92 B body) are PRELIMINARY (one CPU); the table says which call fits which situation and what evidence stands behind it.
|
|
349
|
+
- **Not an Eytzinger / B-tree layout.** Plain sorted arrays only.
|
|
350
|
+
|
|
351
|
+
---
|
|
352
|
+
|
|
353
|
+
## Ecosystem
|
|
354
|
+
|
|
355
|
+
Part of the **@zakkster** zero-GC stack:
|
|
356
|
+
|
|
357
|
+
- [`lite-signal`](https://www.npmjs.com/package/@zakkster/lite-signal) -- zero-GC reactive graph for hot paths
|
|
358
|
+
- [`lite-leak`](https://www.npmjs.com/package/@zakkster/lite-leak) -- leak detection (used by the torture gate)
|
|
359
|
+
- [`lite-gc-profiler`](https://www.npmjs.com/package/@zakkster/lite-gc-profiler) -- GC and allocation profiling (used by the torture gate)
|
|
360
|
+
- **`lite-bisect`** -- this package
|
|
361
|
+
|
|
362
|
+
---
|
|
363
|
+
|
|
364
|
+
## License
|
|
365
|
+
|
|
366
|
+
MIT (c) Zahary Shinikchiev <shinikchiev@yahoo.com>
|
package/llms.txt
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# @zakkster/lite-bisect
|
|
2
|
+
|
|
3
|
+
Zero-GC binary search kernels for sorted typed arrays. Lower / upper bound
|
|
4
|
+
(branchless and branchy), a galloping hint search, segment and nearest. One
|
|
5
|
+
monomorphic copy of every kernel per array type: F64 (Float64Array), F32
|
|
6
|
+
(Float32Array), U32 (Uint32Array), U16 (Uint16Array), I32 (Int32Array).
|
|
7
|
+
Single-file ESM (Bisect.js), hand-written types (Bisect.d.ts), zero runtime
|
|
8
|
+
dependencies, Node >= 22.
|
|
9
|
+
|
|
10
|
+
## Exports (37)
|
|
11
|
+
|
|
12
|
+
7 kernel families x 5 suffixes = 35 kernels, plus 2 cold asserts. No classes,
|
|
13
|
+
no state, no constants (none).
|
|
14
|
+
|
|
15
|
+
Kernel signature: (a, lo, hi, x) => number. The hint kernel adds h.
|
|
16
|
+
|
|
17
|
+
- lowerBoundBranchlessF64, lowerBoundBranchlessF32, lowerBoundBranchlessU32, lowerBoundBranchlessU16, lowerBoundBranchlessI32
|
|
18
|
+
- upperBoundBranchlessF64, upperBoundBranchlessF32, upperBoundBranchlessU32, upperBoundBranchlessU16, upperBoundBranchlessI32
|
|
19
|
+
- lowerBoundBranchyF64, lowerBoundBranchyF32, lowerBoundBranchyU32, lowerBoundBranchyU16, lowerBoundBranchyI32
|
|
20
|
+
- upperBoundBranchyF64, upperBoundBranchyF32, upperBoundBranchyU32, upperBoundBranchyU16, upperBoundBranchyI32
|
|
21
|
+
- lowerBoundHintF64, lowerBoundHintF32, lowerBoundHintU32, lowerBoundHintU16, lowerBoundHintI32 (a, lo, hi, x, h)
|
|
22
|
+
- segmentF64, segmentF32, segmentU32, segmentU16, segmentI32
|
|
23
|
+
- nearestF64, nearestF32, nearestU32, nearestU16, nearestI32
|
|
24
|
+
- assertRange(a, lo, hi), assertSorted(a, lo, hi) (cold; a is any of the five array types)
|
|
25
|
+
|
|
26
|
+
## Contract
|
|
27
|
+
|
|
28
|
+
- a: a sorted, non-decreasing typed array of the suffix's type, no NaN inside.
|
|
29
|
+
- [lo, hi): half-open range, integers, 0 <= lo <= hi <= a.length, hi - lo <= 2^30 - 1.
|
|
30
|
+
- lower: the first i in [lo, hi) with a[i] >= x, else hi.
|
|
31
|
+
- upper: the first i in [lo, hi) with a[i] > x, else hi.
|
|
32
|
+
- hint: lowerBoundHint*(a, lo, hi, x, h) is lower, galloping out from h. h is an integer
|
|
33
|
+
index or NaN (NaN -> lo; outside [lo, hi] clamped). A non-integer h is a caller error
|
|
34
|
+
that may return a wrong index or never return.
|
|
35
|
+
- segment: upper - 1, the last i with a[i] <= x; lo - 1 when x < a[lo], on NaN, on an empty range.
|
|
36
|
+
- nearest: the index closest to x. A tie between the last value below x and the first above
|
|
37
|
+
goes to the LOWER index. An exact match wins (the first i with a[i] === x). Within a run of
|
|
38
|
+
equal values, the index next to x. Empty range -> lo. NaN -> lo.
|
|
39
|
+
- NaN query: every lower / upper / hint kernel returns lo; segment returns lo - 1; nearest returns lo.
|
|
40
|
+
- The hot kernels trust the caller: no range check, no throw. A range outside the contract, an
|
|
41
|
+
unsorted array, a NaN inside the array, or a non-integer hint may return a wrong index or never
|
|
42
|
+
return. assertRange (RangeError unless the range is valid; reads only a.length) and assertSorted
|
|
43
|
+
(assertRange, then RangeError on a NaN in [lo, hi) or a descent) are cold: call them off the hot
|
|
44
|
+
path. No kernel calls them.
|
|
45
|
+
|
|
46
|
+
## Allocation
|
|
47
|
+
|
|
48
|
+
zero allocation where the kernel inlines into its caller, or every number argument (the query; the hint of lowerBoundHint*; lo / hi) is a Smi; a non-Smi number computed at run time or read from a typed array (a fractional query, a NaN hint read from a Float64Array, -0 for lo / hi) and passed across a call V8 does not inline is boxed by the caller: one 16 B HeapNumber per such argument on Node. For no hint pass lo or the literal NaN: neither boxes.
|
|
49
|
+
|
|
50
|
+
Pass a Smi query, and h = lo (or the literal NaN) instead of a NaN read at run time, when a kernel sits across a call V8 does not inline.
|
|
51
|
+
|
|
52
|
+
## Which call to choose
|
|
53
|
+
|
|
54
|
+
LEDGER and RESEARCH ids refer to LEDGER.md and RESEARCH.md in the source repository; they are not in the npm tarball.
|
|
55
|
+
|
|
56
|
+
- lowerBoundBranchless* / upperBoundBranchless*: the default for a read-only search. On F64 at
|
|
57
|
+
n 16 / 256 / 4096 / 65,536 lowerBoundBranchlessF64 read faster than lowerBoundBranchyF64 (STATISTICAL; time ratio 0.373..0.553; ab.yml run
|
|
58
|
+
43145179770, AMD EPYC 9V74 x86-64 and Apple M1 (Virtual) arm64). TurboFan inlines the 103 B body; Maglev does not inline it at its default 100 B cap
|
|
59
|
+
(LEDGER C7; traced on the pinned Node v26.8.2, DETERMINISTIC inline pin).
|
|
60
|
+
- lowerBoundBranchy* / upperBoundBranchy*: the result feeds a write (an insert position, a splice
|
|
61
|
+
index). On F64 at n 16 and 128 with the result feeding copyWithin, lowerBoundBranchlessF64 read slower than
|
|
62
|
+
lowerBoundBranchyF64 (STATISTICAL; time ratio 1.128..1.633 on both CPUs); n > 128 is not gated.
|
|
63
|
+
- lowerBoundHint*: coherent query streams (each query near the previous answer). On F64, coherent
|
|
64
|
+
walk (n 4096 / 65,536, next answer 1 or 4 slots away): faster than lowerBoundBranchless* (STATISTICAL;
|
|
65
|
+
time ratio 0.352..0.572). Uniform (incoherent) queries at n 256 / 4096 / 65,536: slower (STATISTICAL;
|
|
66
|
+
time ratio 1.910..2.762), and n 256 with the next answer 4 slots away: slower (STATISTICAL; time ratio
|
|
67
|
+
1.217 / 1.311). Not measured: upperBound*, F32 / U32 / U16 / I32, n > 65,536, non-power-of-two n, Maglev-tier callers. It answers a lower bound (first a[i] >= x),
|
|
68
|
+
not a segment (upper - 1); there is no segmentHint* export.
|
|
69
|
+
- segment* / nearest*: "which interval holds x" / "which key is closest to x".
|
|
70
|
+
- assertRange / assertSorted: setup and tests only.
|
|
71
|
+
- Call the per-type kernel by name. One call site fed several array types goes megamorphic
|
|
72
|
+
(LEDGER C3). There is no selector and no per-call front that picks a kernel (LEDGER A3, A5).
|
|
73
|
+
|
|
74
|
+
## Usage
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
import { lowerBoundBranchlessF64, segmentF64, nearestF64, assertRange } from "@zakkster/lite-bisect";
|
|
78
|
+
|
|
79
|
+
const a = new Float64Array([1, 3, 3, 5, 8]);
|
|
80
|
+
assertRange(a, 0, 5); // cold, once
|
|
81
|
+
const i = lowerBoundBranchlessF64(a, 0, 5, 3); // -> 1
|
|
82
|
+
const s = segmentF64(a, 0, 5, 4); // -> 2
|
|
83
|
+
const n = nearestF64(a, 0, 5, 4); // -> 2
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Gates (deterministic unless stated)
|
|
87
|
+
|
|
88
|
+
oracle per type (4 seeds), boundary table, mutants (106 rows, all killed), torture with a
|
|
89
|
+
retained-heap gate, c8 100% lines / branches, golden parity of the generated kernels, a TurboFan
|
|
90
|
+
inline pin, tsc type checks (node16 / nodenext / bundler) with an export-parity test, a doc-truth
|
|
91
|
+
test. Tree-shaking is gated under esbuild 0.28 only (one-kernel bundles carry that kernel plus its callees and no other top-level code; sideEffects: false honoured); webpack / rollup not tested. The size / pack gate (npm run size: pack file list + committed budgets) and npm run verify (test + torture + coverage + size + gates:local) are deterministic but outside npm test. The alloc + bytecode gates (npm run gates: the installed @zakkster/lite-utils ^1.1.0 tools; npm run gates:local: a LiteUtils checkout via LITE_UTILS_DIR) run on the pinned Node
|
|
92
|
+
v26.8.2 only, outside npm test, and in hosted CI (ci.yml gates job).
|
|
93
|
+
|
|
94
|
+
## What this is not
|
|
95
|
+
|
|
96
|
+
Not a general search (typed arrays only, sorted, no NaN inside; no plain Array, no comparator).
|
|
97
|
+
Not a sorter. Not a validator on the hot path. Not a polymorphic front (no lowerBound(a, ...)
|
|
98
|
+
that picks a kernel; no length switch, no LINEAR_MAX, no lowerBoundLinear, no Eytzinger layout).
|
|
99
|
+
No benchmark table ships; the only speed words are the STATISTICAL rows of "Which call to choose" (F64, two CPUs, one gated release run).
|
|
100
|
+
|
|
101
|
+
## Links
|
|
102
|
+
|
|
103
|
+
README.md (full reference, evidence and provenance), CHANGELOG.md, Bisect.d.ts.
|
|
104
|
+
|
|
105
|
+
MIT (c) Zahary Shinikchiev <shinikchiev@yahoo.com>
|
package/package.json
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zakkster/lite-bisect",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Zero-GC binary search kernels for sorted typed arrays: branchless and branchy lower / upper bound, a galloping hint search, segment and nearest, one monomorphic copy per array type (Float64, Float32, Uint32, Uint16, Int32).",
|
|
5
|
+
"author": "Zahary Shinikchiev <shinikchiev@yahoo.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/PeshoVurtoleta/lite-bisect.git"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./Bisect.js",
|
|
13
|
+
"module": "./Bisect.js",
|
|
14
|
+
"types": "./Bisect.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./Bisect.d.ts",
|
|
18
|
+
"node": "./Bisect.js",
|
|
19
|
+
"import": "./Bisect.js",
|
|
20
|
+
"default": "./Bisect.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"Bisect.js",
|
|
25
|
+
"Bisect.d.ts",
|
|
26
|
+
"llms.txt",
|
|
27
|
+
"LICENSE.txt",
|
|
28
|
+
"CHANGELOG.md",
|
|
29
|
+
"README.md"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"gen": "node scripts/gen.mjs",
|
|
33
|
+
"gen:check": "node scripts/gen.mjs --check",
|
|
34
|
+
"test": "node --test \"test/*.test.mjs\"",
|
|
35
|
+
"torture": "node --expose-gc --no-concurrent-recompilation --single-threaded-gc --min-semi-space-size=1 --max-semi-space-size=1 test/torture.mjs",
|
|
36
|
+
"gates": "node --test test/gates/alloc.test.mjs test/gates/bytecode.test.mjs",
|
|
37
|
+
"gates:local": "LITE_UTILS_DIR=../LiteUtils npm run gates",
|
|
38
|
+
"test:types": "node --test test/types.test.mjs test/types.exports.test.mjs",
|
|
39
|
+
"size": "node --test test/gates/size.test.mjs",
|
|
40
|
+
"verify": "npm test && npm run torture && npm run coverage && npm run size && npm run gates:local",
|
|
41
|
+
"coverage": "c8 --include=Bisect.js --temp-directory node_modules/.c8/tmp --report-dir node_modules/.c8/report --reporter=text --reporter=text-summary --check-coverage --lines 100 --branches 100 node --test \"test/!(*.inline|*.mutants).test.mjs\""
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"binary-search",
|
|
45
|
+
"bisect",
|
|
46
|
+
"lower-bound",
|
|
47
|
+
"upper-bound",
|
|
48
|
+
"branchless",
|
|
49
|
+
"sorted",
|
|
50
|
+
"typed-array",
|
|
51
|
+
"zero-gc",
|
|
52
|
+
"zero-allocation",
|
|
53
|
+
"zakkster"
|
|
54
|
+
],
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=22"
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": false,
|
|
59
|
+
"dependencies": {},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@zakkster/lite-gc-profiler": "^1.16.0",
|
|
62
|
+
"@zakkster/lite-leak": "^1.10.0",
|
|
63
|
+
"@zakkster/lite-signal": "^1.5.2",
|
|
64
|
+
"@zakkster/lite-utils": "^1.1.0",
|
|
65
|
+
"c8": "^12.0.0",
|
|
66
|
+
"esbuild": "^0.28.2",
|
|
67
|
+
"typescript": "7.0.2"
|
|
68
|
+
}
|
|
69
|
+
}
|