Skip to content

Commit 31e2f4d

Browse files
authored
Rollup merge of rust-lang#111173 - nnethercote:still-more-Encoder-cleanups, r=cjgillot
Still more encoder cleanups r? ``@cjgillot``
2 parents f5c50e3 + 723ca2a commit 31e2f4d

File tree

5 files changed

+59
-127
lines changed

5 files changed

+59
-127
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
108108
emit_i64(i64);
109109
emit_i32(i32);
110110
emit_i16(i16);
111-
emit_i8(i8);
112111

113-
emit_bool(bool);
114-
emit_char(char);
115-
emit_str(&str);
116112
emit_raw_bytes(&[u8]);
117113
}
118114
}

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,11 +1026,7 @@ impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> {
10261026
emit_i64(i64);
10271027
emit_i32(i32);
10281028
emit_i16(i16);
1029-
emit_i8(i8);
10301029

1031-
emit_bool(bool);
1032-
emit_char(char);
1033-
emit_str(&str);
10341030
emit_raw_bytes(&[u8]);
10351031
}
10361032
}

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -506,23 +506,18 @@ macro_rules! implement_ty_decoder {
506506

507507
impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
508508
$crate::__impl_decoder_methods! {
509+
read_usize -> usize;
509510
read_u128 -> u128;
510511
read_u64 -> u64;
511512
read_u32 -> u32;
512513
read_u16 -> u16;
513514
read_u8 -> u8;
514-
read_usize -> usize;
515515

516+
read_isize -> isize;
516517
read_i128 -> i128;
517518
read_i64 -> i64;
518519
read_i32 -> i32;
519520
read_i16 -> i16;
520-
read_i8 -> i8;
521-
read_isize -> isize;
522-
523-
read_bool -> bool;
524-
read_char -> char;
525-
read_str -> &str;
526521
}
527522

528523
#[inline]
@@ -531,13 +526,13 @@ macro_rules! implement_ty_decoder {
531526
}
532527

533528
#[inline]
534-
fn position(&self) -> usize {
535-
self.opaque.position()
529+
fn peek_byte(&self) -> u8 {
530+
self.opaque.peek_byte()
536531
}
537532

538533
#[inline]
539-
fn peek_byte(&self) -> u8 {
540-
self.opaque.peek_byte()
534+
fn position(&self) -> usize {
535+
self.opaque.position()
541536
}
542537
}
543538
}

compiler/rustc_serialize/src/opaque.rs

Lines changed: 51 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -265,52 +265,41 @@ impl Drop for FileEncoder {
265265
}
266266
}
267267

268-
macro_rules! file_encoder_write_leb128 {
269-
($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
270-
const MAX_ENCODED_LEN: usize = $crate::leb128::max_leb128_len::<$int_ty>();
268+
macro_rules! write_leb128 {
269+
($this_fn:ident, $int_ty:ty, $write_leb_fn:ident) => {
270+
#[inline]
271+
fn $this_fn(&mut self, v: $int_ty) {
272+
const MAX_ENCODED_LEN: usize = $crate::leb128::max_leb128_len::<$int_ty>();
271273

272-
// We ensure this during `FileEncoder` construction.
273-
debug_assert!($enc.capacity() >= MAX_ENCODED_LEN);
274+
// We ensure this during `FileEncoder` construction.
275+
debug_assert!(self.capacity() >= MAX_ENCODED_LEN);
274276

275-
let mut buffered = $enc.buffered;
277+
let mut buffered = self.buffered;
276278

277-
// This can't overflow. See assertion in `FileEncoder::with_capacity`.
278-
if std::intrinsics::unlikely(buffered + MAX_ENCODED_LEN > $enc.capacity()) {
279-
$enc.flush();
280-
buffered = 0;
281-
}
279+
// This can't overflow. See assertion in `FileEncoder::with_capacity`.
280+
if std::intrinsics::unlikely(buffered + MAX_ENCODED_LEN > self.capacity()) {
281+
self.flush();
282+
buffered = 0;
283+
}
282284

283-
// SAFETY: The above check and flush ensures that there is enough
284-
// room to write the encoded value to the buffer.
285-
let buf = unsafe {
286-
&mut *($enc.buf.as_mut_ptr().add(buffered) as *mut [MaybeUninit<u8>; MAX_ENCODED_LEN])
287-
};
285+
// SAFETY: The above check and flush ensures that there is enough
286+
// room to write the encoded value to the buffer.
287+
let buf = unsafe {
288+
&mut *(self.buf.as_mut_ptr().add(buffered)
289+
as *mut [MaybeUninit<u8>; MAX_ENCODED_LEN])
290+
};
288291

289-
let encoded = leb128::$fun(buf, $value);
290-
$enc.buffered = buffered + encoded.len();
291-
}};
292+
let encoded = leb128::$write_leb_fn(buf, v);
293+
self.buffered = buffered + encoded.len();
294+
}
295+
};
292296
}
293297

