sailfish/sailfish/src/buffer.rs

493 lines
12 KiB
Rust
Raw Normal View History

use std::alloc::{alloc, dealloc, handle_alloc_error, realloc, Layout};
2020-06-04 16:39:33 -04:00
use std::fmt;
use std::mem::{align_of, ManuallyDrop};
2020-06-04 16:39:33 -04:00
use std::ops::{Add, AddAssign};
use std::ptr;
2020-06-06 06:16:08 -04:00
/// Buffer for rendered contents
///
/// This struct is quite simular to `String`, but some methods are
/// re-implemented for faster buffering.
2020-06-04 16:39:33 -04:00
pub struct Buffer {
data: *mut u8,
len: usize,
capacity: usize,
2020-06-04 16:39:33 -04:00
}
impl Buffer {
/// Create an empty buffer
2020-06-04 16:39:33 -04:00
#[inline]
pub const fn new() -> Buffer {
Self {
data: align_of::<u8>() as *mut u8, // dangling pointer
len: 0,
capacity: 0,
2020-06-04 16:39:33 -04:00
}
}
2020-12-20 04:23:32 -05:00
/// Create a empty buffer with a particular capacity
#[inline]
2020-06-04 16:39:33 -04:00
pub fn with_capacity(n: usize) -> Buffer {
if unlikely!(n == 0) {
Self::new()
} else {
Self {
data: safe_alloc(n),
len: 0,
capacity: n,
}
2020-06-04 16:39:33 -04:00
}
}
2020-12-20 04:23:32 -05:00
/// Extracts a string slice containing the entire buffer
2020-06-04 16:39:33 -04:00
#[inline]
pub fn as_str(&self) -> &str {
unsafe {
let bytes = std::slice::from_raw_parts(self.data, self.len);
std::str::from_utf8_unchecked(bytes)
}
2020-06-04 16:39:33 -04:00
}
2020-12-20 04:23:32 -05:00
/// Returns an unsafe mutable pointer to the inner data
2020-06-10 05:41:12 -04:00
#[inline]
pub fn as_mut_ptr(&self) -> *mut u8 {
self.data
}
2020-12-20 04:23:32 -05:00
/// Returns the length of this buffer in bytes
2020-06-04 16:39:33 -04:00
#[inline]
pub fn len(&self) -> usize {
self.len
2020-06-04 16:39:33 -04:00
}
2020-12-20 04:23:32 -05:00
/// Returns this buffer's capacity in bytes
2020-06-04 16:39:33 -04:00
#[inline]
pub fn capacity(&self) -> usize {
self.capacity
2020-06-04 16:39:33 -04:00
}
#[inline]
#[doc(hidden)]
pub unsafe fn _set_len(&mut self, new_len: usize) {
2020-12-24 01:00:19 -05:00
debug_assert!(new_len <= self.capacity);
self.len = new_len;
}
/// Increase the length of buffer by `additional` bytes
///
/// # Safety
///
/// - `additional` must be less than or equal to `capacity() - len()`
/// - The elements at `old_len..old_len + additional` must be initialized
2020-06-04 16:39:33 -04:00
#[inline]
pub unsafe fn advance(&mut self, additional: usize) {
self.len += additional;
}
2020-12-20 04:23:32 -05:00
/// Returns `true` if this buffer has a length of zero, and `false` otherwise
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
2020-06-04 16:39:33 -04:00
}
/// Same as String::reserve
///
/// # Panics
///
/// This method panics if `size` overflows `isize::MAX`.
#[inline]
pub fn reserve(&mut self, size: usize) {
2020-12-19 07:07:09 -05:00
if likely!(size <= self.capacity - self.len) {
return;
}
self.reserve_internal(size);
}
/// Same as String::reserve except that undefined behaviour can result if `size`
/// overflows `isize::MAX`.
#[inline]
pub(crate) unsafe fn reserve_small(&mut self, size: usize) {
debug_assert!(size <= std::isize::MAX as usize);
2020-12-19 07:07:09 -05:00
if likely!(self.len + size <= self.capacity) {
return;
2020-06-04 16:39:33 -04:00
}
self.reserve_internal(size);
2020-06-04 16:39:33 -04:00
}
#[inline]
#[doc(hidden)]
2020-06-04 16:39:33 -04:00
pub fn clear(&mut self) {
self.len = 0;
2020-06-04 16:39:33 -04:00
}
/// Converts a `Buffer` into a `String` without copy/realloc operation.
2020-06-04 16:39:33 -04:00
#[inline]
pub fn into_string(self) -> String {
2020-07-10 13:39:04 -04:00
debug_assert!(self.len <= self.capacity);
let buf = ManuallyDrop::new(self);
// SAFETY: This operations satisfy all requirements specified in
// https://doc.rust-lang.org/std/string/struct.String.html#safety
unsafe { String::from_raw_parts(buf.data, buf.len, buf.capacity) }
2020-06-04 16:39:33 -04:00
}
2020-12-20 04:23:32 -05:00
/// Appends a given string slice onto the end of this buffer
2020-06-04 16:39:33 -04:00
#[inline]
pub fn push_str(&mut self, data: &str) {
2020-06-04 16:39:33 -04:00
let size = data.len();
2020-06-04 16:39:33 -04:00
unsafe {
// SAFETY: this operation won't overflow because slice cannot exceeds
// isize::MAX bytes.
// https://doc.rust-lang.org/reference/behavior-considered-undefined.html
self.reserve_small(size);
let p = self.data.add(self.len);
2020-06-04 16:39:33 -04:00
std::ptr::copy_nonoverlapping(data.as_ptr(), p, size);
self.len += size;
2020-06-04 16:39:33 -04:00
}
2020-07-10 13:39:04 -04:00
debug_assert!(self.len <= self.capacity);
2020-06-04 16:39:33 -04:00
}
2020-12-20 04:23:32 -05:00
/// Appends the given `char` to the end of this buffer
2020-06-04 16:39:33 -04:00
#[inline]
pub fn push(&mut self, data: char) {
2020-12-16 07:52:39 -05:00
// Question: Is it safe to pass uninitialized memory to `encode_utf8` function?
unsafe {
self.reserve_small(4);
let bp = self.data.add(self.len) as *mut [u8; 4];
let result = data.encode_utf8(&mut *bp);
self.len += result.len();
}
}
2020-06-18 04:27:39 -04:00
#[cfg_attr(feature = "perf-inline", inline)]
fn reserve_internal(&mut self, size: usize) {
debug_assert!(size <= std::isize::MAX as usize);
let new_capacity = std::cmp::max(self.capacity * 2, self.capacity + size);
debug_assert!(new_capacity > self.capacity);
self.data = unsafe { safe_realloc(self.data, self.capacity, new_capacity) };
self.capacity = new_capacity;
2020-07-10 13:39:04 -04:00
debug_assert!(!self.data.is_null());
debug_assert!(self.len <= self.capacity);
}
2020-07-05 00:25:57 -04:00
}
#[inline(never)]
fn safe_alloc(capacity: usize) -> *mut u8 {
assert!(capacity > 0);
assert!(
capacity <= std::isize::MAX as usize,
"capacity is too large"
);
// SAFETY: capacity is non-zero, and always multiple of alignment (1).
unsafe {
let layout = Layout::from_size_align_unchecked(capacity, 1);
let data = alloc(layout);
if data.is_null() {
handle_alloc_error(layout);
}
data
}
}
/// # Safety
///
/// - if `capacity > 0`, `capacity` is the same value that was used to allocate the block
/// of memory pointed by `ptr`.
2020-07-05 00:25:57 -04:00
#[cold]
#[inline(never)]
unsafe fn safe_realloc(ptr: *mut u8, capacity: usize, new_capacity: usize) -> *mut u8 {
assert!(new_capacity > 0);
assert!(
new_capacity <= std::isize::MAX as usize,
"capacity is too large"
);
2020-07-05 00:25:57 -04:00
let data = if unlikely!(capacity == 0) {
let new_layout = Layout::from_size_align_unchecked(new_capacity, 1);
alloc(new_layout)
} else {
2020-07-09 21:20:10 -04:00
let old_layout = Layout::from_size_align_unchecked(capacity, 1);
2020-07-05 00:25:57 -04:00
realloc(ptr, old_layout, new_capacity)
};
2020-06-18 03:32:07 -04:00
2020-07-05 00:25:57 -04:00
if data.is_null() {
handle_alloc_error(Layout::from_size_align_unchecked(new_capacity, 1));
}
2020-07-05 00:25:57 -04:00
data
}
impl Clone for Buffer {
fn clone(&self) -> Self {
2020-06-12 01:59:47 -04:00
unsafe {
if self.is_empty() {
2020-06-12 01:59:47 -04:00
Self::new()
} else {
let buf = Self {
data: safe_alloc(self.len),
2020-06-12 01:59:47 -04:00
len: self.len,
capacity: self.len,
};
ptr::copy_nonoverlapping(self.data, buf.data, self.len);
buf
}
}
}
}
impl fmt::Debug for Buffer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl Drop for Buffer {
fn drop(&mut self) {
if self.capacity != 0 {
// SAFETY: when `self.capacity > 0`, `self.capacity` is the same value
// used for allocate the block of memory pointed by `self.data`.
unsafe {
let layout = Layout::from_size_align_unchecked(self.capacity, 1);
dealloc(self.data, layout);
}
}
2020-06-04 16:39:33 -04:00
}
}
impl fmt::Write for Buffer {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
Buffer::push_str(self, s);
2020-06-04 16:39:33 -04:00
Ok(())
}
}
impl From<String> for Buffer {
/// Shrink the data and pass raw pointer directory to buffer
///
/// This operation is `O(1)`
2020-06-04 16:39:33 -04:00
#[inline]
fn from(other: String) -> Buffer {
let bs = other.into_boxed_str();
let data = Box::leak(bs);
Buffer {
data: data.as_mut_ptr(),
len: data.len(),
capacity: data.len(),
}
2020-06-04 16:39:33 -04:00
}
}
impl From<&str> for Buffer {
#[inline]
fn from(other: &str) -> Buffer {
let mut buf = Buffer::with_capacity(other.len());
if !other.is_empty() {
// SAFETY: `Buffer.capacity()` should be same as `other.len()`, so if `other`
// is not empty, `buf.as_mut_ptr()` is supporsed to point to valid memory.
unsafe {
ptr::copy_nonoverlapping(other.as_ptr(), buf.as_mut_ptr(), other.len());
buf.advance(other.len());
}
}
buf
2020-06-04 16:39:33 -04:00
}
}
impl Add<&str> for Buffer {
type Output = Buffer;
#[inline]
fn add(mut self, other: &str) -> Buffer {
self.push_str(other);
2020-06-04 16:39:33 -04:00
self
}
}
impl AddAssign<&str> for Buffer {
#[inline]
fn add_assign(&mut self, other: &str) {
self.push_str(other)
2020-06-04 16:39:33 -04:00
}
}
impl Default for Buffer {
#[inline]
fn default() -> Buffer {
Buffer::new()
}
}
2020-12-19 04:21:48 -05:00
unsafe impl Send for Buffer {}
unsafe impl Sync for Buffer {}
#[cfg(test)]
mod tests {
2020-12-28 11:32:56 -05:00
use super::*;
use std::sync::{Arc, Barrier, Mutex};
use std::thread;
#[test]
2020-12-17 02:30:47 -05:00
fn push_str() {
let mut buffer = Buffer::new();
2020-06-11 06:02:28 -04:00
assert_eq!(buffer.len(), 0);
assert_eq!(buffer.capacity(), 0);
buffer.push_str("apple");
2020-06-11 06:02:28 -04:00
assert_eq!(buffer.len(), 5);
assert_eq!(buffer.capacity(), 5);
buffer.push_str("pie");
2020-06-11 06:02:28 -04:00
assert_eq!(buffer.len(), 8);
assert_eq!(buffer.capacity(), 10);
2020-12-28 11:32:56 -05:00
for _ in 0..16 {
buffer.push_str("zomg");
}
assert_eq!(buffer.len(), 72);
assert_eq!(buffer.capacity(), 80);
2020-06-11 06:02:28 -04:00
}
#[test]
2020-12-17 02:30:47 -05:00
fn with_capacity() {
let buffer = Buffer::with_capacity(1);
2020-06-11 06:02:28 -04:00
assert!(buffer.is_empty());
assert_eq!(buffer.len(), 0);
assert!(buffer.capacity() >= 1);
}
2020-06-06 23:57:15 -04:00
#[test]
fn string_conversion() {
// from empty string
2020-12-28 11:32:56 -05:00
let s = String::with_capacity(2);
assert!(s.capacity() >= 2);
2020-06-06 23:57:15 -04:00
let mut buf = Buffer::from(s);
assert_eq!(buf.as_str(), "");
2020-12-28 11:32:56 -05:00
// capacity should be shrinked for safety
assert_eq!(buf.capacity(), 0);
buf.push_str("abc");
2020-06-06 23:57:15 -04:00
assert_eq!(buf.as_str(), "abc");
// into non-empty string
let mut s = buf.into_string();
assert_eq!(s, "abc");
s += "defghijklmn";
2020-06-06 23:57:15 -04:00
assert_eq!(s, "abcdefghijklmn");
// from non-empty string
let mut buf = Buffer::from(s);
assert_eq!(buf.as_str(), "abcdefghijklmn");
buf.clear();
assert_eq!(buf.as_str(), "");
// into empty string
2020-07-14 05:27:19 -04:00
let buf = Buffer::default();
2020-06-06 23:57:15 -04:00
let mut s = buf.into_string();
assert_eq!(s, "");
s.push_str("apple");
assert_eq!(s, "apple");
}
2020-07-14 05:27:19 -04:00
2020-12-28 11:32:56 -05:00
#[test]
fn from_str() {
let buf = Buffer::from("abcdefgh");
assert_eq!(buf.as_str(), "abcdefgh");
}
2020-07-14 05:27:19 -04:00
#[test]
fn clone() {
use std::fmt::Write;
let mut s1 = Buffer::with_capacity(0);
let mut s2 = s1.clone();
s1.push('a');
s2.push_str("b");
assert_eq!(s1.as_str(), "a");
assert_eq!(s2.as_str(), "b");
let mut s1 = Buffer::from("foo");
let mut s2 = s1.clone();
s1 = s1 + "bar";
write!(s2, "baz").unwrap();
assert_eq!(s1.as_str(), "foobar");
assert_eq!(s2.as_str(), "foobaz");
s2.clear();
let _ = s2.clone();
2020-07-14 05:27:19 -04:00
}
2020-12-16 07:52:39 -05:00
#[test]
fn push() {
for initial_capacity in &[0, 4, 16] {
let mut s = Buffer::with_capacity(*initial_capacity);
s.push('a');
s.push('é');
s.push('');
s.push('🄫');
assert_eq!(s.as_str(), "🄫");
}
}
2020-12-28 11:32:56 -05:00
#[test]
fn multi_thread() {
const THREADS: usize = 8;
const ITERS: usize = 100;
let barrier = Arc::new(Barrier::new(THREADS));
let buffer = Arc::new(Mutex::new(Buffer::new()));
let mut handles = Vec::with_capacity(THREADS);
for _ in 0..THREADS {
let barrier = barrier.clone();
let buffer = buffer.clone();
handles.push(thread::spawn(move || {
barrier.wait();
for _ in 0..ITERS {
buffer.lock().unwrap().push_str("a");
}
}));
}
for handle in handles {
handle.join().unwrap();
}
assert_eq!(buffer.lock().unwrap().as_str(), "a".repeat(ITERS * THREADS));
}
#[test]
#[should_panic]
fn reserve_overflow() {
let mut buf = Buffer::new();
buf.reserve(std::isize::MAX as usize + 1);
}
#[test]
#[should_panic]
fn empty_alloc() {
safe_alloc(0);
}
}