mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-22 13:29:44 +00:00
raw_svector_ostream: grow and reserve atomically
Including the scratch buffer size in the initial reservation eliminates the subsequent malloc+move operation and offers a healthier constant growth with less memory wastage. When doing this, take care to avoid invalidating the source buffer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212816 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
cb047f2a74
commit
81bc6fb854
@ -729,24 +729,26 @@ void raw_svector_ostream::resync() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
|
void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
|
||||||
// If we're writing bytes from the end of the buffer into the smallvector, we
|
size_t NewSize = OS.size() + Size;
|
||||||
// don't need to copy the bytes, just commit the bytes because they are
|
size_t NewReservation = NewSize + 64;
|
||||||
// already in the right place.
|
|
||||||
if (Ptr == OS.end()) {
|
bool NoOverlap = Ptr + Size < OS.begin() || Ptr > OS.begin() + OS.capacity();
|
||||||
assert(OS.size() + Size <= OS.capacity() && "Invalid write_impl() call!");
|
|
||||||
OS.set_size(OS.size() + Size);
|
if (NoOverlap) {
|
||||||
|
assert(!GetNumBytesInBuffer());
|
||||||
|
OS.reserve(NewReservation);
|
||||||
|
memcpy(OS.end(), Ptr, Size);
|
||||||
|
OS.set_size(NewSize);
|
||||||
|
} else if (Ptr == OS.end()) {
|
||||||
|
// Grow the buffer to include the scratch area without copying.
|
||||||
|
assert(NewSize <= OS.capacity() && "Invalid write_impl() call!");
|
||||||
|
OS.set_size(NewSize);
|
||||||
|
OS.reserve(NewReservation);
|
||||||
} else {
|
} else {
|
||||||
assert(GetNumBytesInBuffer() == 0 &&
|
OS.append(Ptr, Ptr + Size);
|
||||||
"Should be writing from buffer if some bytes in it");
|
OS.reserve(NewReservation);
|
||||||
// Otherwise, do copy the bytes.
|
|
||||||
OS.append(Ptr, Ptr+Size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grow the vector if necessary.
|
|
||||||
if (OS.capacity() - OS.size() < 64)
|
|
||||||
OS.reserve(OS.capacity() * 2);
|
|
||||||
|
|
||||||
// Update the buffer position.
|
|
||||||
SetBuffer(OS.end(), OS.capacity() - OS.size());
|
SetBuffer(OS.end(), OS.capacity() - OS.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user