Add Buffer::render_internal method to speedup reserve method

This commit is contained in:
Kogia-sima 2020-06-16 20:00:35 +09:00
parent 441bceb412
commit 9e2952d92d
2 changed files with 13 additions and 10 deletions

View File

@ -84,14 +84,10 @@ impl Buffer {
self.len == 0
}
#[inline(never)]
#[inline]
pub fn reserve(&mut self, size: usize) {
if size > self.capacity - self.len {
unsafe {
let new_capacity = std::cmp::max(self.capacity * 2, self.len + size);
self.realloc(new_capacity);
self.capacity = new_capacity;
}
self.reserve_internal(size);
}
}
@ -110,7 +106,7 @@ impl Buffer {
pub fn push_str(&mut self, data: &str) {
let size = data.len();
if size > self.capacity - self.len {
self.reserve(size);
self.reserve_internal(size);
}
unsafe {
let p = self.data.add(self.len);
@ -125,6 +121,15 @@ impl Buffer {
self.push_str(data.encode_utf8(&mut buf));
}
#[inline(never)]
fn reserve_internal(&mut self, size: usize) {
unsafe {
let new_capacity = std::cmp::max(self.capacity * 2, self.len + size);
self.realloc(new_capacity);
self.capacity = new_capacity;
}
}
unsafe fn realloc(&mut self, cap: usize) {
if self.capacity == 0 {
let new_layout = Layout::from_size_align_unchecked(cap, 1);

View File

@ -134,9 +134,7 @@ macro_rules! render_int {
fn render(&self, b: &mut Buffer) -> Result<(), RenderError> {
use super::integer::Integer;
if Self::MAX_LEN > b.capacity() - b.len() {
b.reserve(Self::MAX_LEN);
}
b.reserve(Self::MAX_LEN);
unsafe {
let ptr = b.as_mut_ptr().add(b.len());