perf: improve buffer performance
* Use reserve_small in push_str method
* revert 47818c1
to avoid unnecessary reloading of length
This commit is contained in:
parent
77b37aac63
commit
a88aacbc9e
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue