Rollup merge of #59820 - eddyb:proc-macro-rpc-opt, r=nnethercote

proc_macro: stop using LEB128 for RPC.

I'm not sure how much of an improvement this creates, it's pretty tricky to measure.
This commit is contained in:
Mazdak Farrokhzad 2019-04-12 20:36:07 +02:00 committed by GitHub
commit ed1dd1eb74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,32 +24,22 @@ pub(super) trait DecodeMut<'a, 's, S>: Sized {
} }
macro_rules! rpc_encode_decode { macro_rules! rpc_encode_decode {
(uleb128 $ty:ty) => { (le $ty:ty) => {
impl<S> Encode<S> for $ty { impl<S> Encode<S> for $ty {
fn encode(mut self, w: &mut Writer, s: &mut S) { fn encode(self, w: &mut Writer, _: &mut S) {
let mut byte = 0x80; w.write_all(&self.to_le_bytes()).unwrap();
while byte & 0x80 != 0 {
byte = (self & 0x7f) as u8;
self >>= 7;
if self != 0 {
byte |= 0x80;
}
byte.encode(w, s);
}
} }
} }
impl<S> DecodeMut<'_, '_, S> for $ty { impl<S> DecodeMut<'_, '_, S> for $ty {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { fn decode(r: &mut Reader<'_>, _: &mut S) -> Self {
let mut byte = 0x80; const N: usize = ::std::mem::size_of::<$ty>();
let mut v = 0;
let mut shift = 0; let mut bytes = [0; N];
while byte & 0x80 != 0 { bytes.copy_from_slice(&r[..N]);
byte = u8::decode(r, s); *r = &r[N..];
v |= ((byte & 0x7f) as Self) << shift;
shift += 7; Self::from_le_bytes(bytes)
}
v
} }
} }
}; };
@ -136,8 +126,8 @@ impl<S> DecodeMut<'_, '_, S> for u8 {
} }
} }
rpc_encode_decode!(uleb128 u32); rpc_encode_decode!(le u32);
rpc_encode_decode!(uleb128 usize); rpc_encode_decode!(le usize);
impl<S> Encode<S> for bool { impl<S> Encode<S> for bool {
fn encode(self, w: &mut Writer, s: &mut S) { fn encode(self, w: &mut Writer, s: &mut S) {