-
-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: improve forkchoice updateHead() time #5867
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0acf6a
chore: update computeDeltas perf test
twoeths f2bc1fd
perf: avoid too many short lived objects in computeDeltas
twoeths 2f3367d
chore: more tests for different vaildator number
twoeths cfb02bd
chore: add protoNodes param to computeDeltas test
twoeths e16d2f8
chore: update updateHead perf test with 1 day of unfinalized blocks
twoeths 0aa5224
fix: improve nodeIsViableForHead for ProtoArray
twoeths 707af31
chore: create cached indices inside computeDeltas()
twoeths 7e400e3
chore: revise comments
twoeths File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 44 additions & 79 deletions
123
packages/fork-choice/test/perf/protoArray/computeDeltas.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,63 @@ | ||
import {itBench, setBenchOpts} from "@dapplion/benchmark"; | ||
import {expect} from "chai"; | ||
import { | ||
CachedBeaconStateAltair, | ||
computeStartSlotAtEpoch, | ||
EffectiveBalanceIncrements, | ||
getEffectiveBalanceIncrementsZeroed, | ||
} from "@lodestar/state-transition"; | ||
import {TIMELY_SOURCE_FLAG_INDEX} from "@lodestar/params"; | ||
// eslint-disable-next-line import/no-relative-packages | ||
import {generatePerfTestCachedStateAltair} from "../../../../state-transition/test/perf/util.js"; | ||
import {EffectiveBalanceIncrements, getEffectiveBalanceIncrementsZeroed} from "@lodestar/state-transition"; | ||
import {VoteTracker} from "../../../src/protoArray/interface.js"; | ||
import {computeDeltas} from "../../../src/protoArray/computeDeltas.js"; | ||
import {computeProposerBoostScoreFromBalances} from "../../../src/forkChoice/forkChoice.js"; | ||
|
||
/** Same to https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.5/specs/altair/beacon-chain.md#has_flag */ | ||
const TIMELY_SOURCE = 1 << TIMELY_SOURCE_FLAG_INDEX; | ||
function flagIsTimelySource(flag: number): boolean { | ||
return (flag & TIMELY_SOURCE) === TIMELY_SOURCE; | ||
} | ||
|
||
describe("computeDeltas", () => { | ||
let originalState: CachedBeaconStateAltair; | ||
const indices: Map<string, number> = new Map<string, number>(); | ||
let oldBalances: EffectiveBalanceIncrements; | ||
let newBalances: EffectiveBalanceIncrements; | ||
|
||
const oldRoot = "0x32dec344944029ba183ac387a7aa1f2068591c00e9bfadcfb238e50fbe9ea38e"; | ||
const newRoot = "0xb59f3a209f639dd6b5645ea9fad8d441df44c3be93bd1bbf50ef90bf124d1238"; | ||
|
||
before(function () { | ||
this.timeout(2 * 60 * 1000); // Generating the states for the first time is very slow | ||
|
||
originalState = generatePerfTestCachedStateAltair({goBackOneSlot: true}); | ||
|
||
const previousEpochParticipationArr = originalState.previousEpochParticipation.getAll(); | ||
const currentEpochParticipationArr = originalState.currentEpochParticipation.getAll(); | ||
|
||
const numPreviousEpochParticipation = previousEpochParticipationArr.filter(flagIsTimelySource).length; | ||
const numCurrentEpochParticipation = currentEpochParticipationArr.filter(flagIsTimelySource).length; | ||
|
||
expect(numPreviousEpochParticipation).to.equal(250000, "Wrong numPreviousEpochParticipation"); | ||
expect(numCurrentEpochParticipation).to.equal(250000, "Wrong numCurrentEpochParticipation"); | ||
|
||
oldBalances = getEffectiveBalanceIncrementsZeroed(numPreviousEpochParticipation); | ||
newBalances = getEffectiveBalanceIncrementsZeroed(numPreviousEpochParticipation); | ||
|
||
for (let i = 0; i < numPreviousEpochParticipation; i++) { | ||
oldBalances[i] = 32; | ||
newBalances[i] = 32; | ||
} | ||
for (let i = 0; i < 10000; i++) { | ||
indices.set("" + i, i); | ||
} | ||
indices.set(oldRoot, 1001); | ||
indices.set(newRoot, 1001); | ||
}); | ||
|
||
setBenchOpts({ | ||
minMs: 30 * 1000, | ||
maxMs: 40 * 1000, | ||
}); | ||
|
||
itBench({ | ||
id: "computeDeltas", | ||
beforeEach: () => { | ||
const votes: VoteTracker[] = []; | ||
const epoch = originalState.epochCtx.currentShuffling.epoch; | ||
const committee = originalState.epochCtx.getBeaconCommittee(computeStartSlotAtEpoch(epoch), 0); | ||
for (let i = 0; i < 250000; i++) { | ||
if (committee.includes(i)) { | ||
// 2 first numbers are respective to number of validators in goerli, mainnet as of Aug 2023 | ||
for (const numValidator of [500_000, 750_000, 1_400_000, 2_100_000]) { | ||
before(function () { | ||
this.timeout(2 * 60 * 1000); | ||
oldBalances = getEffectiveBalanceIncrementsZeroed(numValidator); | ||
newBalances = getEffectiveBalanceIncrementsZeroed(numValidator); | ||
|
||
for (let i = 0; i < numValidator; i++) { | ||
oldBalances[i] = 32; | ||
newBalances[i] = 32; | ||
} | ||
for (let i = 0; i < 10000; i++) { | ||
indices.set("" + i, i); | ||
} | ||
indices.set(oldRoot, 1001); | ||
indices.set(newRoot, 1002); | ||
}); | ||
|
||
setBenchOpts({ | ||
minMs: 30 * 1000, | ||
maxMs: 40 * 1000, | ||
}); | ||
|
||
itBench({ | ||
id: `computeDeltas ${numValidator} validators`, | ||
beforeEach: () => { | ||
const votes: VoteTracker[] = []; | ||
const epoch = 100_000; | ||
for (let i = 0; i < numValidator; i++) { | ||
votes.push({ | ||
currentRoot: oldRoot, | ||
nextRoot: newRoot, | ||
nextEpoch: epoch, | ||
}); | ||
} else { | ||
votes.push({ | ||
currentRoot: oldRoot, | ||
nextRoot: oldRoot, | ||
nextEpoch: epoch - 1, | ||
}); | ||
} | ||
} | ||
return votes; | ||
}, | ||
fn: (votes) => { | ||
computeDeltas(indices, votes, oldBalances, newBalances, new Set()); | ||
}, | ||
}); | ||
|
||
itBench({ | ||
id: "computeProposerBoostScoreFromBalances", | ||
fn: () => { | ||
computeProposerBoostScoreFromBalances(newBalances, {slotsPerEpoch: 32, proposerScoreBoost: 70}); | ||
}, | ||
}); | ||
return votes; | ||
}, | ||
fn: (votes) => { | ||
computeDeltas(indices, votes, oldBalances, newBalances, new Set()); | ||
}, | ||
}); | ||
|
||
itBench({ | ||
id: `computeProposerBoostScoreFromBalances ${numValidator} validators`, | ||
fn: () => { | ||
computeProposerBoostScoreFromBalances(newBalances, {slotsPerEpoch: 32, proposerScoreBoost: 70}); | ||
}, | ||
}); | ||
} | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can also be improved, it's faster to do a regular for loop than array.from
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about Array.fill performance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last time i checked regular for loop in the fastest way to populate an array. There should be benchmarks in the state transition package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dapplion I'll do
new Array() and a for loop in
in #5882 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you benchmark just pushing to an empty array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not yet, how important is it? in this case there are always more than 0 protonodes in forkchoice