Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Unified Diff: runtime/vm/json_stream.cc

Issue 2791633002: Avoid many temporary allocations when serializing service protocol responses (Closed)
Patch Set: zra review Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/json_stream.cc
diff --git a/runtime/vm/json_stream.cc b/runtime/vm/json_stream.cc
index d95345b4953c184401adf3e477710531c5b33f80..459d077dc622751639f6966a6d4eb389c2a752d8 100644
--- a/runtime/vm/json_stream.cc
+++ b/runtime/vm/json_stream.cc
@@ -23,6 +23,28 @@ namespace dart {
#ifndef PRODUCT
+class MaybeOnStackBuffer {
+ public:
+ explicit MaybeOnStackBuffer(intptr_t size) {
+ if (size > kOnStackBufferCapacity) {
+ p_ = reinterpret_cast<char*>(malloc(size));
+ } else {
+ p_ = &buffer_[0];
+ }
+ }
+ ~MaybeOnStackBuffer() {
+ if (p_ != &buffer_[0]) free(p_);
+ }
+
+ char* p() { return p_; }
+
+ private:
+ static const intptr_t kOnStackBufferCapacity = 4096;
+ char* p_;
+ char buffer_[kOnStackBufferCapacity];
+};
+
+
void AppendJSONStreamConsumer(Dart_StreamConsumer_State state,
const char* stream_name,
const uint8_t* buffer,
@@ -496,15 +518,15 @@ void JSONStream::PrintfValue(const char* format, ...) {
va_start(args, format);
intptr_t len = OS::VSNPrint(NULL, 0, format, args);
va_end(args);
- char* p = reinterpret_cast<char*>(malloc(len + 1));
+ MaybeOnStackBuffer mosb(len + 1);
+ char* p = mosb.p();
va_start(args, format);
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
va_end(args);
ASSERT(len == len2);
buffer_.AddChar('"');
- AddEscapedUTF8String(p);
+ AddEscapedUTF8String(p, len);
buffer_.AddChar('"');
- free(p);
}
@@ -717,15 +739,15 @@ void JSONStream::PrintfProperty(const char* name, const char* format, ...) {
va_start(args, format);
intptr_t len = OS::VSNPrint(NULL, 0, format, args);
va_end(args);
- char* p = reinterpret_cast<char*>(malloc(len + 1));
+ MaybeOnStackBuffer mosb(len + 1);
+ char* p = mosb.p();
va_start(args, format);
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
va_end(args);
ASSERT(len == len2);
buffer_.AddChar('"');
- AddEscapedUTF8String(p);
+ AddEscapedUTF8String(p, len);
buffer_.AddChar('"');
- free(p);
}
@@ -920,15 +942,15 @@ void JSONObject::AddFixedServiceId(const char* format, ...) const {
va_start(args, format);
intptr_t len = OS::VSNPrint(NULL, 0, format, args);
va_end(args);
- char* p = reinterpret_cast<char*>(malloc(len + 1));
+ MaybeOnStackBuffer mosb(len + 1);
+ char* p = mosb.p();
va_start(args, format);
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
va_end(args);
ASSERT(len == len2);
stream_->buffer_.AddChar('"');
- stream_->AddEscapedUTF8String(p);
+ stream_->AddEscapedUTF8String(p, len);
stream_->buffer_.AddChar('"');
- free(p);
}
@@ -994,15 +1016,15 @@ void JSONObject::AddPropertyF(const char* name, const char* format, ...) const {
va_start(args, format);
intptr_t len = OS::VSNPrint(NULL, 0, format, args);
va_end(args);
- char* p = reinterpret_cast<char*>(malloc(len + 1));
+ MaybeOnStackBuffer mosb(len + 1);
+ char* p = mosb.p();
va_start(args, format);
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
va_end(args);
ASSERT(len == len2);
stream_->buffer_.AddChar('"');
- stream_->AddEscapedUTF8String(p);
+ stream_->AddEscapedUTF8String(p, len);
stream_->buffer_.AddChar('"');
- free(p);
}
@@ -1012,15 +1034,15 @@ void JSONArray::AddValueF(const char* format, ...) const {
va_start(args, format);
intptr_t len = OS::VSNPrint(NULL, 0, format, args);
va_end(args);
- char* p = reinterpret_cast<char*>(malloc(len + 1));
+ MaybeOnStackBuffer mosb(len + 1);
+ char* p = mosb.p();
va_start(args, format);
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
va_end(args);
ASSERT(len == len2);
stream_->buffer_.AddChar('"');
- stream_->AddEscapedUTF8String(p);
+ stream_->AddEscapedUTF8String(p, len);
stream_->buffer_.AddChar('"');
- free(p);
}
#endif // !PRODUCT
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698