1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00

Preallocate serialized vector of HashPrefixRow (#909)

This should save 2 reallocation for row since serialize will allocate without
capacity (should be 4) and every time will double, so it will need other 2
re-allocation for a total of 3. While here is 1.

Do the same for HeaderRow.

Co-authored-by: Riccardo Casatta <riccardo@casatta.it>
This commit is contained in:
Roman Zeyde 2023-07-22 22:45:22 +03:00 committed by GitHub
parent 77f761ea7c
commit b041adf9bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ use std::convert::TryFrom;
use bitcoin::blockdata::block::Header as BlockHeader;
use bitcoin::{
consensus::encode::{deserialize, serialize, Decodable, Encodable},
consensus::encode::{deserialize, Decodable, Encodable},
hashes::{hash_newtype, sha256, Hash},
OutPoint, Script, Txid,
};
@ -39,6 +39,7 @@ macro_rules! impl_consensus_encoding {
}
const HASH_PREFIX_LEN: usize = 8;
const HEIGHT_SIZE: usize = 4;
type HashPrefix = [u8; HASH_PREFIX_LEN];
type Height = u32;
@ -49,9 +50,16 @@ pub(crate) struct HashPrefixRow {
height: Height, // transaction confirmed height
}
const HASH_PREFIX_ROW_SIZE: usize = HASH_PREFIX_LEN + HEIGHT_SIZE;
impl HashPrefixRow {
pub(crate) fn to_db_row(&self) -> db::Row {
serialize(self).into_boxed_slice()
let mut vec = Vec::with_capacity(HASH_PREFIX_ROW_SIZE);
let len = self
.consensus_encode(&mut vec)
.expect("in-memory writers don't error");
debug_assert_eq!(len, HASH_PREFIX_ROW_SIZE);
vec.into_boxed_slice()
}
pub(crate) fn from_db_row(row: &[u8]) -> Self {
@ -159,6 +167,8 @@ pub(crate) struct HeaderRow {
pub(crate) header: BlockHeader,
}
const HEADER_ROW_SIZE: usize = 80;
impl_consensus_encoding!(HeaderRow, header);
impl HeaderRow {
@ -167,7 +177,12 @@ impl HeaderRow {
}
pub(crate) fn to_db_row(&self) -> db::Row {
serialize(self).into_boxed_slice()
let mut vec = Vec::with_capacity(HEADER_ROW_SIZE);
let len = self
.consensus_encode(&mut vec)
.expect("in-memory writers don't error");
debug_assert_eq!(len, HEADER_ROW_SIZE);
vec.into_boxed_slice()
}
pub(crate) fn from_db_row(row: &[u8]) -> Self {