Skip to content

Commit 5e8f67b

Browse files
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.
1 parent 190f4c9 commit 5e8f67b

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

Diff for: compiler/rustc_macros/src/serialize.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,17 @@ fn decodable_body(
7676
ty_name,
7777
variants.len()
7878
);
79+
let tag = if variants.len() < u8::MAX as usize {
80+
quote! {
81+
::rustc_serialize::Decoder::read_u8(__decoder) as usize
82+
}
83+
} else {
84+
quote! {
85+
::rustc_serialize::Decoder::read_usize(__decoder)
86+
}
87+
};
7988
quote! {
80-
match ::rustc_serialize::Decoder::read_usize(__decoder) {
89+
match #tag {
8190
#match_inner
8291
n => panic!(#message, n),
8392
}
@@ -206,11 +215,20 @@ fn encodable_body(
206215
variant_idx += 1;
207216
result
208217
});
209-
quote! {
210-
let disc = match *self {
211-
#encode_inner
212-
};
213-
::rustc_serialize::Encoder::emit_usize(__encoder, disc);
218+
if variant_idx < u8::MAX as usize {
219+
quote! {
220+
let disc = match *self {
221+
#encode_inner
222+
};
223+
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
224+
}
225+
} else {
226+
quote! {
227+
let disc = match *self {
228+
#encode_inner
229+
};
230+
::rustc_serialize::Encoder::emit_usize(__encoder, disc);
231+
}
214232
}
215233
};
216234

0 commit comments

Comments
 (0)