@parcel/workers 2.7.0 → 2.8.1

Sign up to get free protection for your applications and to get access to all the features.
package/lib/Worker.js CHANGED
@@ -43,7 +43,7 @@ let WORKER_ID = 0;
43
43
 
44
44
  class Worker extends _events().default {
45
45
  id = WORKER_ID++;
46
- sharedReferences = new Map();
46
+ sentSharedReferences = new Set();
47
47
  calls = new Map();
48
48
  exitCode = null;
49
49
  callId = 0;
@@ -129,6 +129,7 @@ class Worker extends _events().default {
129
129
  }
130
130
 
131
131
  sendSharedReference(ref, value) {
132
+ this.sentSharedReferences.add(ref);
132
133
  return new Promise((resolve, reject) => {
133
134
  this.call({
134
135
  method: 'createSharedReference',
package/lib/WorkerFarm.js CHANGED
@@ -114,6 +114,7 @@ class WorkerFarm extends _events().default {
114
114
  handles = new Map();
115
115
  sharedReferences = new Map();
116
116
  sharedReferencesByValue = new Map();
117
+ serializedSharedReferences = new Map();
117
118
 
118
119
  constructor(farmOptions = {}) {
119
120
  super();
@@ -189,19 +190,25 @@ class WorkerFarm extends _events().default {
189
190
  return this.options.maxConcurrentWorkers > 0 || !this.options.useLocalWorker;
190
191
  }
191
192
 
192
- createHandle(method) {
193
+ createHandle(method, useMainThread = false) {
193
194
  return async (...args) => {
194
195
  // Child process workers are slow to start (~600ms).
195
196
  // While we're waiting, just run on the main thread.
196
197
  // This significantly speeds up startup time.
197
- if (this.shouldUseRemoteWorkers()) {
198
+ if (this.shouldUseRemoteWorkers() && !useMainThread) {
198
199
  return this.addCall(method, [...args, false]);
199
200
  } else {
200
201
  if (this.options.warmWorkers && this.shouldStartRemoteWorkers()) {
201
202
  this.warmupWorker(method, args);
202
203
  }
203
204
 
204
- let processedArgs = (0, _core().restoreDeserializedObject)((0, _core().prepareForSerialization)([...args, false]));
205
+ let processedArgs;
206
+
207
+ if (!useMainThread) {
208
+ processedArgs = (0, _core().restoreDeserializedObject)((0, _core().prepareForSerialization)([...args, false]));
209
+ } else {
210
+ processedArgs = args;
211
+ }
205
212
 
206
213
  if (this.localWorkerInit != null) {
207
214
  await this.localWorkerInit;
@@ -276,9 +283,19 @@ class WorkerFarm extends _events().default {
276
283
  }
277
284
 
278
285
  if (worker.calls.size < this.options.maxConcurrentCallsPerWorker) {
279
- worker.call(this.callQueue.shift());
286
+ this.callWorker(worker, this.callQueue.shift());
287
+ }
288
+ }
289
+ }
290
+
291
+ async callWorker(worker, call) {
292
+ for (let ref of this.sharedReferences.keys()) {
293
+ if (!worker.sentSharedReferences.has(ref)) {
294
+ await worker.sendSharedReference(ref, this.getSerializedSharedReference(ref));
280
295
  }
281
296
  }
297
+
298
+ worker.call(call);
282
299
  }
283
300
 
284
301
  async processRequest(data, worker) {
@@ -408,30 +425,29 @@ class WorkerFarm extends _events().default {
408
425
  return handle;
409
426
  }
410
427
 
411
- async createSharedReference(value, // An optional, pre-serialized representation of the value to be used
412
- // in its place.
413
- buffer) {
428
+ createSharedReference(value, isCacheable = true) {
414
429
  let ref = referenceId++;
415
430
  this.sharedReferences.set(ref, value);
416
431
  this.sharedReferencesByValue.set(value, ref);
417
- let toSend = buffer ? buffer.buffer : value;
418
- let promises = [];
419
432
 
420
- for (let worker of this.workers.values()) {
421
- if (worker.ready) {
422
- promises.push(worker.sendSharedReference(ref, toSend));
423
- }
433
+ if (!isCacheable) {
434
+ this.serializedSharedReferences.set(ref, null);
424
435
  }
425
436
 
426
- await Promise.all(promises);
427
437
  return {
428
438
  ref,
429
439
  dispose: () => {
430
440
  this.sharedReferences.delete(ref);
431
441
  this.sharedReferencesByValue.delete(value);
442
+ this.serializedSharedReferences.delete(ref);
432
443
  let promises = [];
433
444
 
434
445
  for (let worker of this.workers.values()) {
446
+ if (!worker.sentSharedReferences.has(ref)) {
447
+ continue;
448
+ }
449
+
450
+ worker.sentSharedReferences.delete(ref);
435
451
  promises.push(new Promise((resolve, reject) => {
436
452
  worker.call({
437
453
  method: 'deleteSharedReference',
@@ -449,6 +465,24 @@ class WorkerFarm extends _events().default {
449
465
  };
450
466
  }
451
467
 
468
+ getSerializedSharedReference(ref) {
469
+ let cached = this.serializedSharedReferences.get(ref);
470
+
471
+ if (cached) {
472
+ return cached;
473
+ }
474
+
475
+ let value = this.sharedReferences.get(ref);
476
+ let buf = (0, _core().serialize)(value).buffer; // If the reference was created with the isCacheable option set to false,
477
+ // serializedSharedReferences will contain `null` as the value.
478
+
479
+ if (cached !== null) {
480
+ this.serializedSharedReferences.set(ref, buf);
481
+ }
482
+
483
+ return buf;
484
+ }
485
+
452
486
  async startProfile() {
453
487
  let promises = [];
454
488
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/workers",
3
- "version": "2.7.0",
3
+ "version": "2.8.1",
4
4
  "description": "Blazing fast, zero configuration web application bundler",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -21,20 +21,20 @@
21
21
  "node": ">= 12.0.0"
22
22
  },
23
23
  "dependencies": {
24
- "@parcel/diagnostic": "2.7.0",
25
- "@parcel/logger": "2.7.0",
26
- "@parcel/types": "2.7.0",
27
- "@parcel/utils": "2.7.0",
24
+ "@parcel/diagnostic": "2.8.1",
25
+ "@parcel/logger": "2.8.1",
26
+ "@parcel/types": "2.8.1",
27
+ "@parcel/utils": "2.8.1",
28
28
  "chrome-trace-event": "^1.0.2",
29
29
  "nullthrows": "^1.1.1"
30
30
  },
31
31
  "peerDependencies": {
32
- "@parcel/core": "^2.7.0"
32
+ "@parcel/core": "^2.8.1"
33
33
  },
34
34
  "browser": {
35
35
  "./src/cpuCount.js": false,
36
36
  "./src/process/ProcessWorker.js": false,
37
37
  "./src/threads/ThreadsWorker.js": false
38
38
  },
39
- "gitHead": "9e5d05586577e89991ccf90400f2c741dca11aa3"
39
+ "gitHead": "f8d3fc30ca5b33d8f8674525f2a741d662c5986a"
40
40
  }
package/src/Worker.js CHANGED
@@ -31,7 +31,7 @@ export default class Worker extends EventEmitter {
31
31
  +options: WorkerOpts;
32
32
  worker: WorkerImpl;
33
33
  id: number = WORKER_ID++;
34
- sharedReferences: $ReadOnlyMap<SharedReference, mixed> = new Map();
34
+ sentSharedReferences: Set<SharedReference> = new Set();
35
35
 
36
36
  calls: Map<number, WorkerCall> = new Map();
37
37
  exitCode: ?number = null;
@@ -135,6 +135,7 @@ export default class Worker extends EventEmitter {
135
135
  }
136
136
 
137
137
  sendSharedReference(ref: SharedReference, value: mixed): Promise<any> {
138
+ this.sentSharedReferences.add(ref);
138
139
  return new Promise((resolve, reject) => {
139
140
  this.call({
140
141
  method: 'createSharedReference',
package/src/WorkerFarm.js CHANGED
@@ -76,6 +76,7 @@ export default class WorkerFarm extends EventEmitter {
76
76
  handles: Map<number, Handle> = new Map();
77
77
  sharedReferences: Map<SharedReference, mixed> = new Map();
78
78
  sharedReferencesByValue: Map<mixed, SharedReference> = new Map();
79
+ serializedSharedReferences: Map<SharedReference, ?ArrayBuffer> = new Map();
79
80
  profiler: ?Profiler;
80
81
 
81
82
  constructor(farmOptions: $Shape<FarmOptions> = {}) {
@@ -175,21 +176,26 @@ export default class WorkerFarm extends EventEmitter {
175
176
  );
176
177
  }
177
178
 
178
- createHandle(method: string): HandleFunction {
179
+ createHandle(method: string, useMainThread: boolean = false): HandleFunction {
179
180
  return async (...args) => {
180
181
  // Child process workers are slow to start (~600ms).
181
182
  // While we're waiting, just run on the main thread.
182
183
  // This significantly speeds up startup time.
183
- if (this.shouldUseRemoteWorkers()) {
184
+ if (this.shouldUseRemoteWorkers() && !useMainThread) {
184
185
  return this.addCall(method, [...args, false]);
185
186
  } else {
186
187
  if (this.options.warmWorkers && this.shouldStartRemoteWorkers()) {
187
188
  this.warmupWorker(method, args);
188
189
  }
189
190
 
190
- let processedArgs = restoreDeserializedObject(
191
- prepareForSerialization([...args, false]),
192
- );
191
+ let processedArgs;
192
+ if (!useMainThread) {
193
+ processedArgs = restoreDeserializedObject(
194
+ prepareForSerialization([...args, false]),
195
+ );
196
+ } else {
197
+ processedArgs = args;
198
+ }
193
199
 
194
200
  if (this.localWorkerInit != null) {
195
201
  await this.localWorkerInit;
@@ -273,11 +279,24 @@ export default class WorkerFarm extends EventEmitter {
273
279
  }
274
280
 
275
281
  if (worker.calls.size < this.options.maxConcurrentCallsPerWorker) {
276
- worker.call(this.callQueue.shift());
282
+ this.callWorker(worker, this.callQueue.shift());
277
283
  }
278
284
  }
279
285
  }
280
286
 
287
+ async callWorker(worker: Worker, call: WorkerCall): Promise<void> {
288
+ for (let ref of this.sharedReferences.keys()) {
289
+ if (!worker.sentSharedReferences.has(ref)) {
290
+ await worker.sendSharedReference(
291
+ ref,
292
+ this.getSerializedSharedReference(ref),
293
+ );
294
+ }
295
+ }
296
+
297
+ worker.call(call);
298
+ }
299
+
281
300
  async processRequest(
282
301
  data: {|
283
302
  location: FilePath,
@@ -400,33 +419,31 @@ export default class WorkerFarm extends EventEmitter {
400
419
  return handle;
401
420
  }
402
421
 
403
- async createSharedReference(
422
+ createSharedReference(
404
423
  value: mixed,
405
- // An optional, pre-serialized representation of the value to be used
406
- // in its place.
407
- buffer?: Buffer,
408
- ): Promise<{|ref: SharedReference, dispose(): Promise<mixed>|}> {
424
+ isCacheable: boolean = true,
425
+ ): {|ref: SharedReference, dispose(): Promise<mixed>|} {
409
426
  let ref = referenceId++;
410
427
  this.sharedReferences.set(ref, value);
411
428
  this.sharedReferencesByValue.set(value, ref);
412
-
413
- let toSend = buffer ? buffer.buffer : value;
414
- let promises = [];
415
- for (let worker of this.workers.values()) {
416
- if (worker.ready) {
417
- promises.push(worker.sendSharedReference(ref, toSend));
418
- }
429
+ if (!isCacheable) {
430
+ this.serializedSharedReferences.set(ref, null);
419
431
  }
420
432
 
421
- await Promise.all(promises);
422
-
423
433
  return {
424
434
  ref,
425
435
  dispose: () => {
426
436
  this.sharedReferences.delete(ref);
427
437
  this.sharedReferencesByValue.delete(value);
438
+ this.serializedSharedReferences.delete(ref);
439
+
428
440
  let promises = [];
429
441
  for (let worker of this.workers.values()) {
442
+ if (!worker.sentSharedReferences.has(ref)) {
443
+ continue;
444
+ }
445
+
446
+ worker.sentSharedReferences.delete(ref);
430
447
  promises.push(
431
448
  new Promise((resolve, reject) => {
432
449
  worker.call({
@@ -445,6 +462,24 @@ export default class WorkerFarm extends EventEmitter {
445
462
  };
446
463
  }
447
464
 
465
+ getSerializedSharedReference(ref: SharedReference): ArrayBuffer {
466
+ let cached = this.serializedSharedReferences.get(ref);
467
+ if (cached) {
468
+ return cached;
469
+ }
470
+
471
+ let value = this.sharedReferences.get(ref);
472
+ let buf = serialize(value).buffer;
473
+
474
+ // If the reference was created with the isCacheable option set to false,
475
+ // serializedSharedReferences will contain `null` as the value.
476
+ if (cached !== null) {
477
+ this.serializedSharedReferences.set(ref, buf);
478
+ }
479
+
480
+ return buf;
481
+ }
482
+
448
483
  async startProfile() {
449
484
  let promises = [];
450
485
  for (let worker of this.workers.values()) {