u8 tags for smaller enums

100% of the serialized enums during libcore compilation fit into the
smaller tag, and this eliminates hitting the leb128 code for
coding/decoding when we can statically guarantee that's not required.

30% of all leb128 integers serialized in libcore (12981183 total) come
from the usize's removed here.
This commit is contained in:
Mark Rousskov 2024-01-09 18:40:00 -05:00
parent 190f4c9611
commit 5e8f67bbc9

View File

@ -76,8 +76,17 @@ fn decodable_body(
ty_name,
variants.len()
);
let tag = if variants.len() < u8::MAX as usize {
quote! {
::rustc_serialize::Decoder::read_u8(__decoder) as usize
}
} else {
quote! {
::rustc_serialize::Decoder::read_usize(__decoder)
}
};
quote! {
match ::rustc_serialize::Decoder::read_usize(__decoder) {
match #tag {
#match_inner
n => panic!(#message, n),
}
@ -206,11 +215,20 @@ fn encodable_body(
variant_idx += 1;
result
});
quote! {
let disc = match *self {
#encode_inner
};
::rustc_serialize::Encoder::emit_usize(__encoder, disc);
if variant_idx < u8::MAX as usize {
quote! {
let disc = match *self {
#encode_inner
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
}
} else {
quote! {
let disc = match *self {
#encode_inner
};
::rustc_serialize::Encoder::emit_usize(__encoder, disc);
}
}
};