Skip to content

Commit

Permalink
Only serialise and write cache chunks to disk when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeLane committed Jan 23, 2024
1 parent d7af37d commit 84ea07d
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/core/core/src/RequestTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ export default class RequestTracker {
farm: WorkerFarm;
options: ParcelOptions;
signal: ?AbortSignal;
cachedRequestsLastChunk: number | null;

constructor({
graph,
Expand All @@ -869,6 +870,7 @@ export default class RequestTracker {
this.graph = graph || new RequestGraph();
this.farm = farm;
this.options = options;
this.cachedRequestsLastChunk = null;
}

// TODO: refactor (abortcontroller should be created by RequestTracker)
Expand Down Expand Up @@ -1123,7 +1125,7 @@ export default class RequestTracker {
return;
}

const serialisedGraph = this.graph.serialize();
let serialisedGraph = this.graph.serialize();

let total = 0;
const serialiseAndSet = async (
Expand Down Expand Up @@ -1155,7 +1157,7 @@ export default class RequestTracker {
});
};

const queue = new PromiseQueue({
let queue = new PromiseQueue({
maxConcurrent: 32,
});

Expand All @@ -1167,9 +1169,9 @@ export default class RequestTracker {
});

// Preallocating a sparse array is faster than pushing when N is high enough
const cacheableNodes = new Array(serialisedGraph.nodes.length);
let cacheableNodes = new Array(serialisedGraph.nodes.length);
for (let i = 0; i < serialisedGraph.nodes.length; i += 1) {
const node = serialisedGraph.nodes[i];
let node = serialisedGraph.nodes[i];

let resultCacheKey = node?.resultCacheKey;
if (
Expand All @@ -1181,14 +1183,21 @@ export default class RequestTracker {
.add(() => serialiseAndSet(resultCacheKey, node.result))
.catch(() => {});
// eslint-disable-next-line no-unused-vars
const {result: _, ...newNode} = node;
let {result: _, ...newNode} = node;
cacheableNodes[i] = newNode;
} else {
cacheableNodes[i] = node;
}
}

for (let i = 0; i * NODES_PER_BLOB < cacheableNodes.length; i += 1) {
if (
this.cachedRequestsLastChunk !== null &&
i < this.cachedRequestsLastChunk
) {
continue;
}

queue
.add(() =>
serialiseAndSet(
Expand All @@ -1197,6 +1206,7 @@ export default class RequestTracker {
),
)
.catch(() => {});
this.cachedRequestsLastChunk = i;
}

queue
Expand Down Expand Up @@ -1272,8 +1282,8 @@ async function loadRequestGraph(options): Async<RequestGraph> {
i += 1;
}

const serializedRequestGraph = await getAndDeserialize(requestGraphKey);
const requestGraph = RequestGraph.deserialize({
let serializedRequestGraph = await getAndDeserialize(requestGraphKey);
let requestGraph = RequestGraph.deserialize({
...serializedRequestGraph,
nodes: (await Promise.all(nodePromises)).flatMap(nodeChunk => nodeChunk),
});
Expand Down

0 comments on commit 84ea07d

Please sign in to comment.