Fork choice: avoid redundant call to get_ancestor #1880
Merged
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.
Summary
Fork choice
on_block
contains a redundant call toget_ancestor
in the best case scenario.Detail
Consider the following best-case scenario for the beacon chain:
n
andn % SLOTS_PER_EPOCH == 0
.n - 1
ton
caused both the finalized and justified checkpoints to increase.First we will run the following:
We will execute the last line in the above block, therefore
store.justified_checkpoint == state.current_justified_checkpoint
.Next, we face
if state.finalized_checkpoint.epoch > store.finalized_checkpoint.epoch
(code block omitted for brevity). This condition will be true, so we will then be faced with:We know the left-hand-side of the
or
is false (we already set these two values to be equal), so we will always hit the right-hand-side (get_ancestor
). The body of theif
statement is a no-op, so the result ofget_ancestor
is redundant.It's also worth noting that this
get_ancestor
call is particularly onerous seeing as it is based uponstore.justified_checkpoint
and we can't utilize thestate.block_roots
from the block that we are currently processing. (i.e., we must do a state read or a block-by-block reverse iteration).Why am I raising this?
I know that suggesting runtime optimizations to the spec is generally discouraged, but:
get_ancestor
.(1) and (2) are arguments to merge this PR, however I'm not particularly concerned whether or not it gets merged (i.e., not dying on this hill). (3) is the primary driving force at the moment, if you'd be willing to provide some comment :)