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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ fn bench_function(c: &mut Criterion<Instructions>, method: &str) {

pub fn criterion_benchmark(c: &mut Criterion<Instructions>) {
bench_function(c, "insert_block_headers");
bench_function(c, "insert_block_headers_multiple_times");
bench_function(c, "insert_300_blocks");
bench_function(c, "get_metrics");
}
Expand Down
32 changes: 32 additions & 0 deletions benchmarks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,38 @@ fn insert_block_headers() -> u64 {
})
}

// Inserts the same block headers multiple times.
#[query]
fn insert_block_headers_multiple_times() -> u64 {
ic_btc_canister::init(Config {
network: Network::Testnet,
..Config::default()
});

// Compute the next block headers.
let next_block_headers = TESTNET_BLOCKS.with(|b| {
let blocks = b.borrow();
let mut next_block_headers = vec![];
for i in 0..1000 {
let mut block_header_blob = vec![];
BlockHeader::consensus_encode(blocks[i as usize].header(), &mut block_header_blob)
.unwrap();
next_block_headers.push(BlockHeaderBlob::try_from(block_header_blob).unwrap());
}

next_block_headers
});

// Benchmark inserting the block headers.
count_instructions(|| {
with_state_mut(|s| {
for _ in 0..10 {
ic_btc_canister::state::insert_next_block_headers(s, next_block_headers.as_slice());
}
});
})
}

// Returns the number of instructions consumed by the given function.
fn count_instructions<R>(f: impl FnOnce() -> R) -> u64 {
let start = ic_cdk::api::performance_counter(0);
Expand Down
5 changes: 5 additions & 0 deletions canister/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ pub fn insert_next_block_headers(state: &mut State, next_block_headers: &[BlockH
}
};

if state.unstable_blocks.has_next_block_header(&block_header) {
// Already processed this block header. Skip...
continue;
}

let validation_result =
match ValidationContext::new_with_next_block_headers(state, &block_header)
.map_err(|_| InsertBlockError::PrevHeaderNotFound)
Expand Down
7 changes: 7 additions & 0 deletions canister/src/unstable_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ impl UnstableBlocks {
Ok(())
}

/// Returns true if the given block header is already stored as one of the next block headers.
pub fn has_next_block_header(&self, block_header: &BlockHeader) -> bool {
self.next_block_headers
.get_header(&BlockHash::from(block_header.block_hash()))
.is_some()
}

// Public only for testing purpose.
pub(crate) fn next_block_headers_max_height(&self) -> Option<Height> {
self.next_block_headers.get_max_height()
Expand Down
Loading