@vltpkg/tar 1.6.1 → 1.6.2
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 +52 -10
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/placement-pool.d.ts +102 -0
- package/dist/placement-pool.js +406 -0
- package/dist/placement-worker.d.ts +72 -0
- package/dist/placement-worker.js +55 -0
- package/dist/pool.d.ts +40 -11
- package/dist/pool.js +61 -13
- package/dist/unpack.d.ts +16 -0
- package/dist/unpack.js +38 -2
- package/package.json +14 -3
package/README.md
CHANGED
|
@@ -51,16 +51,31 @@ straight out of a cache entry whose file starts with a header block.
|
|
|
51
51
|
|
|
52
52
|
### `class Pool`
|
|
53
53
|
|
|
54
|
-
The interface the vlt CLI extracts through.
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
The interface the vlt CLI extracts through. On a large enough install,
|
|
55
|
+
`unpack` hands tarballs to worker threads, which queue them and cap
|
|
56
|
+
the bytes held in memory. Otherwise, and for every other method, the
|
|
57
|
+
work runs on the main thread. See [Unpack workers](#unpack-workers).
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
to cap their own concurrency. (`@vltpkg/graph`'s reify does.)
|
|
59
|
+
#### `pool.unpack(tarData: Buffer, target: string, format?, options?) => Promise<void>`
|
|
60
60
|
|
|
61
|
-
|
|
61
|
+
Unpack the supplied Buffer of data into the target folder. A worker
|
|
62
|
+
gets a copy, so the caller keeps its buffer.
|
|
62
63
|
|
|
63
|
-
|
|
64
|
+
#### `pool.unpackCacheEntry(file: string, target: string, format?) => Promise<void>`
|
|
65
|
+
|
|
66
|
+
Unpack the tarball inside a vlt cache entry file, whose first 4 bytes
|
|
67
|
+
are the big-endian length of the head before the body. On a worker the
|
|
68
|
+
file is read there, so the main thread never reads or copies the body.
|
|
69
|
+
|
|
70
|
+
#### `pool.warm(jobs: number) => Promise<void>`
|
|
71
|
+
|
|
72
|
+
Start the unpack workers for an install about to unpack `jobs`
|
|
73
|
+
tarballs, so they boot before the first one arrives. Starts nothing
|
|
74
|
+
for a small install. Never rejects.
|
|
75
|
+
|
|
76
|
+
#### `pool.close() => Promise<void>`
|
|
77
|
+
|
|
78
|
+
Terminate the unpack workers, if any were started.
|
|
64
79
|
|
|
65
80
|
#### `pool.unpackFile(file: string, target: string, offset = 0) => Promise<void>`
|
|
66
81
|
|
|
@@ -205,6 +220,33 @@ as possible, and that's all.
|
|
|
205
220
|
|
|
206
221
|
Synchronous IO is used because it is faster and costs less CPU than
|
|
207
222
|
the async writers, which pay a libuv round trip per file. The cost is
|
|
208
|
-
that extraction blocks the event loop while it runs
|
|
209
|
-
|
|
210
|
-
kill switch, not a
|
|
223
|
+
that inline extraction blocks the event loop while it runs; the unpack
|
|
224
|
+
workers move that work off the main thread. Set `VLT_TAR_SYNC=0` to
|
|
225
|
+
make `Pool` use the async writers instead -- a kill switch, not a
|
|
226
|
+
supported mode.
|
|
227
|
+
|
|
228
|
+
## Unpack workers
|
|
229
|
+
|
|
230
|
+
`Pool.unpack()` hands tarballs to a `PlacementPool` of worker threads
|
|
231
|
+
(`src/placement-pool.ts`, `src/placement-worker.ts`) once an install
|
|
232
|
+
is large enough to pay for their boot: reify calls `Pool.warm(jobs)`
|
|
233
|
+
with the number of packages the global store does not already hold,
|
|
234
|
+
and a pool nobody warmed starts itself after
|
|
235
|
+
`DEFAULT_PLACEMENT_MIN_JOBS` unpacks. Below that, and with no spare
|
|
236
|
+
core, unpacking runs as before: inflate on libuv's threadpool, write
|
|
237
|
+
on the main thread. Each worker inflates, parses and writes one
|
|
238
|
+
tarball at a time. A downloaded tarball is copied to the worker; a
|
|
239
|
+
cached one is read from its cache file by the worker itself.
|
|
240
|
+
|
|
241
|
+
The pool caps the bytes of downloaded tarballs waiting for a worker
|
|
242
|
+
(`DEFAULT_HELD_BYTES`). The cap applies once a download has finished,
|
|
243
|
+
so it does not bound the downloads in flight; reify's own concurrency
|
|
244
|
+
limit does that.
|
|
245
|
+
|
|
246
|
+
Global store links stay on the main thread. They are a fraction of a
|
|
247
|
+
millisecond per package, and on a two-core machine the worker boot and
|
|
248
|
+
round trips measured 48% slower than linking inline.
|
|
249
|
+
|
|
250
|
+
- `VLT_PLACEMENT_THREADS=<n>`: worker count; `1` places inline.
|
|
251
|
+
- `VLT_TAR_SYNC=0|1`: the older inline strategies; either disables the
|
|
252
|
+
pool.
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export { unpack, unpackToStoreSync } from './unpack.ts';
|
|
|
2
2
|
export type { TarballFormat } from './unpack.ts';
|
|
3
3
|
export { cloneAvailable } from './clonefile.ts';
|
|
4
4
|
export * from './link-tree.ts';
|
|
5
|
+
export * from './placement-pool.ts';
|
|
5
6
|
export * from './pool.ts';
|
|
6
7
|
export * from './store-entry.ts';
|
|
7
8
|
export { readStoreIndex, storeIndexPath } from './store-index.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { unpack, unpackToStoreSync } from "./unpack.js";
|
|
2
2
|
export { cloneAvailable } from "./clonefile.js";
|
|
3
3
|
export * from "./link-tree.js";
|
|
4
|
+
export * from "./placement-pool.js";
|
|
4
5
|
export * from "./pool.js";
|
|
5
6
|
export * from "./store-entry.js";
|
|
6
7
|
export { readStoreIndex, storeIndexPath } from "./store-index.js";
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { TarballFormat } from '@vltpkg/types';
|
|
2
|
+
/**
|
|
3
|
+
* How many placement worker threads to run.
|
|
4
|
+
*
|
|
5
|
+
* `VLT_PLACEMENT_THREADS` overrides it: `1` (and, as a safety net,
|
|
6
|
+
* anything that is not an integer `>= 1`) places inline on the main
|
|
7
|
+
* thread, the behaviour from before this pool existed and a field kill
|
|
8
|
+
* switch.
|
|
9
|
+
*
|
|
10
|
+
* The default is one worker per core, less one for the main thread,
|
|
11
|
+
* capped at 8: past the inflate, placement is `write`/`mkdir` syscalls,
|
|
12
|
+
* whose metadata locks stop rewarding more threads well before the core
|
|
13
|
+
* count on the machines this was measured on. One core (or zero spare)
|
|
14
|
+
* resolves to `1`, i.e. inline.
|
|
15
|
+
*/
|
|
16
|
+
export declare const placementThreads: () => number;
|
|
17
|
+
/**
|
|
18
|
+
* Below this many packages an install places inline. On a small
|
|
19
|
+
* machine (two cores, two threads each) three workers take 60 to 90ms
|
|
20
|
+
* to boot, which a 60-package install cannot earn back: it measured
|
|
21
|
+
* 16% slower with a pool than without. A 400-package install does
|
|
22
|
+
* earn it back.
|
|
23
|
+
*/
|
|
24
|
+
export declare const DEFAULT_PLACEMENT_MIN_JOBS = 100;
|
|
25
|
+
/**
|
|
26
|
+
* How many bytes of downloaded tarballs may wait for an unpack worker.
|
|
27
|
+
* A tarball that would push past the cap waits for room before it is
|
|
28
|
+
* handed over; one larger than the whole cap goes as soon as nothing
|
|
29
|
+
* else is held. The cap applies after the download, so the downloads
|
|
30
|
+
* still in flight are bounded by the caller's concurrency, not by this.
|
|
31
|
+
*/
|
|
32
|
+
export declare const DEFAULT_HELD_BYTES: number;
|
|
33
|
+
/**
|
|
34
|
+
* A pool of worker threads that each unpack tarballs into place. See
|
|
35
|
+
* `placement-worker.ts` for why, and for why store links are not here.
|
|
36
|
+
*
|
|
37
|
+
* Workers are started by `warm`, which reify calls as soon as it
|
|
38
|
+
* knows how many packages it will place, so they boot while the first
|
|
39
|
+
* packages are being fetched rather than on the critical path of the
|
|
40
|
+
* first placement. A pool that was never warmed starts its workers on
|
|
41
|
+
* its own once it has seen `minJobs` placements, so an install that
|
|
42
|
+
* places as it resolves still gets them. Each worker reports when it is
|
|
43
|
+
* ready, and jobs queue until one is. Below `minJobs` packages, `warm`
|
|
44
|
+
* starts nothing and everything stays inline.
|
|
45
|
+
*
|
|
46
|
+
* An idle worker is `unref`d so it does not keep the process alive, and
|
|
47
|
+
* `ref`d again while it holds a job, so the pool needs no explicit
|
|
48
|
+
* `close` to let the process exit -- though reify calls it anyway, to
|
|
49
|
+
* free the threads as soon as placement is done.
|
|
50
|
+
*
|
|
51
|
+
* With a size below 2 (a kill switch, one core, or no spare core) there
|
|
52
|
+
* are no workers at all: placement runs inline, exactly as before.
|
|
53
|
+
*/
|
|
54
|
+
export declare class PlacementPool {
|
|
55
|
+
#private;
|
|
56
|
+
constructor(size?: number, minJobs?: number, heldMax?: number);
|
|
57
|
+
/** True when placement runs inline, with no worker threads. */
|
|
58
|
+
get inline(): boolean;
|
|
59
|
+
/** True once the workers were started and placement goes to them. */
|
|
60
|
+
get threaded(): boolean;
|
|
61
|
+
/** Number of worker threads spawned so far (for tests). */
|
|
62
|
+
get workerCount(): number;
|
|
63
|
+
/** Number of workers that have booted and can take jobs (for tests). */
|
|
64
|
+
get readyCount(): number;
|
|
65
|
+
/** Compressed bytes downloaded and waiting for, or in, an unpack. */
|
|
66
|
+
get heldBytes(): number;
|
|
67
|
+
/**
|
|
68
|
+
* Start the workers for an install that will place `jobs` packages,
|
|
69
|
+
* so they boot now rather than when the first placement arrives.
|
|
70
|
+
* Starts nothing, leaving placement inline, when the pool is inline
|
|
71
|
+
* or closed, when it was already warmed, or when `jobs` is below the
|
|
72
|
+
* minimum. Resolves once every worker it started is ready or gone; it
|
|
73
|
+
* never rejects, so a caller may fire and forget it.
|
|
74
|
+
*/
|
|
75
|
+
warm(jobs: number): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Unpack `tarData` into `target`: on a worker once the pool is
|
|
78
|
+
* threaded, inline otherwise (inflating on libuv's threadpool and
|
|
79
|
+
* writing on the main thread, as before this pool existed). Resolves
|
|
80
|
+
* when the files are in place.
|
|
81
|
+
*
|
|
82
|
+
* The bytes are copied before they go to a worker: the caller may
|
|
83
|
+
* still hold them (the registry client caches its response bodies),
|
|
84
|
+
* and a transfer would detach them for everyone. For a tarball that
|
|
85
|
+
* is already on disk, {@link unpackCacheEntry} avoids the copy.
|
|
86
|
+
*/
|
|
87
|
+
unpack(tarData: Buffer, target: string, format?: TarballFormat): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Unpack the tarball in the vlt cache entry `file` into `target`. A
|
|
90
|
+
* worker reads the file itself, so the main thread neither reads nor
|
|
91
|
+
* copies the body, and nothing is held against the byte cap. Inline
|
|
92
|
+
* when the pool is not threaded, like {@link unpack}.
|
|
93
|
+
*/
|
|
94
|
+
unpackCacheEntry(file: string, target: string, format?: TarballFormat): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Terminate every worker. Safe to call more than once. Any placement
|
|
97
|
+
* still assigned or queued is rejected rather than left hanging, since
|
|
98
|
+
* terminating a worker stops it before it can reply, and a `warm()`
|
|
99
|
+
* still waiting on the boot is released.
|
|
100
|
+
*/
|
|
101
|
+
close(): Promise<void>;
|
|
102
|
+
}
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { availableParallelism } from 'node:os';
|
|
2
|
+
import { Worker } from 'node:worker_threads';
|
|
3
|
+
import { lane, trace } from '@vltpkg/output/trace';
|
|
4
|
+
import { __CODE_SPLIT_SCRIPT_NAME } from "./placement-worker.js";
|
|
5
|
+
import { unpackCacheEntryParallel, unpackParallel } from "./unpack.js";
|
|
6
|
+
/**
|
|
7
|
+
* How many placement worker threads to run.
|
|
8
|
+
*
|
|
9
|
+
* `VLT_PLACEMENT_THREADS` overrides it: `1` (and, as a safety net,
|
|
10
|
+
* anything that is not an integer `>= 1`) places inline on the main
|
|
11
|
+
* thread, the behaviour from before this pool existed and a field kill
|
|
12
|
+
* switch.
|
|
13
|
+
*
|
|
14
|
+
* The default is one worker per core, less one for the main thread,
|
|
15
|
+
* capped at 8: past the inflate, placement is `write`/`mkdir` syscalls,
|
|
16
|
+
* whose metadata locks stop rewarding more threads well before the core
|
|
17
|
+
* count on the machines this was measured on. One core (or zero spare)
|
|
18
|
+
* resolves to `1`, i.e. inline.
|
|
19
|
+
*/
|
|
20
|
+
export const placementThreads = () => {
|
|
21
|
+
const env = process.env.VLT_PLACEMENT_THREADS;
|
|
22
|
+
if (env !== undefined && env !== '') {
|
|
23
|
+
const n = Number(env);
|
|
24
|
+
return Number.isInteger(n) && n >= 1 ? n : 1;
|
|
25
|
+
}
|
|
26
|
+
return Math.min(Math.max(availableParallelism() - 1, 1), 8);
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Below this many packages an install places inline. On a small
|
|
30
|
+
* machine (two cores, two threads each) three workers take 60 to 90ms
|
|
31
|
+
* to boot, which a 60-package install cannot earn back: it measured
|
|
32
|
+
* 16% slower with a pool than without. A 400-package install does
|
|
33
|
+
* earn it back.
|
|
34
|
+
*/
|
|
35
|
+
export const DEFAULT_PLACEMENT_MIN_JOBS = 100;
|
|
36
|
+
/**
|
|
37
|
+
* How many bytes of downloaded tarballs may wait for an unpack worker.
|
|
38
|
+
* A tarball that would push past the cap waits for room before it is
|
|
39
|
+
* handed over; one larger than the whole cap goes as soon as nothing
|
|
40
|
+
* else is held. The cap applies after the download, so the downloads
|
|
41
|
+
* still in flight are bounded by the caller's concurrency, not by this.
|
|
42
|
+
*/
|
|
43
|
+
export const DEFAULT_HELD_BYTES = 64 * 1024 * 1024;
|
|
44
|
+
/**
|
|
45
|
+
* A pool of worker threads that each unpack tarballs into place. See
|
|
46
|
+
* `placement-worker.ts` for why, and for why store links are not here.
|
|
47
|
+
*
|
|
48
|
+
* Workers are started by `warm`, which reify calls as soon as it
|
|
49
|
+
* knows how many packages it will place, so they boot while the first
|
|
50
|
+
* packages are being fetched rather than on the critical path of the
|
|
51
|
+
* first placement. A pool that was never warmed starts its workers on
|
|
52
|
+
* its own once it has seen `minJobs` placements, so an install that
|
|
53
|
+
* places as it resolves still gets them. Each worker reports when it is
|
|
54
|
+
* ready, and jobs queue until one is. Below `minJobs` packages, `warm`
|
|
55
|
+
* starts nothing and everything stays inline.
|
|
56
|
+
*
|
|
57
|
+
* An idle worker is `unref`d so it does not keep the process alive, and
|
|
58
|
+
* `ref`d again while it holds a job, so the pool needs no explicit
|
|
59
|
+
* `close` to let the process exit -- though reify calls it anyway, to
|
|
60
|
+
* free the threads as soon as placement is done.
|
|
61
|
+
*
|
|
62
|
+
* With a size below 2 (a kill switch, one core, or no spare core) there
|
|
63
|
+
* are no workers at all: placement runs inline, exactly as before.
|
|
64
|
+
*/
|
|
65
|
+
export class PlacementPool {
|
|
66
|
+
#size;
|
|
67
|
+
#minJobs;
|
|
68
|
+
#heldMax;
|
|
69
|
+
#workers = [];
|
|
70
|
+
#ready = new Set();
|
|
71
|
+
#idle = [];
|
|
72
|
+
#queue = [];
|
|
73
|
+
#pending = new Map();
|
|
74
|
+
#seq = 0;
|
|
75
|
+
#seen = 0;
|
|
76
|
+
#started = false;
|
|
77
|
+
#threaded = false;
|
|
78
|
+
#closed = false;
|
|
79
|
+
#booting = [];
|
|
80
|
+
#held = 0;
|
|
81
|
+
#room = [];
|
|
82
|
+
#busy = 0;
|
|
83
|
+
#queued = 0;
|
|
84
|
+
constructor(size = placementThreads(), minJobs = DEFAULT_PLACEMENT_MIN_JOBS, heldMax = DEFAULT_HELD_BYTES) {
|
|
85
|
+
this.#size = size;
|
|
86
|
+
this.#minJobs = minJobs;
|
|
87
|
+
this.#heldMax = heldMax;
|
|
88
|
+
}
|
|
89
|
+
/** True when placement runs inline, with no worker threads. */
|
|
90
|
+
get inline() {
|
|
91
|
+
return this.#size < 2;
|
|
92
|
+
}
|
|
93
|
+
/** True once the workers were started and placement goes to them. */
|
|
94
|
+
get threaded() {
|
|
95
|
+
return this.#threaded;
|
|
96
|
+
}
|
|
97
|
+
// Whether a placement arriving now runs on the main thread: no
|
|
98
|
+
// workers at all, closed, or not (or no longer) threaded. A method,
|
|
99
|
+
// not a getter, so a re-check after an await is not narrowed away.
|
|
100
|
+
#inlineNow() {
|
|
101
|
+
return this.inline || this.#closed || !this.#threaded;
|
|
102
|
+
}
|
|
103
|
+
/** Number of worker threads spawned so far (for tests). */
|
|
104
|
+
get workerCount() {
|
|
105
|
+
return this.#workers.length;
|
|
106
|
+
}
|
|
107
|
+
/** Number of workers that have booted and can take jobs (for tests). */
|
|
108
|
+
get readyCount() {
|
|
109
|
+
return this.#ready.size;
|
|
110
|
+
}
|
|
111
|
+
/** Compressed bytes downloaded and waiting for, or in, an unpack. */
|
|
112
|
+
get heldBytes() {
|
|
113
|
+
return this.#held;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Start the workers for an install that will place `jobs` packages,
|
|
117
|
+
* so they boot now rather than when the first placement arrives.
|
|
118
|
+
* Starts nothing, leaving placement inline, when the pool is inline
|
|
119
|
+
* or closed, when it was already warmed, or when `jobs` is below the
|
|
120
|
+
* minimum. Resolves once every worker it started is ready or gone; it
|
|
121
|
+
* never rejects, so a caller may fire and forget it.
|
|
122
|
+
*/
|
|
123
|
+
async warm(jobs) {
|
|
124
|
+
if (this.inline ||
|
|
125
|
+
this.#closed ||
|
|
126
|
+
this.#threaded ||
|
|
127
|
+
jobs < this.#minJobs) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
this.#threaded = true;
|
|
131
|
+
this.#started = true;
|
|
132
|
+
try {
|
|
133
|
+
while (this.#workers.length < this.#size)
|
|
134
|
+
this.#spawn();
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
// `new Worker` can throw, for example when the system is out of
|
|
138
|
+
// threads. Keep the workers that did start. With none, placement
|
|
139
|
+
// stays inline, since queued jobs would wait for a worker forever.
|
|
140
|
+
if (!this.#workers.length) {
|
|
141
|
+
this.#threaded = false;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
await new Promise(resolve => {
|
|
146
|
+
this.#booting.push(resolve);
|
|
147
|
+
this.#checkBooted();
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
// A pool nobody warmed starts itself once enough placements have
|
|
151
|
+
// come through to pay for the boot. The placements before that ran
|
|
152
|
+
// inline; the ones from here on queue for the workers. Workers that
|
|
153
|
+
// were started once and all died are not started again: the pool
|
|
154
|
+
// stays inline, as it would after a `warm`.
|
|
155
|
+
#maybeAutoWarm() {
|
|
156
|
+
if (this.#started || this.#closed || this.inline)
|
|
157
|
+
return;
|
|
158
|
+
if (++this.#seen < this.#minJobs)
|
|
159
|
+
return;
|
|
160
|
+
void this.warm(this.#seen);
|
|
161
|
+
}
|
|
162
|
+
// Every live worker is ready, or none is left: settle any warm() that
|
|
163
|
+
// is waiting on the boot.
|
|
164
|
+
#checkBooted() {
|
|
165
|
+
if (this.#ready.size < this.#workers.length)
|
|
166
|
+
return;
|
|
167
|
+
const waiters = this.#booting;
|
|
168
|
+
this.#booting = [];
|
|
169
|
+
for (const resolve of waiters)
|
|
170
|
+
resolve();
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Unpack `tarData` into `target`: on a worker once the pool is
|
|
174
|
+
* threaded, inline otherwise (inflating on libuv's threadpool and
|
|
175
|
+
* writing on the main thread, as before this pool existed). Resolves
|
|
176
|
+
* when the files are in place.
|
|
177
|
+
*
|
|
178
|
+
* The bytes are copied before they go to a worker: the caller may
|
|
179
|
+
* still hold them (the registry client caches its response bodies),
|
|
180
|
+
* and a transfer would detach them for everyone. For a tarball that
|
|
181
|
+
* is already on disk, {@link unpackCacheEntry} avoids the copy.
|
|
182
|
+
*/
|
|
183
|
+
async unpack(tarData, target, format) {
|
|
184
|
+
this.#maybeAutoWarm();
|
|
185
|
+
if (this.#inlineNow())
|
|
186
|
+
return unpackParallel(tarData, target, format);
|
|
187
|
+
const bytes = tarData.byteLength;
|
|
188
|
+
await this.#reserve(bytes);
|
|
189
|
+
// the pool may have closed, or lost its last worker, while this
|
|
190
|
+
// waited for room: it is inline now, so place inline
|
|
191
|
+
if (this.#inlineNow()) {
|
|
192
|
+
this.#release(bytes);
|
|
193
|
+
return unpackParallel(tarData, target, format);
|
|
194
|
+
}
|
|
195
|
+
const copy = new Uint8Array(bytes);
|
|
196
|
+
copy.set(tarData);
|
|
197
|
+
await this.#submit({
|
|
198
|
+
kind: 'unpack',
|
|
199
|
+
id: this.#seq++,
|
|
200
|
+
buffer: copy.buffer,
|
|
201
|
+
byteOffset: 0,
|
|
202
|
+
byteLength: bytes,
|
|
203
|
+
target,
|
|
204
|
+
format,
|
|
205
|
+
}, bytes);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Unpack the tarball in the vlt cache entry `file` into `target`. A
|
|
209
|
+
* worker reads the file itself, so the main thread neither reads nor
|
|
210
|
+
* copies the body, and nothing is held against the byte cap. Inline
|
|
211
|
+
* when the pool is not threaded, like {@link unpack}.
|
|
212
|
+
*/
|
|
213
|
+
async unpackCacheEntry(file, target, format) {
|
|
214
|
+
this.#maybeAutoWarm();
|
|
215
|
+
if (this.#inlineNow()) {
|
|
216
|
+
return unpackCacheEntryParallel(file, target, format);
|
|
217
|
+
}
|
|
218
|
+
await this.#submit({ kind: 'cacheEntry', id: this.#seq++, file, target, format }, 0);
|
|
219
|
+
}
|
|
220
|
+
// Wait until `bytes` more may be held. Nothing waits while nothing is
|
|
221
|
+
// held, so one oversized tarball never deadlocks.
|
|
222
|
+
async #reserve(bytes) {
|
|
223
|
+
while (this.#held > 0 && this.#held + bytes > this.#heldMax) {
|
|
224
|
+
await new Promise(resolve => this.#room.push(resolve));
|
|
225
|
+
}
|
|
226
|
+
this.#held += bytes;
|
|
227
|
+
}
|
|
228
|
+
#release(bytes) {
|
|
229
|
+
this.#held -= bytes;
|
|
230
|
+
// wake everyone; each re-checks and queues again if there is still
|
|
231
|
+
// no room, which keeps the order of arrival
|
|
232
|
+
const waiters = this.#room;
|
|
233
|
+
this.#room = [];
|
|
234
|
+
for (const resolve of waiters)
|
|
235
|
+
resolve();
|
|
236
|
+
}
|
|
237
|
+
// Queue the job, or hand it to a free worker. A ready worker is free:
|
|
238
|
+
// use it. Otherwise queue; a worker takes the next queued job as it
|
|
239
|
+
// finishes, or as it finishes booting. Placing inline while the
|
|
240
|
+
// workers boot was measured and rejected: on a machine with few cores
|
|
241
|
+
// the main thread's placement contends with the boots, and the install
|
|
242
|
+
// comes out slower than either.
|
|
243
|
+
#submit(job, bytes) {
|
|
244
|
+
return new Promise((resolve, reject) => {
|
|
245
|
+
this.#pending.set(job.id, { bytes, resolve, reject });
|
|
246
|
+
const worker = this.#idle.pop();
|
|
247
|
+
if (worker)
|
|
248
|
+
this.#assign(worker, job);
|
|
249
|
+
else {
|
|
250
|
+
this.#queue.push(job);
|
|
251
|
+
this.#queued++;
|
|
252
|
+
this.#trace();
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
#trace() {
|
|
257
|
+
lane('unpack', this.#busy, this.#queued);
|
|
258
|
+
}
|
|
259
|
+
#spawn() {
|
|
260
|
+
trace('pool:spawn', { workers: this.#workers.length + 1 });
|
|
261
|
+
const worker = new Worker(__CODE_SPLIT_SCRIPT_NAME);
|
|
262
|
+
worker.on('message', (reply) => this.#onMessage(worker, reply));
|
|
263
|
+
// a worker can die two ways: 'error' for an uncaught exception, and
|
|
264
|
+
// 'exit' for anything else (a process.exit, an OOM kill). Both are
|
|
265
|
+
// terminal and both reach #onLost, which ignores the second.
|
|
266
|
+
worker.on('error', (er) => this.#onLost(worker, er));
|
|
267
|
+
worker.on('exit', () => this.#onLost(worker, new Error('placement worker exited')));
|
|
268
|
+
// A worker starts ref'd: jobs queue while it boots, and with nothing
|
|
269
|
+
// else keeping the event loop alive (no reporter timer, as with
|
|
270
|
+
// --view=silent) an unref'd boot would let the process exit with the
|
|
271
|
+
// install still awaiting them. It unrefs itself once it is ready and
|
|
272
|
+
// idle (see #park), so an idle pool never keeps the process alive.
|
|
273
|
+
this.#workers.push(worker);
|
|
274
|
+
}
|
|
275
|
+
#assign(worker, job) {
|
|
276
|
+
// a job is only ever assigned while its pending entry is live
|
|
277
|
+
const pending = this.#pending.get(job.id);
|
|
278
|
+
/* c8 ignore next */
|
|
279
|
+
if (pending)
|
|
280
|
+
pending.worker = worker;
|
|
281
|
+
this.#busy++;
|
|
282
|
+
this.#trace();
|
|
283
|
+
// keep the process alive while this worker has a job in flight
|
|
284
|
+
worker.ref();
|
|
285
|
+
// the tarball bytes, if any, are transferred, not copied again
|
|
286
|
+
worker.postMessage(job, job.kind === 'unpack' ? [job.buffer] : []);
|
|
287
|
+
}
|
|
288
|
+
// Hand the worker the next queued job, or park it idle.
|
|
289
|
+
#park(worker) {
|
|
290
|
+
const next = this.#queue.shift();
|
|
291
|
+
if (next) {
|
|
292
|
+
this.#queued--;
|
|
293
|
+
this.#assign(worker, next);
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
worker.unref();
|
|
297
|
+
this.#idle.push(worker);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
#settle(id, pending) {
|
|
301
|
+
// a queued job was never busy; an assigned one was
|
|
302
|
+
if (pending.worker) {
|
|
303
|
+
this.#busy--;
|
|
304
|
+
this.#trace();
|
|
305
|
+
}
|
|
306
|
+
this.#release(pending.bytes);
|
|
307
|
+
this.#pending.delete(id);
|
|
308
|
+
}
|
|
309
|
+
#onMessage(worker, reply) {
|
|
310
|
+
if ('ready' in reply) {
|
|
311
|
+
// the worker has booted: it can take queued work, and warm() may
|
|
312
|
+
// now have everything it was waiting for
|
|
313
|
+
this.#ready.add(worker);
|
|
314
|
+
trace('pool:ready', { ready: this.#ready.size });
|
|
315
|
+
this.#park(worker);
|
|
316
|
+
this.#checkBooted();
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const pending = this.#pending.get(reply.id);
|
|
320
|
+
/* c8 ignore next - a reply always matches a live pending entry */
|
|
321
|
+
if (pending)
|
|
322
|
+
this.#settle(reply.id, pending);
|
|
323
|
+
// give the worker its next job before settling, so throughput does
|
|
324
|
+
// not wait on the caller's continuation
|
|
325
|
+
this.#park(worker);
|
|
326
|
+
/* c8 ignore next */
|
|
327
|
+
if (!pending)
|
|
328
|
+
return;
|
|
329
|
+
if ('result' in reply)
|
|
330
|
+
pending.resolve();
|
|
331
|
+
else {
|
|
332
|
+
const er = new Error(reply.error);
|
|
333
|
+
er.stack = reply.stack ?? er.stack;
|
|
334
|
+
if (reply.code !== undefined)
|
|
335
|
+
er.code = reply.code;
|
|
336
|
+
pending.reject(er);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
// A worker died outright, not a job that threw (that comes back as a
|
|
340
|
+
// reply). Drop it, reject the one job it was running, and keep the
|
|
341
|
+
// queue moving so no placement promise is left hanging. 'error' and
|
|
342
|
+
// 'exit' both land here, so the first call drops the worker and the
|
|
343
|
+
// second returns.
|
|
344
|
+
#onLost(worker, er) {
|
|
345
|
+
if (!this.#workers.includes(worker))
|
|
346
|
+
return;
|
|
347
|
+
this.#workers = this.#workers.filter(w => w !== worker);
|
|
348
|
+
this.#idle = this.#idle.filter(w => w !== worker);
|
|
349
|
+
this.#ready.delete(worker);
|
|
350
|
+
for (const [id, pending] of this.#pending) {
|
|
351
|
+
if (pending.worker === worker) {
|
|
352
|
+
this.#settle(id, pending);
|
|
353
|
+
pending.reject(er);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
// a worker lost during boot may be the one warm() was waiting on
|
|
357
|
+
this.#checkBooted();
|
|
358
|
+
// A surviving worker drains the queue as it finishes or boots (see
|
|
359
|
+
// #park), and a worker is only idle when the queue is empty, so
|
|
360
|
+
// queued jobs are never stranded while any worker remains -- and
|
|
361
|
+
// nothing is spawned here, where a failed `new Worker` would escape
|
|
362
|
+
// this event handler. With no workers left nothing will ever run
|
|
363
|
+
// the queue, so reject it rather than let those promises hang, and
|
|
364
|
+
// place inline from here on rather than queue for workers that will
|
|
365
|
+
// never come.
|
|
366
|
+
if (this.#workers.length)
|
|
367
|
+
return;
|
|
368
|
+
this.#threaded = false;
|
|
369
|
+
this.#rejectQueued(er);
|
|
370
|
+
}
|
|
371
|
+
#rejectQueued(er) {
|
|
372
|
+
const queued = this.#queue;
|
|
373
|
+
this.#queue = [];
|
|
374
|
+
for (const job of queued) {
|
|
375
|
+
this.#queued--;
|
|
376
|
+
const pending = this.#pending.get(job.id);
|
|
377
|
+
/* c8 ignore next */
|
|
378
|
+
if (pending) {
|
|
379
|
+
this.#settle(job.id, pending);
|
|
380
|
+
pending.reject(er);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Terminate every worker. Safe to call more than once. Any placement
|
|
386
|
+
* still assigned or queued is rejected rather than left hanging, since
|
|
387
|
+
* terminating a worker stops it before it can reply, and a `warm()`
|
|
388
|
+
* still waiting on the boot is released.
|
|
389
|
+
*/
|
|
390
|
+
async close() {
|
|
391
|
+
this.#closed = true;
|
|
392
|
+
this.#threaded = false;
|
|
393
|
+
const workers = this.#workers;
|
|
394
|
+
this.#workers = [];
|
|
395
|
+
this.#idle = [];
|
|
396
|
+
this.#ready.clear();
|
|
397
|
+
const er = new Error('placement pool closed');
|
|
398
|
+
this.#rejectQueued(er);
|
|
399
|
+
for (const [id, pending] of this.#pending) {
|
|
400
|
+
this.#settle(id, pending);
|
|
401
|
+
pending.reject(er);
|
|
402
|
+
}
|
|
403
|
+
this.#checkBooted();
|
|
404
|
+
await Promise.all(workers.map(w => w.terminate()));
|
|
405
|
+
}
|
|
406
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { TarballFormat } from '@vltpkg/types';
|
|
2
|
+
/**
|
|
3
|
+
* Unpacking a tarball is synchronous work the main thread cannot
|
|
4
|
+
* afford: inflating it, parsing it and writing its files. While the
|
|
5
|
+
* main thread does that, no registry response is read, no manifest is
|
|
6
|
+
* parsed and no node is placed in the graph. A `PlacementPool` runs
|
|
7
|
+
* this script as a worker thread so several packages are unpacked at
|
|
8
|
+
* once, across cores, and the main thread stays free to keep the
|
|
9
|
+
* network busy.
|
|
10
|
+
*
|
|
11
|
+
* Hardlinking a global store entry stays on the main thread: it is a
|
|
12
|
+
* fraction of a millisecond per package, and on a two-core machine the
|
|
13
|
+
* boot of the workers and the round trips never paid for themselves
|
|
14
|
+
* (measured: +48% on the placement phase of a warm install).
|
|
15
|
+
*
|
|
16
|
+
* The build emits it as its own bundled file: the exported
|
|
17
|
+
* {@link __CODE_SPLIT_SCRIPT_NAME} is the path to spawn (the source
|
|
18
|
+
* `.ts` in dev and test, the built `.js` in the bundle), and
|
|
19
|
+
* {@link handleJob} is the per-message handler, exported so it can be
|
|
20
|
+
* tested without a live worker.
|
|
21
|
+
*/
|
|
22
|
+
export declare const __CODE_SPLIT_SCRIPT_NAME: string;
|
|
23
|
+
/**
|
|
24
|
+
* Unpack a tarball into `target`. The bytes arrive as an `ArrayBuffer`
|
|
25
|
+
* transferred from the main thread.
|
|
26
|
+
*/
|
|
27
|
+
export type UnpackJob = {
|
|
28
|
+
kind: 'unpack';
|
|
29
|
+
id: number;
|
|
30
|
+
buffer: ArrayBuffer;
|
|
31
|
+
byteOffset: number;
|
|
32
|
+
byteLength: number;
|
|
33
|
+
target: string;
|
|
34
|
+
format?: TarballFormat;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Unpack the tarball in a vlt cache entry file into `target`. The
|
|
38
|
+
* worker reads the file itself, so the main thread never holds or
|
|
39
|
+
* copies the body.
|
|
40
|
+
*/
|
|
41
|
+
export type CacheEntryJob = {
|
|
42
|
+
kind: 'cacheEntry';
|
|
43
|
+
id: number;
|
|
44
|
+
file: string;
|
|
45
|
+
target: string;
|
|
46
|
+
format?: TarballFormat;
|
|
47
|
+
};
|
|
48
|
+
export type PlacementJob = UnpackJob | CacheEntryJob;
|
|
49
|
+
/**
|
|
50
|
+
* A worker's reply for job `id`: `true` once unpacked, or the message,
|
|
51
|
+
* stack and errno code of the error thrown, which the pool rebuilds
|
|
52
|
+
* into an Error so a worker failure looks like the inline call failing.
|
|
53
|
+
*/
|
|
54
|
+
export type PlacementReply = {
|
|
55
|
+
id: number;
|
|
56
|
+
result: true;
|
|
57
|
+
} | {
|
|
58
|
+
id: number;
|
|
59
|
+
error: string;
|
|
60
|
+
stack?: string;
|
|
61
|
+
code?: string;
|
|
62
|
+
}
|
|
63
|
+
/** posted once, when the worker has booted and can take jobs */
|
|
64
|
+
| {
|
|
65
|
+
ready: true;
|
|
66
|
+
};
|
|
67
|
+
/** A port that takes a {@link PlacementReply}; the real one is `parentPort`. */
|
|
68
|
+
export type ReplyPort = {
|
|
69
|
+
postMessage: (reply: PlacementReply) => void;
|
|
70
|
+
};
|
|
71
|
+
/** Run one job and post the result (or its error) back. */
|
|
72
|
+
export declare const handleJob: (port: ReplyPort, job: PlacementJob) => void;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { isMainThread, parentPort } from 'node:worker_threads';
|
|
2
|
+
import { unpackCacheEntrySync, unpackSync } from "./unpack.js";
|
|
3
|
+
/**
|
|
4
|
+
* Unpacking a tarball is synchronous work the main thread cannot
|
|
5
|
+
* afford: inflating it, parsing it and writing its files. While the
|
|
6
|
+
* main thread does that, no registry response is read, no manifest is
|
|
7
|
+
* parsed and no node is placed in the graph. A `PlacementPool` runs
|
|
8
|
+
* this script as a worker thread so several packages are unpacked at
|
|
9
|
+
* once, across cores, and the main thread stays free to keep the
|
|
10
|
+
* network busy.
|
|
11
|
+
*
|
|
12
|
+
* Hardlinking a global store entry stays on the main thread: it is a
|
|
13
|
+
* fraction of a millisecond per package, and on a two-core machine the
|
|
14
|
+
* boot of the workers and the round trips never paid for themselves
|
|
15
|
+
* (measured: +48% on the placement phase of a warm install).
|
|
16
|
+
*
|
|
17
|
+
* The build emits it as its own bundled file: the exported
|
|
18
|
+
* {@link __CODE_SPLIT_SCRIPT_NAME} is the path to spawn (the source
|
|
19
|
+
* `.ts` in dev and test, the built `.js` in the bundle), and
|
|
20
|
+
* {@link handleJob} is the per-message handler, exported so it can be
|
|
21
|
+
* tested without a live worker.
|
|
22
|
+
*/
|
|
23
|
+
export const __CODE_SPLIT_SCRIPT_NAME = import.meta.filename;
|
|
24
|
+
/** Run one job and post the result (or its error) back. */
|
|
25
|
+
export const handleJob = (port, job) => {
|
|
26
|
+
try {
|
|
27
|
+
if (job.kind === 'cacheEntry') {
|
|
28
|
+
unpackCacheEntrySync(job.file, job.target, job.format);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
unpackSync(Buffer.from(job.buffer, job.byteOffset, job.byteLength), job.target, job.format);
|
|
32
|
+
}
|
|
33
|
+
port.postMessage({ id: job.id, result: true });
|
|
34
|
+
}
|
|
35
|
+
catch (er) {
|
|
36
|
+
// a non-Error can be thrown, so narrow before reading its fields
|
|
37
|
+
const e = er instanceof Error ? er : undefined;
|
|
38
|
+
port.postMessage({
|
|
39
|
+
id: job.id,
|
|
40
|
+
error: e?.message ?? String(er),
|
|
41
|
+
stack: e?.stack,
|
|
42
|
+
code: e?.code,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
// Runs only when spawned as a worker thread; on the main thread this
|
|
47
|
+
// module is imported just for the exports above.
|
|
48
|
+
/* c8 ignore start - covered by spawning a real worker in the tests */
|
|
49
|
+
if (!isMainThread && parentPort) {
|
|
50
|
+
const port = parentPort;
|
|
51
|
+
port.on('message', (job) => handleJob(port, job));
|
|
52
|
+
// tell the pool this worker can take jobs; until then they queue
|
|
53
|
+
port.postMessage({ ready: true });
|
|
54
|
+
}
|
|
55
|
+
/* c8 ignore stop */
|
package/dist/pool.d.ts
CHANGED
|
@@ -4,24 +4,39 @@ import type { TarballFormat } from '@vltpkg/types';
|
|
|
4
4
|
/**
|
|
5
5
|
* Unpacks tarballs into place.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
7
|
+
* Unpacking -- inflating, parsing and writing a tarball -- runs on a
|
|
8
|
+
* {@link PlacementPool} of worker threads once an install is large
|
|
9
|
+
* enough to pay for their boot (see `warm`), so several packages are
|
|
10
|
+
* unpacked at once and the main thread stays free for the network and
|
|
11
|
+
* the graph. Linking a global store entry stays on the main thread; see
|
|
12
|
+
* `placement-worker.ts` for the measurement behind that. Until then, and on a
|
|
13
|
+
* machine with no spare core, it runs as it did before the pool
|
|
14
|
+
* existed: decompression on libuv's threadpool, the files written
|
|
15
|
+
* synchronously on the main thread, which is measurably faster than the
|
|
16
|
+
* async writers (the libuv round trip per file costs more than the IO).
|
|
17
|
+
* `VLT_TAR_SYNC` switches that pairing and disables the pool; see the
|
|
18
|
+
* constant above.
|
|
12
19
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* lower and elsewhere, by `UV_THREADPOOL_SIZE` (4 by default).
|
|
20
|
+
* The pool queues placements for its workers and caps the compressed
|
|
21
|
+
* bytes held between download and unpack. The caller still caps how
|
|
22
|
+
* many extractions it has in flight: reify does, in
|
|
23
|
+
* `@vltpkg/graph` `src/reify/index.ts`.
|
|
18
24
|
*/
|
|
19
25
|
export declare class Pool {
|
|
26
|
+
#private;
|
|
20
27
|
/**
|
|
21
28
|
* Provide the tardata to be unpacked, and the location where it's to be
|
|
22
|
-
* placed. Resolves when the tarball has been extracted.
|
|
29
|
+
* placed. Resolves when the tarball has been extracted. See
|
|
30
|
+
* {@link PlacementPool.unpack}.
|
|
23
31
|
*/
|
|
24
32
|
unpack(tarData: Buffer, target: string, format?: TarballFormat): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Unpack the tarball in the vlt cache entry `file`, whose first 4
|
|
35
|
+
* bytes are the big-endian length of the head before the body. On a
|
|
36
|
+
* worker the file is read there, so the main thread never holds the
|
|
37
|
+
* body. See {@link PlacementPool.unpackCacheEntry}.
|
|
38
|
+
*/
|
|
39
|
+
unpackCacheEntry(file: string, target: string, format?: TarballFormat): Promise<void>;
|
|
25
40
|
/**
|
|
26
41
|
* Same, reading the tarball from a file and skipping `offset` leading
|
|
27
42
|
* bytes, so a cache entry is unpacked without loading it into the
|
|
@@ -34,6 +49,20 @@ export declare class Pool {
|
|
|
34
49
|
* {@link linkFromStore}.
|
|
35
50
|
*/
|
|
36
51
|
linkFromStore(storeEntry: string, target: string, opts?: LinkFromStoreOptions): Promise<StoreLinkResult>;
|
|
52
|
+
/**
|
|
53
|
+
* Start the unpack workers for an install about to unpack `jobs`
|
|
54
|
+
* tarballs, so they boot while the first packages are fetched rather
|
|
55
|
+
* than on the first placement. Never rejects. See
|
|
56
|
+
* {@link PlacementPool.warm}.
|
|
57
|
+
*/
|
|
58
|
+
warm(jobs: number): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Terminate the placement worker threads, if any were started. The
|
|
61
|
+
* pool works without this -- idle workers do not keep the process
|
|
62
|
+
* alive -- but reify calls it when an install's placement is done, to
|
|
63
|
+
* free the threads promptly.
|
|
64
|
+
*/
|
|
65
|
+
close(): Promise<void>;
|
|
37
66
|
/**
|
|
38
67
|
* Explode a tarball into `dir` for the global store. See
|
|
39
68
|
* {@link unpackToStoreSync}.
|
package/dist/pool.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { linkFromStore } from "./link-tree.js";
|
|
3
|
-
import {
|
|
3
|
+
import { PlacementPool } from "./placement-pool.js";
|
|
4
|
+
import { cacheEntryBody, unpack, unpackCacheEntrySync, unpackFileParallel, unpackFileSync, unpackSync, unpackToStoreSync, } from "./unpack.js";
|
|
4
5
|
/**
|
|
5
6
|
* Which decompress/write pair {@link Pool} uses, from `VLT_TAR_SYNC`.
|
|
6
7
|
* Both explicit values are field kill switches: either restores an
|
|
@@ -18,29 +19,55 @@ const mode = process.env.VLT_TAR_SYNC;
|
|
|
18
19
|
/**
|
|
19
20
|
* Unpacks tarballs into place.
|
|
20
21
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
22
|
+
* Unpacking -- inflating, parsing and writing a tarball -- runs on a
|
|
23
|
+
* {@link PlacementPool} of worker threads once an install is large
|
|
24
|
+
* enough to pay for their boot (see `warm`), so several packages are
|
|
25
|
+
* unpacked at once and the main thread stays free for the network and
|
|
26
|
+
* the graph. Linking a global store entry stays on the main thread; see
|
|
27
|
+
* `placement-worker.ts` for the measurement behind that. Until then, and on a
|
|
28
|
+
* machine with no spare core, it runs as it did before the pool
|
|
29
|
+
* existed: decompression on libuv's threadpool, the files written
|
|
30
|
+
* synchronously on the main thread, which is measurably faster than the
|
|
31
|
+
* async writers (the libuv round trip per file costs more than the IO).
|
|
32
|
+
* `VLT_TAR_SYNC` switches that pairing and disables the pool; see the
|
|
33
|
+
* constant above.
|
|
26
34
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* lower and elsewhere, by `UV_THREADPOOL_SIZE` (4 by default).
|
|
35
|
+
* The pool queues placements for its workers and caps the compressed
|
|
36
|
+
* bytes held between download and unpack. The caller still caps how
|
|
37
|
+
* many extractions it has in flight: reify does, in
|
|
38
|
+
* `@vltpkg/graph` `src/reify/index.ts`.
|
|
32
39
|
*/
|
|
33
40
|
export class Pool {
|
|
41
|
+
#placement;
|
|
42
|
+
get #pool() {
|
|
43
|
+
this.#placement ??= new PlacementPool();
|
|
44
|
+
return this.#placement;
|
|
45
|
+
}
|
|
34
46
|
/**
|
|
35
47
|
* Provide the tardata to be unpacked, and the location where it's to be
|
|
36
|
-
* placed. Resolves when the tarball has been extracted.
|
|
48
|
+
* placed. Resolves when the tarball has been extracted. See
|
|
49
|
+
* {@link PlacementPool.unpack}.
|
|
37
50
|
*/
|
|
38
51
|
async unpack(tarData, target, format) {
|
|
39
52
|
if (mode === '0')
|
|
40
53
|
return unpack(tarData, target, format);
|
|
41
54
|
if (mode === '1')
|
|
42
55
|
return unpackSync(tarData, target, format);
|
|
43
|
-
return
|
|
56
|
+
return this.#pool.unpack(tarData, target, format);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Unpack the tarball in the vlt cache entry `file`, whose first 4
|
|
60
|
+
* bytes are the big-endian length of the head before the body. On a
|
|
61
|
+
* worker the file is read there, so the main thread never holds the
|
|
62
|
+
* body. See {@link PlacementPool.unpackCacheEntry}.
|
|
63
|
+
*/
|
|
64
|
+
async unpackCacheEntry(file, target, format) {
|
|
65
|
+
if (mode === '0') {
|
|
66
|
+
return unpack(cacheEntryBody(await readFile(file)), target, format);
|
|
67
|
+
}
|
|
68
|
+
if (mode === '1')
|
|
69
|
+
return unpackCacheEntrySync(file, target, format);
|
|
70
|
+
return this.#pool.unpackCacheEntry(file, target, format);
|
|
44
71
|
}
|
|
45
72
|
/**
|
|
46
73
|
* Same, reading the tarball from a file and skipping `offset` leading
|
|
@@ -62,8 +89,29 @@ export class Pool {
|
|
|
62
89
|
* {@link linkFromStore}.
|
|
63
90
|
*/
|
|
64
91
|
async linkFromStore(storeEntry, target, opts) {
|
|
92
|
+
// links stay on the main thread: see `placement-worker.ts`
|
|
65
93
|
return linkFromStore(storeEntry, target, opts);
|
|
66
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Start the unpack workers for an install about to unpack `jobs`
|
|
97
|
+
* tarballs, so they boot while the first packages are fetched rather
|
|
98
|
+
* than on the first placement. Never rejects. See
|
|
99
|
+
* {@link PlacementPool.warm}.
|
|
100
|
+
*/
|
|
101
|
+
async warm(jobs) {
|
|
102
|
+
if (mode !== undefined)
|
|
103
|
+
return;
|
|
104
|
+
await this.#pool.warm(jobs);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Terminate the placement worker threads, if any were started. The
|
|
108
|
+
* pool works without this -- idle workers do not keep the process
|
|
109
|
+
* alive -- but reify calls it when an install's placement is done, to
|
|
110
|
+
* free the threads promptly.
|
|
111
|
+
*/
|
|
112
|
+
async close() {
|
|
113
|
+
await this.#placement?.close();
|
|
114
|
+
}
|
|
67
115
|
/**
|
|
68
116
|
* Explode a tarball into `dir` for the global store. See
|
|
69
117
|
* {@link unpackToStoreSync}.
|
package/dist/unpack.d.ts
CHANGED
|
@@ -46,6 +46,22 @@ export declare const unpackFileParallel: (file: string, target: string, offset?:
|
|
|
46
46
|
* tarball never lands in the registry client's in-memory cache.
|
|
47
47
|
*/
|
|
48
48
|
export declare const unpackFileSync: (file: string, target: string, offset?: number, format?: TarballFormat) => void;
|
|
49
|
+
/**
|
|
50
|
+
* The tarball inside a vlt cache entry file: the file starts with a
|
|
51
|
+
* 4-byte big-endian length of the head, and the body follows the head.
|
|
52
|
+
* The length is read from the same bytes as the body, so an entry that
|
|
53
|
+
* `cache-unzip` rewrote with a different head is still read correctly.
|
|
54
|
+
*/
|
|
55
|
+
export declare const cacheEntryBody: (entry: Buffer) => Buffer;
|
|
56
|
+
/**
|
|
57
|
+
* Unpack the tarball in the vlt cache entry `file`. See
|
|
58
|
+
* {@link cacheEntryBody}.
|
|
59
|
+
*/
|
|
60
|
+
export declare const unpackCacheEntrySync: (file: string, target: string, format?: TarballFormat) => void;
|
|
61
|
+
/**
|
|
62
|
+
* Same, inflating on libuv's threadpool. See {@link unpackParallel}.
|
|
63
|
+
*/
|
|
64
|
+
export declare const unpackCacheEntryParallel: (file: string, target: string, format?: TarballFormat) => Promise<void>;
|
|
49
65
|
/** Sibling temp path used to build `target` before renaming it in. */
|
|
50
66
|
export declare const tmpName: (target: string) => string;
|
|
51
67
|
/**
|
package/dist/unpack.js
CHANGED
|
@@ -7,6 +7,7 @@ import { rimraf, rimrafSync } from 'rimraf';
|
|
|
7
7
|
import { Header } from 'tar/header';
|
|
8
8
|
import { Pax } from 'tar/pax';
|
|
9
9
|
import { brotliDecompress as brotliCB, brotliDecompressSync as brotliSyncCB, unzip as unzipCB, unzipSync as unzipSyncCB, } from 'node:zlib';
|
|
10
|
+
import { span } from '@vltpkg/output/trace';
|
|
10
11
|
import { findTarDir } from "./find-tar-dir.js";
|
|
11
12
|
import { storeIndexManifest } from "./store-index.js";
|
|
12
13
|
// Matches node-tar's MAX_DECOMPRESSION_RATIO, which npm uses via pacote.
|
|
@@ -205,7 +206,15 @@ export const unpack = async (tarData, target, format) => {
|
|
|
205
206
|
* libuv round trip per file, which costs more than the IO itself.
|
|
206
207
|
*/
|
|
207
208
|
export const unpackSync = (tarData, target, format) => {
|
|
208
|
-
|
|
209
|
+
const inflated = span('inflate', {
|
|
210
|
+
bytes: tarData.length,
|
|
211
|
+
sync: true,
|
|
212
|
+
});
|
|
213
|
+
const buffer = decompressSync(tarData, format);
|
|
214
|
+
inflated({ out: buffer.length });
|
|
215
|
+
const written = span('write', { bytes: buffer.length });
|
|
216
|
+
unpackUnzippedSync(buffer, target);
|
|
217
|
+
written();
|
|
209
218
|
};
|
|
210
219
|
/**
|
|
211
220
|
* Inflate off the main thread, then write the files on it.
|
|
@@ -221,7 +230,12 @@ export const unpackSync = (tarData, target, format) => {
|
|
|
221
230
|
* costs more CPU than gzip's, but it speeds up `.tgz` installs too.
|
|
222
231
|
*/
|
|
223
232
|
export const unpackParallel = async (tarData, target, format) => {
|
|
224
|
-
|
|
233
|
+
const inflated = span('inflate', { bytes: tarData.length });
|
|
234
|
+
const buffer = await decompress(tarData, format);
|
|
235
|
+
inflated({ out: buffer.length });
|
|
236
|
+
const written = span('write', { bytes: buffer.length });
|
|
237
|
+
unpackUnzippedSync(buffer, target);
|
|
238
|
+
written();
|
|
225
239
|
};
|
|
226
240
|
/**
|
|
227
241
|
* {@link unpackParallel} reading the tarball from a file, so the read
|
|
@@ -235,6 +249,28 @@ export const unpackFileParallel = async (file, target, offset = 0, format) => un
|
|
|
235
249
|
* tarball never lands in the registry client's in-memory cache.
|
|
236
250
|
*/
|
|
237
251
|
export const unpackFileSync = (file, target, offset = 0, format) => unpackSync(readFileSync(file).subarray(offset), target, format);
|
|
252
|
+
/**
|
|
253
|
+
* The tarball inside a vlt cache entry file: the file starts with a
|
|
254
|
+
* 4-byte big-endian length of the head, and the body follows the head.
|
|
255
|
+
* The length is read from the same bytes as the body, so an entry that
|
|
256
|
+
* `cache-unzip` rewrote with a different head is still read correctly.
|
|
257
|
+
*/
|
|
258
|
+
export const cacheEntryBody = (entry) => {
|
|
259
|
+
const headSize = entry.length >= 4 ? entry.readUInt32BE(0) : 0;
|
|
260
|
+
if (headSize < 7 || headSize > entry.length) {
|
|
261
|
+
throw error('not a cache entry', { found: headSize });
|
|
262
|
+
}
|
|
263
|
+
return entry.subarray(headSize);
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* Unpack the tarball in the vlt cache entry `file`. See
|
|
267
|
+
* {@link cacheEntryBody}.
|
|
268
|
+
*/
|
|
269
|
+
export const unpackCacheEntrySync = (file, target, format) => unpackSync(cacheEntryBody(readFileSync(file)), target, format);
|
|
270
|
+
/**
|
|
271
|
+
* Same, inflating on libuv's threadpool. See {@link unpackParallel}.
|
|
272
|
+
*/
|
|
273
|
+
export const unpackCacheEntryParallel = async (file, target, format) => unpackParallel(cacheEntryBody(await readFile(file)), target, format);
|
|
238
274
|
/** Sibling temp path used to build `target` before renaming it in. */
|
|
239
275
|
export const tmpName = (target) => dirname(target) + sep + '.' + basename(target) + '.' + tmpSuffix();
|
|
240
276
|
/**
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vltpkg/tar",
|
|
3
3
|
"description": "An extremely limited and very fast tar extractor",
|
|
4
|
-
"version": "1.6.
|
|
4
|
+
"version": "1.6.2",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
@@ -13,8 +13,9 @@
|
|
|
13
13
|
"url": "http://vlt.sh"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@vltpkg/error-cause": "1.6.
|
|
17
|
-
"@vltpkg/
|
|
16
|
+
"@vltpkg/error-cause": "1.6.2",
|
|
17
|
+
"@vltpkg/output": "1.6.2",
|
|
18
|
+
"@vltpkg/types": "1.6.2",
|
|
18
19
|
"rimraf": "^6.1.2",
|
|
19
20
|
"tar": "^7.5.2"
|
|
20
21
|
},
|
|
@@ -70,6 +71,16 @@
|
|
|
70
71
|
"default": "./dist/link-tree.js"
|
|
71
72
|
}
|
|
72
73
|
},
|
|
74
|
+
"./placement-pool": {
|
|
75
|
+
"import": {
|
|
76
|
+
"default": "./dist/placement-pool.js"
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"./placement-worker": {
|
|
80
|
+
"import": {
|
|
81
|
+
"default": "./dist/placement-worker.js"
|
|
82
|
+
}
|
|
83
|
+
},
|
|
73
84
|
"./pool": {
|
|
74
85
|
"import": {
|
|
75
86
|
"default": "./dist/pool.js"
|