From a88aacbc9e52c155ce4b44fc8371d3f90aaf5ad8 Mon Sep 17 00:00:00 2001 From: Kogia-sima Date: Mon, 29 Mar 2021 08:19:13 +0900 Subject: [PATCH] perf: improve buffer performance * Use reserve_small in push_str method * revert 47818c1 to avoid unnecessary reloading of length --- sailfish/src/runtime/buffer.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sailfish/src/runtime/buffer.rs b/sailfish/src/runtime/buffer.rs index 8e46942..8bae951 100644 --- a/sailfish/src/runtime/buffer.rs +++ b/sailfish/src/runtime/buffer.rs @@ -136,12 +136,12 @@ impl Buffer { pub fn push_str(&mut self, data: &str) { let size = data.len(); - // NOTE: Since there's no guarantee that the maximum slice size won't overflow - // isize::MAX, we must call `reserve()` instead of `reserve_small()`. See - // https://github.com/rust-lang/rust/pull/79930#issuecomment-747135498 for more - // details. - self.reserve(size); 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); std::ptr::copy_nonoverlapping(data.as_ptr(), p, size); self.len += size; @@ -165,7 +165,7 @@ impl Buffer { 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.len + size); + 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;