mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-19 13:58:28 +00:00
Begin refactor for less allocation
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
//! Encode and decode a list 10,000 times.
|
||||
//! Encode and decode a list many times.
|
||||
//!
|
||||
//! Useful for `cargo flamegraph`.
|
||||
|
||||
@@ -7,7 +7,7 @@ use ssz::{Decodable, Encodable};
|
||||
fn main() {
|
||||
let vec: Vec<u64> = vec![4242; 8196];
|
||||
|
||||
let output: Vec<Vec<u64>> = (0..10_000)
|
||||
let output: Vec<Vec<u64>> = (0..40_000)
|
||||
.into_iter()
|
||||
.map(|_| Vec::from_ssz_bytes(&vec.as_ssz_bytes()).unwrap())
|
||||
.collect();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszDecoderBuilder, SszStream};
|
||||
use ssz::{encode_length, Decodable, DecodeError, Encodable, SszDecoderBuilder, SszStream};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Foo {
|
||||
@@ -12,14 +12,23 @@ impl Encodable for Foo {
|
||||
<u16 as Encodable>::is_ssz_fixed_len() && <Vec<u16> as Encodable>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn as_ssz_bytes(&self) -> Vec<u8> {
|
||||
let mut stream = SszStream::new();
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||
let offset = <u16 as Encodable>::ssz_fixed_len()
|
||||
+ <Vec<u16> as Encodable>::ssz_fixed_len()
|
||||
+ <u16 as Encodable>::ssz_fixed_len();
|
||||
|
||||
stream.append(&self.a);
|
||||
stream.append(&self.b);
|
||||
stream.append(&self.c);
|
||||
let mut fixed = Vec::with_capacity(offset);
|
||||
let mut variable = vec![];
|
||||
|
||||
stream.drain()
|
||||
if <u16 as Encodable>::is_ssz_fixed_len() {
|
||||
self.a.ssz_append(&mut fixed);
|
||||
} else {
|
||||
fixed.append(encode_length())
|
||||
}
|
||||
|
||||
if <Vec<u16> as Encodable>::is_ssz_fixed_len() {
|
||||
self.a.ssz_append(&mut fixed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user