2020-12-29 15:14:33 -08:00
|
|
|
use crate::leb128::{self, max_leb128_len};
|
2021-03-25 11:43:03 +01:00
|
|
|
use crate::serialize::{self, Encoder as _};
|
|
|
|
use std::convert::TryInto;
|
2020-12-06 17:30:55 -08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, Write};
|
2020-12-16 19:46:19 -08:00
|
|
|
use std::mem::MaybeUninit;
|
2020-12-06 17:30:55 -08:00
|
|
|
use std::path::Path;
|
2020-12-16 19:46:19 -08:00
|
|
|
use std::ptr;
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2016-01-19 14:39:23 +13:00
|
|
|
// -----------------------------------------------------------------------------
|
2015-12-25 13:59:02 -05:00
|
|
|
// Encoder
|
2016-01-19 14:39:23 +13:00
|
|
|
// -----------------------------------------------------------------------------
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
pub type EncodeResult = Result<(), !>;
|
2016-08-28 07:10:22 +03:00
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
pub struct Encoder {
|
|
|
|
pub data: Vec<u8>,
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
impl Encoder {
|
|
|
|
pub fn new(data: Vec<u8>) -> Encoder {
|
|
|
|
Encoder { data }
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
2017-12-19 22:31:15 -05:00
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
pub fn into_inner(self) -> Vec<u8> {
|
|
|
|
self.data
|
2017-12-19 22:31:15 -05:00
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2020-12-06 17:30:55 -08:00
|
|
|
#[inline]
|
|
|
|
pub fn position(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
2018-06-04 22:14:02 +02:00
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2020-12-06 17:30:55 -08:00
|
|
|
macro_rules! write_leb128 {
|
|
|
|
($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
|
|
|
|
const MAX_ENCODED_LEN: usize = max_leb128_len!($int_ty);
|
|
|
|
let old_len = $enc.data.len();
|
|
|
|
|
|
|
|
if MAX_ENCODED_LEN > $enc.data.capacity() - old_len {
|
|
|
|
$enc.data.reserve(MAX_ENCODED_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
// SAFETY: The above check and `reserve` ensures that there is enough
|
|
|
|
// room to write the encoded value to the vector's internal buffer.
|
|
|
|
unsafe {
|
|
|
|
let buf = &mut *($enc.data.as_mut_ptr().add(old_len)
|
|
|
|
as *mut [MaybeUninit<u8>; MAX_ENCODED_LEN]);
|
|
|
|
let encoded = leb128::$fun(buf, $value);
|
|
|
|
$enc.data.set_len(old_len + encoded.len());
|
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
|
|
|
|
Ok(())
|
2019-12-22 17:42:04 -05:00
|
|
|
}};
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2021-12-01 00:31:46 +01:00
|
|
|
/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
|
|
|
|
/// This way we can skip validation and still be relatively sure that deserialization
|
|
|
|
/// did not desynchronize.
|
|
|
|
///
|
|
|
|
/// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
|
|
|
|
const STR_SENTINEL: u8 = 0xC1;
|
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
impl serialize::Encoder for Encoder {
|
|
|
|
type Error = !;
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2018-09-11 23:32:41 +09:00
|
|
|
fn emit_unit(&mut self) -> EncodeResult {
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2016-08-24 02:20:51 -04:00
|
|
|
fn emit_usize(&mut self, v: usize) -> EncodeResult {
|
2020-12-06 17:30:55 -08:00
|
|
|
write_leb128!(self, v, usize, write_usize_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2016-08-23 03:56:52 +03:00
|
|
|
fn emit_u128(&mut self, v: u128) -> EncodeResult {
|
2020-12-06 17:30:55 -08:00
|
|
|
write_leb128!(self, v, u128, write_u128_leb128)
|
2016-08-23 03:56:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_u64(&mut self, v: u64) -> EncodeResult {
|
2020-12-06 17:30:55 -08:00
|
|
|
write_leb128!(self, v, u64, write_u64_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_u32(&mut self, v: u32) -> EncodeResult {
|
2020-12-06 17:30:55 -08:00
|
|
|
write_leb128!(self, v, u32, write_u32_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_u16(&mut self, v: u16) -> EncodeResult {
|
2021-12-28 09:29:08 +01:00
|
|
|
self.data.extend_from_slice(&v.to_le_bytes());
|
|
|
|
Ok(())
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_u8(&mut self, v: u8) -> EncodeResult {
|
2018-06-04 22:14:02 +02:00
|
|
|
self.data.push(v);
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2016-08-24 02:20:51 -04:00
|
|
|
fn emit_isize(&mut self, v: isize) -> EncodeResult {
|
2020-12-06 17:30:55 -08:00
|
|
|
write_leb128!(self, v, isize, write_isize_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2016-08-23 03:56:52 +03:00
|
|
|
fn emit_i128(&mut self, v: i128) -> EncodeResult {
|
2020-12-06 17:30:55 -08:00
|
|
|
write_leb128!(self, v, i128, write_i128_leb128)
|
2016-08-23 03:56:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_i64(&mut self, v: i64) -> EncodeResult {
|
2020-12-06 17:30:55 -08:00
|
|
|
write_leb128!(self, v, i64, write_i64_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_i32(&mut self, v: i32) -> EncodeResult {
|
2020-12-06 17:30:55 -08:00
|
|
|
write_leb128!(self, v, i32, write_i32_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_i16(&mut self, v: i16) -> EncodeResult {
|
2021-12-28 09:29:08 +01:00
|
|
|
self.data.extend_from_slice(&v.to_le_bytes());
|
|
|
|
Ok(())
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_i8(&mut self, v: i8) -> EncodeResult {
|
2022-01-29 14:36:35 +01:00
|
|
|
self.emit_u8(v as u8)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_bool(&mut self, v: bool) -> EncodeResult {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.emit_u8(if v { 1 } else { 0 })
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_f64(&mut self, v: f64) -> EncodeResult {
|
2020-08-06 03:30:02 +06:00
|
|
|
let as_u64: u64 = v.to_bits();
|
2015-12-25 13:59:02 -05:00
|
|
|
self.emit_u64(as_u64)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_f32(&mut self, v: f32) -> EncodeResult {
|
2020-08-06 03:30:02 +06:00
|
|
|
let as_u32: u32 = v.to_bits();
|
2015-12-25 13:59:02 -05:00
|
|
|
self.emit_u32(as_u32)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_char(&mut self, v: char) -> EncodeResult {
|
|
|
|
self.emit_u32(v as u32)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_str(&mut self, v: &str) -> EncodeResult {
|
2016-08-24 02:20:51 -04:00
|
|
|
self.emit_usize(v.len())?;
|
2021-12-01 00:31:46 +01:00
|
|
|
self.emit_raw_bytes(v.as_bytes())?;
|
|
|
|
self.emit_u8(STR_SENTINEL)
|
2021-03-11 22:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_raw_bytes(&mut self, s: &[u8]) -> EncodeResult {
|
|
|
|
self.data.extend_from_slice(s);
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-06 17:30:55 -08:00
|
|
|
pub type FileEncodeResult = Result<(), io::Error>;
|
|
|
|
|
|
|
|
// `FileEncoder` encodes data to file via fixed-size buffer.
|
|
|
|
//
|
|
|
|
// When encoding large amounts of data to a file, using `FileEncoder` may be
|
|
|
|
// preferred over using `Encoder` to encode to a `Vec`, and then writing the
|
|
|
|
// `Vec` to file, as the latter uses as much memory as there is encoded data,
|
|
|
|
// while the former uses the fixed amount of memory allocated to the buffer.
|
|
|
|
// `FileEncoder` also has the advantage of not needing to reallocate as data
|
|
|
|
// is appended to it, but the disadvantage of requiring more error handling,
|
|
|
|
// which has some runtime overhead.
|
|
|
|
pub struct FileEncoder {
|
|
|
|
// The input buffer. For adequate performance, we need more control over
|
|
|
|
// buffering than `BufWriter` offers. If `BufWriter` ever offers a raw
|
|
|
|
// buffer access API, we can use it, and remove `buf` and `buffered`.
|
|
|
|
buf: Box<[MaybeUninit<u8>]>,
|
|
|
|
buffered: usize,
|
|
|
|
flushed: usize,
|
|
|
|
file: File,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileEncoder {
|
|
|
|
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
|
|
|
|
const DEFAULT_BUF_SIZE: usize = 8192;
|
|
|
|
FileEncoder::with_capacity(path, DEFAULT_BUF_SIZE)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_capacity<P: AsRef<Path>>(path: P, capacity: usize) -> io::Result<Self> {
|
|
|
|
// Require capacity at least as large as the largest LEB128 encoding
|
|
|
|
// here, so that we don't have to check or handle this on every write.
|
|
|
|
assert!(capacity >= max_leb128_len());
|
|
|
|
|
|
|
|
// Require capacity small enough such that some capacity checks can be
|
|
|
|
// done using guaranteed non-overflowing add rather than sub, which
|
|
|
|
// shaves an instruction off those code paths (on x86 at least).
|
|
|
|
assert!(capacity <= usize::MAX - max_leb128_len());
|
|
|
|
|
|
|
|
let file = File::create(path)?;
|
|
|
|
|
|
|
|
Ok(FileEncoder { buf: Box::new_uninit_slice(capacity), buffered: 0, flushed: 0, file })
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn position(&self) -> usize {
|
2020-12-06 17:30:55 -08:00
|
|
|
// Tracking position this way instead of having a `self.position` field
|
|
|
|
// means that we don't have to update the position on every write call.
|
|
|
|
self.flushed + self.buffered
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn flush(&mut self) -> FileEncodeResult {
|
|
|
|
// This is basically a copy of `BufWriter::flush`. If `BufWriter` ever
|
|
|
|
// offers a raw buffer access API, we can use it, and remove this.
|
|
|
|
|
|
|
|
/// Helper struct to ensure the buffer is updated after all the writes
|
|
|
|
/// are complete. It tracks the number of written bytes and drains them
|
|
|
|
/// all from the front of the buffer when dropped.
|
|
|
|
struct BufGuard<'a> {
|
|
|
|
buffer: &'a mut [u8],
|
|
|
|
encoder_buffered: &'a mut usize,
|
|
|
|
encoder_flushed: &'a mut usize,
|
|
|
|
flushed: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BufGuard<'a> {
|
|
|
|
fn new(
|
|
|
|
buffer: &'a mut [u8],
|
|
|
|
encoder_buffered: &'a mut usize,
|
|
|
|
encoder_flushed: &'a mut usize,
|
|
|
|
) -> Self {
|
|
|
|
assert_eq!(buffer.len(), *encoder_buffered);
|
|
|
|
Self { buffer, encoder_buffered, encoder_flushed, flushed: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The unwritten part of the buffer
|
|
|
|
fn remaining(&self) -> &[u8] {
|
|
|
|
&self.buffer[self.flushed..]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Flag some bytes as removed from the front of the buffer
|
|
|
|
fn consume(&mut self, amt: usize) {
|
|
|
|
self.flushed += amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// true if all of the bytes have been written
|
|
|
|
fn done(&self) -> bool {
|
|
|
|
self.flushed >= *self.encoder_buffered
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for BufGuard<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.flushed > 0 {
|
|
|
|
if self.done() {
|
|
|
|
*self.encoder_flushed += *self.encoder_buffered;
|
|
|
|
*self.encoder_buffered = 0;
|
|
|
|
} else {
|
|
|
|
self.buffer.copy_within(self.flushed.., 0);
|
|
|
|
*self.encoder_flushed += self.flushed;
|
|
|
|
*self.encoder_buffered -= self.flushed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut guard = BufGuard::new(
|
|
|
|
unsafe { MaybeUninit::slice_assume_init_mut(&mut self.buf[..self.buffered]) },
|
|
|
|
&mut self.buffered,
|
|
|
|
&mut self.flushed,
|
|
|
|
);
|
|
|
|
|
|
|
|
while !guard.done() {
|
|
|
|
match self.file.write(guard.remaining()) {
|
|
|
|
Ok(0) => {
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::WriteZero,
|
|
|
|
"failed to write the buffered data",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(n) => guard.consume(n),
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
self.buf.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn write_one(&mut self, value: u8) -> FileEncodeResult {
|
|
|
|
// We ensure this during `FileEncoder` construction.
|
|
|
|
debug_assert!(self.capacity() >= 1);
|
|
|
|
|
|
|
|
let mut buffered = self.buffered;
|
|
|
|
|
|
|
|
if std::intrinsics::unlikely(buffered >= self.capacity()) {
|
|
|
|
self.flush()?;
|
|
|
|
buffered = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SAFETY: The above check and `flush` ensures that there is enough
|
|
|
|
// room to write the input to the buffer.
|
|
|
|
unsafe {
|
|
|
|
*MaybeUninit::slice_as_mut_ptr(&mut self.buf).add(buffered) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.buffered = buffered + 1;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn write_all(&mut self, buf: &[u8]) -> FileEncodeResult {
|
|
|
|
let capacity = self.capacity();
|
|
|
|
let buf_len = buf.len();
|
|
|
|
|
|
|
|
if std::intrinsics::likely(buf_len <= capacity) {
|
|
|
|
let mut buffered = self.buffered;
|
|
|
|
|
|
|
|
if std::intrinsics::unlikely(buf_len > capacity - buffered) {
|
|
|
|
self.flush()?;
|
|
|
|
buffered = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SAFETY: The above check and `flush` ensures that there is enough
|
|
|
|
// room to write the input to the buffer.
|
|
|
|
unsafe {
|
|
|
|
let src = buf.as_ptr();
|
|
|
|
let dst = MaybeUninit::slice_as_mut_ptr(&mut self.buf).add(buffered);
|
|
|
|
ptr::copy_nonoverlapping(src, dst, buf_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.buffered = buffered + buf_len;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
self.write_all_unbuffered(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_all_unbuffered(&mut self, mut buf: &[u8]) -> FileEncodeResult {
|
|
|
|
if self.buffered > 0 {
|
|
|
|
self.flush()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is basically a copy of `Write::write_all` but also updates our
|
|
|
|
// `self.flushed`. It's necessary because `Write::write_all` does not
|
|
|
|
// return the number of bytes written when an error is encountered, and
|
|
|
|
// without that, we cannot accurately update `self.flushed` on error.
|
|
|
|
while !buf.is_empty() {
|
|
|
|
match self.file.write(buf) {
|
|
|
|
Ok(0) => {
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::WriteZero,
|
|
|
|
"failed to write whole buffer",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(n) => {
|
|
|
|
buf = &buf[n..];
|
|
|
|
self.flushed += n;
|
|
|
|
}
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for FileEncoder {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let _result = self.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! file_encoder_write_leb128 {
|
|
|
|
($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
|
|
|
|
const MAX_ENCODED_LEN: usize = max_leb128_len!($int_ty);
|
|
|
|
|
|
|
|
// We ensure this during `FileEncoder` construction.
|
|
|
|
debug_assert!($enc.capacity() >= MAX_ENCODED_LEN);
|
|
|
|
|
|
|
|
let mut buffered = $enc.buffered;
|
|
|
|
|
|
|
|
// This can't overflow. See assertion in `FileEncoder::with_capacity`.
|
|
|
|
if std::intrinsics::unlikely(buffered + MAX_ENCODED_LEN > $enc.capacity()) {
|
|
|
|
$enc.flush()?;
|
|
|
|
buffered = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SAFETY: The above check and flush ensures that there is enough
|
|
|
|
// room to write the encoded value to the buffer.
|
|
|
|
let buf = unsafe {
|
|
|
|
&mut *($enc.buf.as_mut_ptr().add(buffered) as *mut [MaybeUninit<u8>; MAX_ENCODED_LEN])
|
|
|
|
};
|
|
|
|
|
|
|
|
let encoded = leb128::$fun(buf, $value);
|
|
|
|
$enc.buffered = buffered + encoded.len();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl serialize::Encoder for FileEncoder {
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_unit(&mut self) -> FileEncodeResult {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_usize(&mut self, v: usize) -> FileEncodeResult {
|
|
|
|
file_encoder_write_leb128!(self, v, usize, write_usize_leb128)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_u128(&mut self, v: u128) -> FileEncodeResult {
|
|
|
|
file_encoder_write_leb128!(self, v, u128, write_u128_leb128)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_u64(&mut self, v: u64) -> FileEncodeResult {
|
|
|
|
file_encoder_write_leb128!(self, v, u64, write_u64_leb128)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_u32(&mut self, v: u32) -> FileEncodeResult {
|
|
|
|
file_encoder_write_leb128!(self, v, u32, write_u32_leb128)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_u16(&mut self, v: u16) -> FileEncodeResult {
|
2021-12-28 09:29:08 +01:00
|
|
|
self.write_all(&v.to_le_bytes())
|
2020-12-06 17:30:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_u8(&mut self, v: u8) -> FileEncodeResult {
|
|
|
|
self.write_one(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_isize(&mut self, v: isize) -> FileEncodeResult {
|
|
|
|
file_encoder_write_leb128!(self, v, isize, write_isize_leb128)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_i128(&mut self, v: i128) -> FileEncodeResult {
|
|
|
|
file_encoder_write_leb128!(self, v, i128, write_i128_leb128)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_i64(&mut self, v: i64) -> FileEncodeResult {
|
|
|
|
file_encoder_write_leb128!(self, v, i64, write_i64_leb128)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_i32(&mut self, v: i32) -> FileEncodeResult {
|
|
|
|
file_encoder_write_leb128!(self, v, i32, write_i32_leb128)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_i16(&mut self, v: i16) -> FileEncodeResult {
|
2021-12-28 09:29:08 +01:00
|
|
|
self.write_all(&v.to_le_bytes())
|
2020-12-06 17:30:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_i8(&mut self, v: i8) -> FileEncodeResult {
|
2021-12-28 09:29:08 +01:00
|
|
|
self.emit_u8(v as u8)
|
2020-12-06 17:30:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_bool(&mut self, v: bool) -> FileEncodeResult {
|
|
|
|
self.emit_u8(if v { 1 } else { 0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_f64(&mut self, v: f64) -> FileEncodeResult {
|
|
|
|
let as_u64: u64 = v.to_bits();
|
|
|
|
self.emit_u64(as_u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_f32(&mut self, v: f32) -> FileEncodeResult {
|
|
|
|
let as_u32: u32 = v.to_bits();
|
|
|
|
self.emit_u32(as_u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_char(&mut self, v: char) -> FileEncodeResult {
|
|
|
|
self.emit_u32(v as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_str(&mut self, v: &str) -> FileEncodeResult {
|
|
|
|
self.emit_usize(v.len())?;
|
2021-12-01 00:31:46 +01:00
|
|
|
self.emit_raw_bytes(v.as_bytes())?;
|
|
|
|
self.emit_u8(STR_SENTINEL)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
2021-03-11 22:06:45 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_raw_bytes(&mut self, s: &[u8]) -> FileEncodeResult {
|
|
|
|
self.write_all(s)
|
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-01-19 14:39:23 +13:00
|
|
|
// -----------------------------------------------------------------------------
|
2015-12-25 13:59:02 -05:00
|
|
|
// Decoder
|
2016-01-19 14:39:23 +13:00
|
|
|
// -----------------------------------------------------------------------------
|
2015-12-25 13:59:02 -05:00
|
|
|
|
|
|
|
pub struct Decoder<'a> {
|
|
|
|
pub data: &'a [u8],
|
|
|
|
position: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Decoder<'a> {
|
2018-12-05 18:59:48 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> {
|
2019-12-22 17:42:04 -05:00
|
|
|
Decoder { data, position }
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn position(&self) -> usize {
|
|
|
|
self.position
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2017-11-28 16:58:02 +01:00
|
|
|
pub fn set_position(&mut self, pos: usize) {
|
|
|
|
self.position = pos
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn advance(&mut self, bytes: usize) {
|
|
|
|
self.position += bytes;
|
|
|
|
}
|
2021-03-25 11:43:03 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
|
|
|
|
let start = self.position;
|
|
|
|
self.position += bytes;
|
|
|
|
&self.data[start..self.position]
|
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-29 15:14:33 -08:00
|
|
|
macro_rules! read_leb128 {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
($dec:expr, $fun:ident) => {{ leb128::$fun($dec.data, &mut $dec.position) }};
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> serialize::Decoder for Decoder<'a> {
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_unit(&mut self) -> () {
|
|
|
|
()
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:56:52 +03:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_u128(&mut self) -> u128 {
|
2020-12-29 15:14:33 -08:00
|
|
|
read_leb128!(self, read_u128_leb128)
|
2016-08-23 03:56:52 +03:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_u64(&mut self) -> u64 {
|
2020-12-29 15:14:33 -08:00
|
|
|
read_leb128!(self, read_u64_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_u32(&mut self) -> u32 {
|
2020-12-29 15:14:33 -08:00
|
|
|
read_leb128!(self, read_u32_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_u16(&mut self) -> u16 {
|
2021-12-28 09:29:08 +01:00
|
|
|
let bytes = [self.data[self.position], self.data[self.position + 1]];
|
|
|
|
let value = u16::from_le_bytes(bytes);
|
|
|
|
self.position += 2;
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
value
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_u8(&mut self) -> u8 {
|
2015-12-25 13:59:02 -05:00
|
|
|
let value = self.data[self.position];
|
|
|
|
self.position += 1;
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
value
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_usize(&mut self) -> usize {
|
2020-12-29 15:14:33 -08:00
|
|
|
read_leb128!(self, read_usize_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:56:52 +03:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_i128(&mut self) -> i128 {
|
2020-12-29 15:14:33 -08:00
|
|
|
read_leb128!(self, read_i128_leb128)
|
2016-08-23 03:56:52 +03:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_i64(&mut self) -> i64 {
|
2020-12-29 15:14:33 -08:00
|
|
|
read_leb128!(self, read_i64_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_i32(&mut self) -> i32 {
|
2020-12-29 15:14:33 -08:00
|
|
|
read_leb128!(self, read_i32_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_i16(&mut self) -> i16 {
|
2021-12-28 09:29:08 +01:00
|
|
|
let bytes = [self.data[self.position], self.data[self.position + 1]];
|
|
|
|
let value = i16::from_le_bytes(bytes);
|
|
|
|
self.position += 2;
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
value
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_i8(&mut self) -> i8 {
|
2022-01-29 14:36:35 +01:00
|
|
|
let value = self.data[self.position];
|
2015-12-25 13:59:02 -05:00
|
|
|
self.position += 1;
|
2022-01-29 14:36:35 +01:00
|
|
|
value as i8
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_isize(&mut self) -> isize {
|
2020-12-29 15:14:33 -08:00
|
|
|
read_leb128!(self, read_isize_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_bool(&mut self) -> bool {
|
|
|
|
let value = self.read_u8();
|
|
|
|
value != 0
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_f64(&mut self) -> f64 {
|
|
|
|
let bits = self.read_u64();
|
|
|
|
f64::from_bits(bits)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_f32(&mut self) -> f32 {
|
|
|
|
let bits = self.read_u32();
|
|
|
|
f32::from_bits(bits)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn read_char(&mut self) -> char {
|
|
|
|
let bits = self.read_u32();
|
|
|
|
std::char::from_u32(bits).unwrap()
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2022-02-22 18:05:51 -05:00
|
|
|
fn read_str(&mut self) -> &str {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
let len = self.read_usize();
|
2021-12-01 00:31:46 +01:00
|
|
|
let sentinel = self.data[self.position + len];
|
|
|
|
assert!(sentinel == STR_SENTINEL);
|
|
|
|
let s = unsafe {
|
|
|
|
std::str::from_utf8_unchecked(&self.data[self.position..self.position + len])
|
|
|
|
};
|
|
|
|
self.position += len + 1;
|
2022-02-22 18:05:51 -05:00
|
|
|
s
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
2021-03-11 22:06:45 +01:00
|
|
|
|
|
|
|
#[inline]
|
2022-01-20 09:59:30 +11:00
|
|
|
fn read_raw_bytes_into(&mut self, s: &mut [u8]) {
|
2021-03-11 22:06:45 +01:00
|
|
|
let start = self.position;
|
2021-03-25 11:43:03 +01:00
|
|
|
self.position += s.len();
|
|
|
|
s.copy_from_slice(&self.data[start..self.position]);
|
2021-03-11 22:06:45 +01:00
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
2020-12-16 19:03:31 -08:00
|
|
|
|
2020-12-16 21:03:45 -08:00
|
|
|
// Specializations for contiguous byte sequences follow. The default implementations for slices
|
|
|
|
// encode and decode each element individually. This isn't necessary for `u8` slices when using
|
|
|
|
// opaque encoders and decoders, because each `u8` is unchanged by encoding and decoding.
|
|
|
|
// Therefore, we can use more efficient implementations that process the entire sequence at once.
|
|
|
|
|
|
|
|
// Specialize encoding byte slices. This specialization also applies to encoding `Vec<u8>`s, etc.,
|
|
|
|
// since the default implementations call `encode` on their slices internally.
|
2020-12-16 19:03:31 -08:00
|
|
|
impl serialize::Encodable<Encoder> for [u8] {
|
|
|
|
fn encode(&self, e: &mut Encoder) -> EncodeResult {
|
|
|
|
serialize::Encoder::emit_usize(e, self.len())?;
|
2021-03-11 22:06:45 +01:00
|
|
|
e.emit_raw_bytes(self)
|
2020-12-16 19:03:31 -08:00
|
|
|
}
|
|
|
|
}
|
2020-12-16 21:03:45 -08:00
|
|
|
|
2020-12-06 17:30:55 -08:00
|
|
|
impl serialize::Encodable<FileEncoder> for [u8] {
|
|
|
|
fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult {
|
|
|
|
serialize::Encoder::emit_usize(e, self.len())?;
|
|
|
|
e.emit_raw_bytes(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 21:03:45 -08:00
|
|
|
// Specialize decoding `Vec<u8>`. This specialization also applies to decoding `Box<[u8]>`s, etc.,
|
|
|
|
// since the default implementations call `decode` to produce a `Vec<u8>` internally.
|
|
|
|
impl<'a> serialize::Decodable<Decoder<'a>> for Vec<u8> {
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn decode(d: &mut Decoder<'a>) -> Self {
|
|
|
|
let len = serialize::Decoder::read_usize(d);
|
|
|
|
d.read_raw_bytes(len).to_owned()
|
2020-12-16 21:03:45 -08:00
|
|
|
}
|
|
|
|
}
|
2021-03-04 19:24:11 +01:00
|
|
|
|
|
|
|
// An integer that will always encode to 8 bytes.
|
|
|
|
pub struct IntEncodedWithFixedSize(pub u64);
|
|
|
|
|
|
|
|
impl IntEncodedWithFixedSize {
|
|
|
|
pub const ENCODED_SIZE: usize = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl serialize::Encodable<Encoder> for IntEncodedWithFixedSize {
|
2021-03-11 22:06:45 +01:00
|
|
|
#[inline]
|
2021-03-04 19:24:11 +01:00
|
|
|
fn encode(&self, e: &mut Encoder) -> EncodeResult {
|
2021-03-11 22:06:45 +01:00
|
|
|
let _start_pos = e.position();
|
|
|
|
e.emit_raw_bytes(&self.0.to_le_bytes())?;
|
|
|
|
let _end_pos = e.position();
|
|
|
|
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
|
2021-03-04 19:24:11 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize {
|
2021-03-11 22:06:45 +01:00
|
|
|
#[inline]
|
2021-03-04 19:24:11 +01:00
|
|
|
fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult {
|
2021-03-11 22:06:45 +01:00
|
|
|
let _start_pos = e.position();
|
2021-03-18 18:54:01 +01:00
|
|
|
e.emit_raw_bytes(&self.0.to_le_bytes())?;
|
2021-03-11 22:06:45 +01:00
|
|
|
let _end_pos = e.position();
|
|
|
|
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
|
2021-03-04 19:24:11 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
|
2021-03-11 22:06:45 +01:00
|
|
|
#[inline]
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize {
|
2021-03-11 22:06:45 +01:00
|
|
|
let _start_pos = decoder.position();
|
2021-03-25 11:43:03 +01:00
|
|
|
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
|
2021-03-11 22:06:45 +01:00
|
|
|
let _end_pos = decoder.position();
|
|
|
|
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
|
2021-03-04 19:24:11 +01:00
|
|
|
|
2021-03-25 11:43:03 +01:00
|
|
|
let value = u64::from_le_bytes(bytes.try_into().unwrap());
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 13:22:50 +11:00
|
|
|
IntEncodedWithFixedSize(value)
|
2021-03-04 19:24:11 +01:00
|
|
|
}
|
|
|
|
}
|