[vm] Avoid quadratic growth in (Malloc)TextBuffer and ZoneTextBuffer.

In particular, avoid quadratic memory use in ZoneTextBuffer.

Change-Id: I8d2ea77d35cd3c2c353ae557a2ee676ee8413653
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/95483
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
Ryan Macnak 2019-03-05 22:32:22 +00:00 committed by commit-bot@chromium.org
parent c921716a30
commit 60de5b4983
3 changed files with 3 additions and 13 deletions

View file

@ -135,12 +135,7 @@ void TextBuffer::AddEscapedString(const char* s) {
void TextBuffer::EnsureCapacity(intptr_t len) {
intptr_t remaining = buf_size_ - msg_len_;
if (remaining <= len) {
const int kBufferSpareCapacity = 64; // Somewhat arbitrary.
// TODO(turnidge): do we need to guard against overflow or other
// security issues here? Text buffers are used by the debugger
// to send user-controlled data (e.g. values of string variables) to
// the debugger front-end.
intptr_t new_size = buf_size_ + len + kBufferSpareCapacity;
intptr_t new_size = buf_size_ + Utils::Maximum(buf_size_, len);
char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size));
if (new_buf == NULL) {
OUT_OF_MEMORY();

View file

@ -20,7 +20,7 @@ namespace dart {
DEFINE_FLAG(bool, trace_compilation_trace, false, "Trace compilation trace.");
CompilationTraceSaver::CompilationTraceSaver(Zone* zone)
: buf_(zone, 4 * KB),
: buf_(zone, 1 * MB),
func_name_(String::Handle(zone)),
cls_(Class::Handle(zone)),
cls_name_(String::Handle(zone)),

View file

@ -50,12 +50,7 @@ void ZoneTextBuffer::AddString(const char* s) {
void ZoneTextBuffer::EnsureCapacity(intptr_t len) {
intptr_t remaining = capacity_ - length_;
if (remaining <= len) {
const int kBufferSpareCapacity = 64; // Somewhat arbitrary.
// TODO(turnidge): do we need to guard against overflow or other
// security issues here? Text buffers are used by the debugger
// to send user-controlled data (e.g. values of string variables) to
// the debugger front-end.
intptr_t new_capacity = capacity_ + len + kBufferSpareCapacity;
intptr_t new_capacity = capacity_ + Utils::Maximum(capacity_, len);
buffer_ = zone_->Realloc<char>(buffer_, capacity_, new_capacity);
capacity_ = new_capacity;
}