llvm.org GIT mirror llvm / 425d08c
Switch raw_svector_ostream to use the vector as the ostream buffer. - This avoids unnecessary malloc/free overhead in the common case, and unnecessary copying from the ostream buffer into the output vector. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79434 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 10 years ago
2 changed file(s) with 33 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
451451 /// counting the bytes currently in the buffer.
452452 virtual uint64_t current_pos();
453453 public:
454 /// Construct a new raw_svector_ostream.
455 ///
456 /// \arg O - The vector to write to; this *must* have at least 128 bytes of
457 /// free space in it.
454458 explicit raw_svector_ostream(SmallVectorImpl &O);
455459 ~raw_svector_ostream();
456460 };
9494 OutBufEnd = OutBufStart+Size;
9595 OutBufCur = OutBufStart;
9696 BufferMode = Mode;
97
98 assert(OutBufStart <= OutBufEnd && "Invalid size!");
9799 }
98100
99101 raw_ostream &raw_ostream::operator<<(unsigned long N) {
477479 // raw_svector_ostream
478480 //===----------------------------------------------------------------------===//
479481
482 // The raw_svector_ostream implementation uses the SmallVector itself as the
483 // buffer for the raw_ostream. We guarantee that the raw_ostream buffer is
484 // always pointing past the end of the vector, but within the vector
485 // capacity. This allows raw_ostream to write directly into the correct place,
486 // and we only need to set the vector size when the data is flushed.
487
480488 raw_svector_ostream::raw_svector_ostream(SmallVectorImpl &O) : OS(O) {
489 // Set up the initial external buffer. We enforce that the buffer must have at
490 // least 128 bytes free; raw_ostream itself only requires 64, but we want to
491 // make sure that we don't grow the buffer unnecessarily on destruction (when
492 // the data is flushed). See the FIXME below.
493 if (OS.capacity() - OS.size() < 128)
494 llvm_report_error("Invalid argument, must have at least 128 bytes free!");
495 SetBuffer(OS.end(), OS.capacity() - OS.size());
481496 }
482497
483498 raw_svector_ostream::~raw_svector_ostream() {
499 // FIXME: Prevent resizing during this flush().
484500 flush();
485501 }
486502
487503 void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
488 OS.append(Ptr, Ptr + Size);
504 assert(Ptr == OS.end() && OS.size() + Size <= OS.capacity() &&
505 "Invalid write_impl() call!");
506
507 // We don't need to copy the bytes, just commit the bytes to the
508 // SmallVector.
509 OS.set_size(OS.size() + Size);
510
511 // Grow the vector if necessary.
512 if (OS.capacity() - OS.size() < 64)
513 OS.reserve(OS.capacity() * 2);
514
515 // Update the buffer position.
516 SetBuffer(OS.end(), OS.capacity() - OS.size());
489517 }
490518
491519 uint64_t raw_svector_ostream::current_pos() { return OS.size(); }