-
-
Notifications
You must be signed in to change notification settings - Fork 348
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
feat: load state from Uint8Array #6057
Merged
+1,316
−44
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e8bc051
fix: implement loadState api
twoeths 1eb9b2a
chore: benchmark findModifiedValidators()
twoeths cd7b1c1
feat: implement deserializeContainerIgnoreFields()
twoeths 930e5bb
feat: implement loadValidator()
twoeths 50876c6
fix: type the ignoreFields
twoeths a669b4b
chore: benchmark loadState()
twoeths a420c54
fix: add '--max-old-space-size=4096' to github workflow
twoeths 1443a84
fix: revert default vc count in perf test
twoeths 73bdbee
Revert "fix: add '--max-old-space-size=4096' to github workflow"
twoeths 56bff34
chore: rename loadCachedBeaconState to loadUnfinalizedCachedBeaconState
twoeths a26c830
chore: loadValidator - reuse fields as much as possible
twoeths c7df27b
chore: more benchmarks to compare Uint8Array
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
chore: benchmark findModifiedValidators()
- Loading branch information
commit 1eb9b2a545bf9057ca62bc6a47994df51472892e
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
47 changes: 47 additions & 0 deletions
47
packages/state-transition/src/util/loadState/findModifiedInactivityScores.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// UintNum64 = 8 bytes | ||
export const INACTIVITY_SCORE_SIZE = 8; | ||
|
||
/** | ||
* As monitored on mainnet, inactivityScores are not changed much and they are mostly 0 | ||
* Using Buffer.compare is the fastest way as noted in `./findModifiedValidators.ts` | ||
* @returns output parameter modifiedValidators: validator indices that are modified | ||
*/ | ||
export function findModifiedInactivityScores( | ||
inactivityScoresBytes: Uint8Array, | ||
inactivityScoresBytes2: Uint8Array, | ||
modifiedValidators: number[], | ||
validatorOffset = 0 | ||
): void { | ||
if (inactivityScoresBytes.length !== inactivityScoresBytes2.length) { | ||
throw new Error( | ||
"inactivityScoresBytes.length !== inactivityScoresBytes2.length " + | ||
inactivityScoresBytes.length + | ||
" vs " + | ||
inactivityScoresBytes2.length | ||
); | ||
} | ||
|
||
if (Buffer.compare(inactivityScoresBytes, inactivityScoresBytes2) === 0) { | ||
return; | ||
} | ||
|
||
if (inactivityScoresBytes.length === INACTIVITY_SCORE_SIZE) { | ||
modifiedValidators.push(validatorOffset); | ||
return; | ||
} | ||
|
||
const numValidator = Math.floor(inactivityScoresBytes.length / INACTIVITY_SCORE_SIZE); | ||
const halfValidator = Math.floor(numValidator / 2); | ||
findModifiedInactivityScores( | ||
inactivityScoresBytes.subarray(0, halfValidator * INACTIVITY_SCORE_SIZE), | ||
inactivityScoresBytes2.subarray(0, halfValidator * INACTIVITY_SCORE_SIZE), | ||
modifiedValidators, | ||
validatorOffset | ||
); | ||
findModifiedInactivityScores( | ||
inactivityScoresBytes.subarray(halfValidator * INACTIVITY_SCORE_SIZE), | ||
inactivityScoresBytes2.subarray(halfValidator * INACTIVITY_SCORE_SIZE), | ||
modifiedValidators, | ||
validatorOffset + halfValidator | ||
); | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/state-transition/src/util/loadState/findModifiedValidators.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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {VALIDATOR_BYTES_SIZE} from "../sszBytes.js"; | ||
|
||
/** | ||
* Find modified validators by comparing two validators bytes using Buffer.compare() recursively | ||
* - As noted in packages/state-transition/test/perf/util/loadState/findModifiedValidators.test.ts, serializing validators and compare Uint8Array is the fastest way | ||
* - The performance is quite stable and can afford a lot of difference in validators (the benchmark tested up to 10k but it's not likely we have that difference in mainnet) | ||
* - Also packages/state-transition/test/perf/misc/byteArrayEquals.test.ts shows that Buffer.compare() is very efficient for large Uint8Array | ||
* | ||
* @returns output parameter modifiedValidators: validator indices that are modified | ||
*/ | ||
export function findModifiedValidators( | ||
validatorsBytes: Uint8Array, | ||
validatorsBytes2: Uint8Array, | ||
modifiedValidators: number[], | ||
validatorOffset = 0 | ||
): void { | ||
if (validatorsBytes.length !== validatorsBytes2.length) { | ||
throw new Error( | ||
"validatorsBytes.length !== validatorsBytes2.length " + validatorsBytes.length + " vs " + validatorsBytes2.length | ||
); | ||
} | ||
|
||
if (Buffer.compare(validatorsBytes, validatorsBytes2) === 0) { | ||
return; | ||
} | ||
|
||
if (validatorsBytes.length === VALIDATOR_BYTES_SIZE) { | ||
modifiedValidators.push(validatorOffset); | ||
return; | ||
} | ||
|
||
const numValidator = Math.floor(validatorsBytes.length / VALIDATOR_BYTES_SIZE); | ||
const halfValidator = Math.floor(numValidator / 2); | ||
findModifiedValidators( | ||
validatorsBytes.subarray(0, halfValidator * VALIDATOR_BYTES_SIZE), | ||
validatorsBytes2.subarray(0, halfValidator * VALIDATOR_BYTES_SIZE), | ||
modifiedValidators, | ||
validatorOffset | ||
); | ||
findModifiedValidators( | ||
validatorsBytes.subarray(halfValidator * VALIDATOR_BYTES_SIZE), | ||
validatorsBytes2.subarray(halfValidator * VALIDATOR_BYTES_SIZE), | ||
modifiedValidators, | ||
validatorOffset + halfValidator | ||
); | ||
} |
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
56 changes: 56 additions & 0 deletions
56
packages/state-transition/test/perf/misc/byteArrayEquals.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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {itBench} from "@dapplion/benchmark"; | ||
import {byteArrayEquals} from "@chainsafe/ssz"; | ||
import {generateState} from "../../utils/state.js"; | ||
import {generateValidators} from "../../utils/validator.js"; | ||
|
||
/** | ||
* compare Uint8Array, the longer the array, the better performance Buffer.compare() is | ||
* - with 32 bytes, Buffer.compare() is 1.5x faster (rootEquals.test.ts showed > 2x faster) | ||
* ✔ byteArrayEquals 32 1.004480e+7 ops/s 99.55400 ns/op - 19199 runs 2.08 s | ||
* ✔ Buffer.compare 32 1.553495e+7 ops/s 64.37100 ns/op - 3634 runs 0.303 s | ||
* | ||
* - with 1024 bytes, Buffer.compare() is 21.8x faster | ||
* ✔ byteArrayEquals 1024 379239.7 ops/s 2.636855 us/op - 117 runs 0.811 s | ||
* ✔ Buffer.compare 1024 8269999 ops/s 120.9190 ns/op - 3330 runs 0.525 s | ||
* | ||
* - with 16384 bytes, Buffer.compare() is 41x faster | ||
* ✔ byteArrayEquals 16384 23808.76 ops/s 42.00135 us/op - 13 runs 1.05 s | ||
* ✔ Buffer.compare 16384 975058.0 ops/s 1.025580 us/op - 297 runs 0.806 s | ||
* | ||
* - with 123687377 bytes, Buffer.compare() is 38x faster | ||
* ✔ byteArrayEquals 123687377 3.077884 ops/s 324.8985 ms/op - 1 runs 64.5 s | ||
* ✔ Buffer.compare 123687377 114.7834 ops/s 8.712061 ms/op - 13 runs 12.1 s | ||
*/ | ||
describe("compare Uint8Array", () => { | ||
const numValidator = 1_000_000; | ||
const validators = generateValidators(numValidator); | ||
const state = generateState({validators: validators}); | ||
const stateBytes = state.serialize(); | ||
|
||
const lengths = [32, 1024, 16384, stateBytes.length]; | ||
// const lengths = [stateBytes.length]; | ||
for (const length of lengths) { | ||
const runsFactor = length > 16384 ? 100 : 1000; | ||
const bytes = stateBytes.subarray(0, length); | ||
const bytes2 = bytes.slice(); | ||
itBench({ | ||
id: `byteArrayEquals ${length}`, | ||
fn: () => { | ||
for (let i = 0; i < runsFactor; i++) { | ||
byteArrayEquals(bytes, bytes2); | ||
} | ||
}, | ||
runsFactor, | ||
}); | ||
|
||
itBench({ | ||
id: `Buffer.compare ${length}`, | ||
fn: () => { | ||
for (let i = 0; i < runsFactor; i++) { | ||
Buffer.compare(bytes, bytes2); | ||
} | ||
}, | ||
runsFactor, | ||
}); | ||
} | ||
}); |
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
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.
You should extend this comparision tests to various checks: