Skip to content
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

perf: skip next block headers if they are already inserted. #245

Merged
merged 1 commit into from
Aug 9, 2023

Conversation

ielashi
Copy link
Collaborator

@ielashi ielashi commented Aug 9, 2023

As the Bitcoin canister is syncing, it can encounter the following scenario frequently if it's falling far behind:

// Initial state
unstable_state = {
  blocks: {A, B, C};
  next_block_headers: {D, E, F}
}

// Calls get_successors, and gets the following response:
get_successors_response {
  blocks: {D},
  next_block_headers: {E, F}
}

In this scenario, the block headers E and F are processed twice, and that is relatively expensive.

This commit adds logic to check if a block header has already been added to the unstable state and, if yes, skips it. A benchmark has been added to prevent this from regressing.

Before the optimization:

insert_block_headers_multiple_times
                        time:   [118.21 B Instructions 118.21 B Instructions 118.21 B Instructions]

After the optimization:

insert_block_headers_multiple_times
                        time:   [12.055 B Instructions 12.055 B Instructions 12.055 B Instructions]
                        change: [-89.802% -89.802% -89.802%] (p = 0.00 < 0.05)
                        Performance has improved.

As the Bitcoin canister is syncing, it can encounter the following
pattern if it's falling far behind:

```
unstable_state = {
  blocks: {A, B, C};
  next_block_headers: {D, E, F}
}

// Calls get_successors, and gets the following response:
get_successors_response {
  blocks: {D},
  next_block_headers: {E, F}
}
```

In this scenario, the canister processes block headers `E` and `F` twice,
which is unnecessary computation.

This commit adds logic to check if a block header has already been added
to the unstable blocks and, if yes, skips it. A benchmark has been added
to prevent this from regressing.

Before the optimization:

```
insert_block_headers_multiple_times
                        time:   [118.21 B Instructions 118.21 B Instructions 118.21 B Instructions]
```

After the optimization:

```
insert_block_headers_multiple_times
                        time:   [12.055 B Instructions 12.055 B Instructions 12.055 B Instructions]
                        change: [-89.802% -89.802% -89.802%] (p = 0.00 < 0.05)
                        Performance has improved.
```
@ielashi ielashi requested a review from a team as a code owner August 9, 2023 08:52
@ielashi ielashi merged commit 3197171 into master Aug 9, 2023
21 checks passed
@ielashi ielashi deleted the ielashi/ignore_added_next_block_headers branch August 9, 2023 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants