-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Conversation
util/rlp/src/stream.rs
Outdated
stream | ||
} | ||
|
||
pub fn append<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: RlpEncodable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One nice change we could make would be to impl
on [u8]
instead of &'a [u8]
(likewise for str), and then do E: RlpEncodable + ?Sized
so we don't have to double-reference slices (which currently leads to lots of &&*my_vec
in the codebase)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be included in separate pr, this one is big enough ;)
@@ -162,7 +162,8 @@ impl Decodable for Step { | |||
|
|||
impl Encodable for Step { | |||
fn rlp_append(&self, s: &mut RlpStream) { | |||
RlpEncodable::rlp_append(&self.number(), s); | |||
//s.append(&self.number()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old comment.
@@ -278,6 +279,8 @@ mod tests { | |||
::rlp::encode(&H520::default()).to_vec(), | |||
Vec::new() | |||
]; | |||
|
|||
println!("seal: {:?}", seal); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary print
util/rlp/src/stream.rs
Outdated
finished_list: bool, | ||
} | ||
|
||
impl Default for RlpStream { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
double space after Default
@@ -16,7 +16,7 @@ | |||
|
|||
//! Peer status and capabilities. | |||
|
|||
use rlp::{DecoderError, RlpDecodable, RlpEncodable, RlpStream, Stream, UntrustedRlp, View}; | |||
use rlp::{DecoderError, RlpDecodable, Encodable, RlpStream, UntrustedRlp, View}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was RlpEncodable
renamed, but not the rest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RlpDecodable
was renamed accordingly in the next pr. The rest of data structures names remained unchanged. I didn't want to introduce to many changes in a single pr
} | ||
} | ||
|
||
impl<T> Encodable for Option<T> where T: Encodable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is dangerous and should be removed IMO. Why does Option
has to be a list?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a question to me. It's already like that on master,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know. Might be a good time to get rid of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will file an issue and fix in the next pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how else would you encode an Option
in RLP?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Manually. My point is that changing e.g. a struct member type T
to Option<T>
silently changes encoding of that member to a list. I recall fixing a bug caused by this.
lgtm |
util/rlp/src/impls.rs
Outdated
fn rlp_append(&self, s: &mut RlpStream) { | ||
let leading_empty_bytes = self.leading_zeros() as usize / 8; | ||
let mut buffer = [0u8; $size]; | ||
(&mut buffer as &mut [u8]).$func::<BigEndian>(*self).expect("buffer.len() == sizeof(*self); qed"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be just
BigEndian::$func(&mut buffer, *self);
util/rlp/src/stream.rs
Outdated
let leading_empty_bytes = size.leading_zeros() as usize / 8; | ||
let size_bytes = 4 - leading_empty_bytes as u8; | ||
let mut buffer = [0u8; 4]; | ||
(&mut buffer as &mut [u8]).write_u32::<BigEndian>(size).expect("buffer.len() == sizeof(value); qed"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be just
BigEndian::write_u32(&mut buffer, size)
reduction of code size and number of traits is good. addition of |
lgtm |
is it the case that the only underlying reason for the |
yes |
i seem to remember there was something we could do with traits, no? Have a |
Nothing less complicated than what we had before (with at least 3 different traits) comes to my mind. And I think the solution from this pr is less complicated and error prone |
well the fact i now have two different functions to call in order to do the same conceptual thing, depending on some arbitrary distinction between datatypes, adds to the complexity. that said, i won't block it if y'all prefer this way of doing it. |
for now, it's only serialization refactor. it's still WIP, but wanted to know, if you like interface changes I made:
changes:
BytesEncodable
traitEncodable
traitToBytes
Encodable
is no longer implemented toT where T: ToBytes
RlpStream
does not implementStream
trait. All functions where moved to public interface. I find additionalStream
import to be really annoyingbyteorder
for serializationappend_list
which should be used to append list of encodable itemsappend_internal
which should be used for wrapper types to encoding item without incrementing list elements counterbenchmarks:
so encoding integers is almost 25% faster