Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Optimize no-op case

Manav Rathi9c0d9ac53800
diff --git a/web/packages/new/photos/services/ml/cluster.ts b/web/packages/new/photos/services/ml/cluster.ts
index ed848f4d24..ffe286272d 100644
--- a/web/packages/new/photos/services/ml/cluster.ts
+++ b/web/packages/new/photos/services/ml/cluster.ts
@@ -317,13 +317,29 @@ const clusterBatchLinear = async (
state: ClusteringState,
onProgress: (progress: ClusteringProgress) => void,
) => {
+ const [clustered, unclustered] = faces.reduce<
+ [ClusterFace[], ClusterFace[]]
+ >(
+ (split, face) => (
+ split[state.faceIDToClusterID.has(face.faceID) ? 0 : 1].push(face),
+ split
+ ),
+ [[], []],
+ );
+
+ if (!unclustered.length) {
+ // Optimization: early exit if nothing in batch is unclustered. In a
+ // single test (so it might be not be a universal benefit) of ~8k faces,
+ // it helped reduce the no-op time by 10x.
+ onProgress({ completed: faces.length, total: faces.length });
+ return;
+ }
+
// Sort the faces so that the already clustered ones are at the front.
- faces = faces
- .filter((f) => state.faceIDToClusterID.has(f.faceID))
- .concat(faces.filter((f) => !state.faceIDToClusterID.has(f.faceID)));
+ const sortedFaces = clustered.concat(unclustered);
// For each face in the batch
- for (const [i, fi] of faces.entries()) {
+ for (const [i, fi] of sortedFaces.entries()) {
if (i % 100 == 0) {
onProgress({ completed: i, total: faces.length });
// See: [Note: Draining the event loop during clustering]