Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.83.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC)
Options
Source code
#![feature(bench_black_box)] use std::marker::PhantomData; pub struct BigBuf { buf: Vec<u8>, } impl BigBuf { pub fn new(sz: usize) -> Self { // Fill with data to ensure that the memory is mapped let mut buf = Vec::with_capacity(sz); let mut ctr = 128u8; for _ in 0..sz { ctr = ctr.wrapping_add(1); buf.push(ctr); } Self { buf } } } pub struct Bbpv1<'a> { sli: &'a [u8] } impl<'a> ProdV1 for Bbpv1<'a> { fn try_take(&mut self) -> Option<u8> { let (f, rest) = self.sli.split_first()?; self.sli = rest; Some(*f) } fn try_take_n(&mut self, ct: usize) -> Option<&[u8]> { if ct <= self.sli.len() { let (st, rest) = self.sli.split_at(ct); self.sli = rest; Some(st) } else { None } } fn ctry_take_n<const N: usize>(&mut self) -> Option<&[u8; N]> { todo!() } } pub struct Bbcv1<'a> { sli: &'a mut [u8], } pub struct Bbcv2<'a> { sli: &'a mut [u8], idx: usize, } pub struct Bbcv3<'a> { cursor: *mut u8, end: *mut u8, _lt: PhantomData<&'a u8> } impl BigBuf { pub fn as_cons<'a>(&'a mut self) -> Bbcv1<'a> { Bbcv1 { sli: self.buf.as_mut_slice(), } } pub fn as_cons2<'a>(&'a mut self) -> Bbcv2<'a> { Bbcv2 { sli: self.buf.as_mut_slice(), idx: 0, } } pub fn as_cons3<'a>(&'a mut self) -> Bbcv3<'a> { Bbcv3 { cursor: self.buf.as_mut_ptr(), end: unsafe { self.buf.as_mut_ptr().add(self.buf.len()) }, _lt: PhantomData, } } } impl<'a> ConsV1 for Bbcv1<'a> { fn try_push(&mut self, b: u8) -> Result<(), ()> { if self.sli.len() == 0 { return Err(()) } let sli = core::mem::take(&mut self.sli); sli[0] = b; self.sli = &mut sli[1..]; Ok(()) } fn try_extend(&mut self, b: &[u8]) -> Result<(), ()> { let blen = b.len(); if blen > self.sli.len() { Err(()) } else { let sli = core::mem::take(&mut self.sli); let (now, later) = sli.split_at_mut(blen); now.copy_from_slice(b); self.sli = later; Ok(()) } } } impl<'a> ConsV1 for Bbcv2<'a> { fn try_push(&mut self, b: u8) -> Result<(), ()> { *self.sli.get_mut(self.idx).ok_or(())? = b; self.idx = self.idx.wrapping_add(1); Ok(()) } fn try_extend(&mut self, b: &[u8]) -> Result<(), ()> { let blen = b.len(); if (blen + self.idx) > self.sli.len() { Err(()) } else { self.sli[self.idx..][..blen].copy_from_slice(b); self.idx += blen; Ok(()) } } } impl<'a> ConsV1 for Bbcv3<'a> { fn try_push(&mut self, b: u8) -> Result<(), ()> { if self.cursor == self.end { Err(()) } else { unsafe { self.cursor.write(b); self.cursor = self.cursor.add(1); } Ok(()) } } fn try_extend(&mut self, b: &[u8]) -> Result<(), ()> { let remain = (self.end as usize) - (self.cursor as usize); let blen = b.len(); if blen > remain { Err(()) } else { unsafe { core::ptr::copy_nonoverlapping(b.as_ptr(), self.cursor, blen); self.cursor = self.cursor.add(blen); } Ok(()) } } fn try_extend_with<F, const N: usize>(&mut self, f: F) -> Result<(), ()> where F: FnOnce(&mut [u8; N]) { let remain = (self.end as usize) - (self.cursor as usize); if N > remain { Err(()) } else { unsafe { f(&mut *self.cursor.cast::<[u8; N]>()); self.cursor = self.cursor.add(N); } Ok(()) } } } pub trait ProdV1 { fn try_take(&mut self) -> Option<u8>; fn try_take_n(&mut self, ct: usize) -> Option<&[u8]>; fn ctry_take_n<const N: usize>(&mut self) -> Option<&[u8; N]>; } pub trait ConsV1 { fn try_push(&mut self, b: u8) -> Result<(), ()>; fn try_extend(&mut self, b: &'_ [u8]) -> Result<(), ()>; fn try_extend_with<F, const N: usize>(&mut self, _f: F) -> Result<(), ()> where F: FnOnce(&mut [u8; N]) { todo!() } } pub fn smoke() { let mut buf = BigBuf::new(1024); let x = std::hint::black_box(2usize); let mut buffy = [0u8; 8]; let buffy = std::hint::black_box(buffy); let mut cons = buf.as_cons3(); // cons.try_extend_with::<_, 1>(|b| b[0] = 1).unwrap(); // cons.try_extend_with::<_, 2>(|b| b.copy_from_slice(&[0xAC, 0xDF])).unwrap(); // cons.try_extend_with::<_, 4>(|b| b.copy_from_slice(&[0xAC, 0xDF, 0xAC, 0xDF])).unwrap(); // cons.try_extend_with::<_, 9>(|b| b.copy_from_slice(&[0xAC, 0xDF, 0xAC, 0xDF, 0x00, 0xAC, 0xDF, 0xAC, 0xDF])).unwrap(); match x { 1 => cons.try_extend_with::<_, 1>(|b| b[0] = buffy[0]), 2 => cons.try_extend_with::<_, 2>(|b| b.copy_from_slice(&buffy[..2])), 3 => cons.try_extend_with::<_, 3>(|b| b.copy_from_slice(&buffy[..3])), 4 => cons.try_extend_with::<_, 4>(|b| b.copy_from_slice(&buffy[..4])), 5 => cons.try_extend_with::<_, 5>(|b| b.copy_from_slice(&buffy[..5])), 6 => cons.try_extend_with::<_, 6>(|b| b.copy_from_slice(&buffy[..6])), 7 => cons.try_extend_with::<_, 7>(|b| b.copy_from_slice(&buffy[..7])), 8 => cons.try_extend_with::<_, 8>(|b| b.copy_from_slice(&buffy[..8])), _ => unsafe { core::hint::unreachable_unchecked() }, }.unwrap(); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree