From 5e8f67bbc9017cc02338e959ca49f5355ff22f07 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jan 2024 18:40:00 -0500 Subject: [PATCH] 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. --- compiler/rustc_macros/src/serialize.rs | 30 ++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index 9ca7ce09ba6..98b53945b91 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -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); + } } };