mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 12:11:59 +00:00
Begin refactor for less allocation
This commit is contained in:
@@ -3,10 +3,10 @@ use super::*;
|
||||
mod impls;
|
||||
|
||||
pub trait Encodable {
|
||||
fn as_ssz_bytes(&self) -> Vec<u8>;
|
||||
|
||||
fn is_ssz_fixed_len() -> bool;
|
||||
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>);
|
||||
|
||||
/// The number of bytes this object occupies in the fixed-length portion of the SSZ bytes.
|
||||
///
|
||||
/// By default, this is set to `BYTES_PER_LENGTH_OFFSET` which is suitable for variable length
|
||||
@@ -15,6 +15,14 @@ pub trait Encodable {
|
||||
fn ssz_fixed_len() -> usize {
|
||||
BYTES_PER_LENGTH_OFFSET
|
||||
}
|
||||
|
||||
fn as_ssz_bytes(&self) -> Vec<u8> {
|
||||
let mut buf = vec![];
|
||||
|
||||
self.ssz_append(&mut buf);
|
||||
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VariableLengths {
|
||||
@@ -40,11 +48,13 @@ impl SszStream {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/// Append some item to the stream.
|
||||
pub fn append<T: Encodable>(&mut self, item: &T) {
|
||||
let mut bytes = item.as_ssz_bytes();
|
||||
|
||||
if T::is_ssz_fixed_len() {
|
||||
self.app
|
||||
self.fixed_bytes.append(&mut bytes);
|
||||
} else {
|
||||
self.variable_lengths.push(VariableLengths {
|
||||
@@ -57,6 +67,32 @@ impl SszStream {
|
||||
self.variable_bytes.append(&mut bytes);
|
||||
}
|
||||
}
|
||||
*/
|
||||
pub fn reserve<T: Encodable>(&mut self, additional: usize) {
|
||||
if T::is_ssz_fixed_len() {
|
||||
self.fixed_bytes.reserve(additional * T::ssz_fixed_len());
|
||||
} else {
|
||||
self.fixed_bytes
|
||||
.reserve(additional * BYTES_PER_LENGTH_OFFSET);
|
||||
self.variable_lengths.reserve(additional);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn append_fixed_bytes(&mut self, bytes: &[u8]) {
|
||||
self.fixed_bytes.extend_from_slice(bytes)
|
||||
}
|
||||
|
||||
pub fn append_variable_bytes(&mut self, bytes: &[u8]) {
|
||||
self.variable_lengths.push(VariableLengths {
|
||||
fixed_bytes_position: self.fixed_bytes.len(),
|
||||
variable_bytes_length: bytes.len(),
|
||||
});
|
||||
|
||||
self.fixed_bytes
|
||||
.append(&mut vec![0; BYTES_PER_LENGTH_OFFSET]);
|
||||
|
||||
self.variable_bytes.extend_from_slice(bytes);
|
||||
}
|
||||
|
||||
/// Update the offsets (if any) in the fixed-length bytes to correctly point to the values in
|
||||
/// the variable length part.
|
||||
|
||||
Reference in New Issue
Block a user