From f2bc1fdbb8e315c716f5cd367e5f6344abee3fde Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Wed, 9 Aug 2023 14:35:25 +0700 Subject: [PATCH] perf: avoid too many short lived objects in computeDeltas --- .../fork-choice/src/protoArray/computeDeltas.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/fork-choice/src/protoArray/computeDeltas.ts b/packages/fork-choice/src/protoArray/computeDeltas.ts index 6b2d1133d37c..7e19c1d2879f 100644 --- a/packages/fork-choice/src/protoArray/computeDeltas.ts +++ b/packages/fork-choice/src/protoArray/computeDeltas.ts @@ -21,6 +21,10 @@ export function computeDeltas( ): number[] { const deltas = Array.from({length: indices.size}, () => 0); const zeroHash = HEX_ZERO_HASH; + // avoid creating new variables in the loop to potentially reduce GC pressure + let oldBalance, newBalance: number; + let currentRoot, nextRoot: string; + let currentDeltaIndex, nextDeltaIndex: number | undefined; for (let vIndex = 0; vIndex < votes.length; vIndex++) { const vote = votes[vIndex]; // There is no need to create a score change if the validator has never voted or both of their @@ -28,26 +32,27 @@ export function computeDeltas( if (vote === undefined) { continue; } - const {currentRoot, nextRoot} = vote; + currentRoot = vote.currentRoot; + nextRoot = vote.nextRoot; if (currentRoot === zeroHash && nextRoot === zeroHash) { continue; } // IF the validator was not included in the _old_ balances (i.e. it did not exist yet) // then say its balance was 0 - const oldBalance = oldBalances[vIndex] ?? 0; + oldBalance = oldBalances[vIndex] ?? 0; // If the validator's vote is not known in the _new_ balances, then use a balance of zero. // // It is possible that there was a vote for an unknown validator if we change our justified // state to a new state with a higher epoch that is on a different fork because that fork may have // on-boarded fewer validators than the prior fork. - const newBalance = newBalances[vIndex] ?? 0; + newBalance = newBalances[vIndex] ?? 0; if (equivocatingIndices.size > 0 && equivocatingIndices.has(vIndex)) { // this function could be called multiple times but we only want to process slashing validator for 1 time if (currentRoot !== zeroHash) { - const currentDeltaIndex = indices.get(currentRoot); + currentDeltaIndex = indices.get(currentRoot); if (currentDeltaIndex !== undefined) { if (currentDeltaIndex >= deltas.length) { throw new ProtoArrayError({ @@ -65,7 +70,7 @@ export function computeDeltas( if (currentRoot !== nextRoot || oldBalance !== newBalance) { // We ignore the vote if it is not known in `indices . // We assume that it is outside of our tree (ie: pre-finalization) and therefore not interesting - const currentDeltaIndex = indices.get(currentRoot); + currentDeltaIndex = indices.get(currentRoot); if (currentDeltaIndex !== undefined) { if (currentDeltaIndex >= deltas.length) { throw new ProtoArrayError({ @@ -77,7 +82,7 @@ export function computeDeltas( } // We ignore the vote if it is not known in `indices . // We assume that it is outside of our tree (ie: pre-finalization) and therefore not interesting - const nextDeltaIndex = indices.get(nextRoot); + nextDeltaIndex = indices.get(nextRoot); if (nextDeltaIndex !== undefined) { if (nextDeltaIndex >= deltas.length) { throw new ProtoArrayError({