294298
impl Encoder for FileEncoder {
295-
#[inline]
296-
fn emit_usize(&mut self, v: usize) {
297-
file_encoder_write_leb128!(self, v, usize, write_usize_leb128)
298-
}
299-
300-
#[inline]
301-
fn emit_u128(&mut self, v: u128) {
302-
file_encoder_write_leb128!(self, v, u128, write_u128_leb128)
303-
}
304-
305-
#[inline]
306-
fn emit_u64(&mut self, v: u64) {
307-
file_encoder_write_leb128!(self, v, u64, write_u64_leb128)
308-
}
309-
310-
#[inline]
311-
fn emit_u32(&mut self, v: u32) {
312-
file_encoder_write_leb128!(self, v, u32, write_u32_leb128)
313-
}
299+
write_leb128!(emit_usize, usize, write_usize_leb128);
300+
write_leb128!(emit_u128, u128, write_u128_leb128);
301+
write_leb128!(emit_u64, u64, write_u64_leb128);
302+
write_leb128!(emit_u32, u32, write_u32_leb128);
314303

315304
#[inline]
316305
fn emit_u16(&mut self, v: u16) {
@@ -322,25 +311,10 @@ impl Encoder for FileEncoder {
322311
self.write_one(v);
323312
}
324313

325-
#[inline]
326-
fn emit_isize(&mut self, v: isize) {
327-
file_encoder_write_leb128!(self, v, isize, write_isize_leb128)
328-
}
329-
330-
#[inline]
331-
fn emit_i128(&mut self, v: i128) {
332-
file_encoder_write_leb128!(self, v, i128, write_i128_leb128)
333-
}
334-
335-
#[inline]
336-
fn emit_i64(&mut self, v: i64) {
337-
file_encoder_write_leb128!(self, v, i64, write_i64_leb128)
338-
}
339-
340-
#[inline]
341-
fn emit_i32(&mut self, v: i32) {
342-
file_encoder_write_leb128!(self, v, i32, write_i32_leb128)
343-
}
314+
write_leb128!(emit_isize, isize, write_isize_leb128);
315+
write_leb128!(emit_i128, i128, write_i128_leb128);
316+
write_leb128!(emit_i64, i64, write_i64_leb128);
317+
write_leb128!(emit_i32, i32, write_i32_leb128);
344318

345319
#[inline]
346320
fn emit_i16(&mut self, v: i16) {
@@ -437,30 +411,19 @@ impl<'a> MemDecoder<'a> {
437411
}
438412

439413
macro_rules! read_leb128 {
440-
($dec:expr, $fun:ident) => {{ leb128::$fun($dec) }};
414+
($this_fn:ident, $int_ty:ty, $read_leb_fn:ident) => {
415+
#[inline]
416+
fn $this_fn(&mut self) -> $int_ty {
417+
leb128::$read_leb_fn(self)
418+
}
419+
};
441420
}
442421

443422
impl<'a> Decoder for MemDecoder<'a> {
444-
#[inline]
445-
fn position(&self) -> usize {
446-
// SAFETY: This type guarantees start <= current
447-
unsafe { self.current.sub_ptr(self.start) }
448-
}
449-
450-
#[inline]
451-
fn read_u128(&mut self) -> u128 {
452-
read_leb128!(self, read_u128_leb128)
453-
}
454-
455-
#[inline]
456-
fn read_u64(&mut self) -> u64 {
457-
read_leb128!(self, read_u64_leb128)
458-
}
459-
460-
#[inline]
461-
fn read_u32(&mut self) -> u32 {
462-
read_leb128!(self, read_u32_leb128)
463-
}
423+
read_leb128!(read_usize, usize, read_usize_leb128);
424+
read_leb128!(read_u128, u128, read_u128_leb128);
425+
read_leb128!(read_u64, u64, read_u64_leb128);
426+
read_leb128!(read_u32, u32, read_u32_leb128);
464427

465428
#[inline]
466429
fn read_u16(&mut self) -> u16 {
@@ -480,36 +443,16 @@ impl<'a> Decoder for MemDecoder<'a> {
480443
}
481444
}
482445

483-
#[inline]
484-
fn read_usize(&mut self) -> usize {
485-
read_leb128!(self, read_usize_leb128)
486-
}
487-
488-
#[inline]
489-
fn read_i128(&mut self) -> i128 {
490-
read_leb128!(self, read_i128_leb128)
491-
}
492-
493-
#[inline]
494-
fn read_i64(&mut self) -> i64 {
495-
read_leb128!(self, read_i64_leb128)
496-
}
497-
498-
#[inline]
499-
fn read_i32(&mut self) -> i32 {
500-
read_leb128!(self, read_i32_leb128)
501-
}
446+
read_leb128!(read_isize, isize, read_isize_leb128);
447+
read_leb128!(read_i128, i128, read_i128_leb128);
448+
read_leb128!(read_i64, i64, read_i64_leb128);
449+
read_leb128!(read_i32, i32, read_i32_leb128);
502450

503451
#[inline]
504452
fn read_i16(&mut self) -> i16 {
505453
i16::from_le_bytes(self.read_array())
506454
}
507455

508-
#[inline]
509-
fn read_isize(&mut self) -> isize {
510-
read_leb128!(self, read_isize_leb128)
511-
}
512-
513456
#[inline]
514457
fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
515458
if bytes > self.remaining() {
@@ -532,6 +475,12 @@ impl<'a> Decoder for MemDecoder<'a> {
532475
// Since we just checked current == end, the current pointer must be inbounds.
533476
unsafe { *self.current }
534477
}
478+
479+
#[inline]
480+
fn position(&self) -> usize {
481+
// SAFETY: This type guarantees start <= current
482+
unsafe { self.current.sub_ptr(self.start) }
483+
}
535484
}
536485

537486
// Specializations for contiguous byte sequences follow. The default implementations for slices

compiler/rustc_serialize/src/serialize.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
//! Support code for encoding and decoding types.
22
3-
/*
4-
Core encoding and decoding interfaces.
5-
*/
6-
73
use std::alloc::Allocator;
84
use std::borrow::Cow;
95
use std::cell::{Cell, RefCell};
@@ -35,13 +31,13 @@ const STR_SENTINEL: u8 = 0xC1;
3531
/// really makes sense to store floating-point values at all.
3632
/// (If you need it, revert <https://github.com/rust-lang/rust/pull/109984>.)
3733
pub trait Encoder {
38-
// Primitive types:
3934
fn emit_usize(&mut self, v: usize);
4035
fn emit_u128(&mut self, v: u128);
4136
fn emit_u64(&mut self, v: u64);
4237
fn emit_u32(&mut self, v: u32);
4338
fn emit_u16(&mut self, v: u16);
4439
fn emit_u8(&mut self, v: u8);
40+
4541
fn emit_isize(&mut self, v: isize);
4642
fn emit_i128(&mut self, v: i128);
4743
fn emit_i64(&mut self, v: i64);
@@ -93,13 +89,13 @@ pub trait Encoder {
9389
/// really makes sense to store floating-point values at all.
9490
/// (If you need it, revert <https://github.com/rust-lang/rust/pull/109984>.)
9591
pub trait Decoder {
96-
// Primitive types:
9792
fn read_usize(&mut self) -> usize;
9893
fn read_u128(&mut self) -> u128;
9994
fn read_u64(&mut self) -> u64;
10095
fn read_u32(&mut self) -> u32;
10196
fn read_u16(&mut self) -> u16;
10297
fn read_u8(&mut self) -> u8;
98+
10399
fn read_isize(&mut self) -> isize;
104100
fn read_i128(&mut self) -> i128;
105101
fn read_i64(&mut self) -> i64;

0 commit comments

Comments
 (0)