From afa3587ccc745f916f531afb4525e1b594fefc69 Mon Sep 17 00:00:00 2001 From: Jan Lewandowski Date: Wed, 28 Aug 2024 17:51:23 +0900 Subject: [PATCH] impr(linearizeOps): remove the need for sorting before splicing --- packages/crdt/src/cros/ReduceActionType/index.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/crdt/src/cros/ReduceActionType/index.ts b/packages/crdt/src/cros/ReduceActionType/index.ts index 0a3d0ade..c2d63acd 100644 --- a/packages/crdt/src/cros/ReduceActionType/index.ts +++ b/packages/crdt/src/cros/ReduceActionType/index.ts @@ -45,7 +45,7 @@ export class ReduceActionType { } linearizeOps(): Operation[] { - const order = this.hashGraph.topologicalSort(); + let order = this.hashGraph.topologicalSort(); const result: Operation[] = []; let i = 0; @@ -90,15 +90,19 @@ export class ReduceActionType { const resolved = this.resolveConflicts(concurrentOps); switch (resolved.action) { - case ActionType.Reduce: - // Sort the indices in descending order, so that splice does not mess up the order - resolved.indices.sort((a, b) => (a < b ? 1 : -1)); + case ActionType.Reduce: { + const newOrder = []; for (const idx of resolved.indices) { if (idx === i) shouldIncrementI = false; - order.splice(idx, 1); + order[idx] = ""; } + for (const val of order) { + if (val !== "") newOrder.push(val); + } + order = newOrder; if (!shouldIncrementI) j = order.length; // Break out of inner loop break; + } case ActionType.Nop: j++; break;