Skip to content

Commit

Permalink
perf: avoid too many short lived objects in computeDeltas
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Aug 9, 2023
1 parent f0acf6a commit f2bc1fd
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/fork-choice/src/protoArray/computeDeltas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,38 @@ 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
// votes are for the zero hash (genesis block)
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({
Expand All @@ -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({
Expand All @@ -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({
Expand Down

0 comments on commit f2bc1fd

Please sign in to comment.