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

Side by Side Diff: runtime/vm/json_stream.cc

Issue 2254543006: Reduce copying in sending service responses. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: dbc, sync Created 4 years, 4 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 unified diff | Download patch
« no previous file with comments | « runtime/vm/json_stream.h ('k') | runtime/vm/service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/assert.h" 5 #include "platform/assert.h"
6 6
7 #include "include/dart_native_api.h"
7 #include "vm/dart_entry.h" 8 #include "vm/dart_entry.h"
8 #include "vm/debugger.h" 9 #include "vm/debugger.h"
9 #include "vm/json_stream.h" 10 #include "vm/json_stream.h"
10 #include "vm/message.h" 11 #include "vm/message.h"
11 #include "vm/metrics.h" 12 #include "vm/metrics.h"
12 #include "vm/object.h" 13 #include "vm/object.h"
14 #include "vm/safepoint.h"
13 #include "vm/service.h" 15 #include "vm/service.h"
14 #include "vm/service_event.h" 16 #include "vm/service_event.h"
15 #include "vm/timeline.h" 17 #include "vm/timeline.h"
16 #include "vm/unicode.h" 18 #include "vm/unicode.h"
17 19
18 20
19 namespace dart { 21 namespace dart {
20 22
21 #ifndef PRODUCT 23 #ifndef PRODUCT
22 24
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 va_start(args2, details_format); 188 va_start(args2, details_format);
187 OS::VSNPrint(buffer, (len + 1), details_format, args2); 189 OS::VSNPrint(buffer, (len + 1), details_format, args2);
188 va_end(args2); 190 va_end(args2);
189 191
190 data.AddProperty("details", buffer); 192 data.AddProperty("details", buffer);
191 } 193 }
192 } 194 }
193 } 195 }
194 196
195 197
196 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
197 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size);
198 return reinterpret_cast<uint8_t*>(new_ptr);
199 }
200
201
202 void JSONStream::PostNullReply(Dart_Port port) { 198 void JSONStream::PostNullReply(Dart_Port port) {
203 PortMap::PostMessage(new Message( 199 PortMap::PostMessage(new Message(
204 port, Object::null(), Message::kNormalPriority)); 200 port, Object::null(), Message::kNormalPriority));
205 } 201 }
206 202
207 203
204 static void Finalizer(void* isolate_callback_data,
205 Dart_WeakPersistentHandle handle,
206 void* buffer) {
207 free(buffer);
208 }
209
210
208 void JSONStream::PostReply() { 211 void JSONStream::PostReply() {
209 Dart_Port port = reply_port(); 212 Dart_Port port = reply_port();
210 ASSERT(port != ILLEGAL_PORT); 213 ASSERT(port != ILLEGAL_PORT);
211 set_reply_port(ILLEGAL_PORT); // Prevent double replies. 214 set_reply_port(ILLEGAL_PORT); // Prevent double replies.
212 ASSERT(seq_ != NULL); 215 ASSERT(seq_ != NULL);
213 if (seq_->IsString()) { 216 if (seq_->IsString()) {
214 const String& str = String::Cast(*seq_); 217 const String& str = String::Cast(*seq_);
215 PrintProperty("id", str.ToCString()); 218 PrintProperty("id", str.ToCString());
216 } else if (seq_->IsInteger()) { 219 } else if (seq_->IsInteger()) {
217 const Integer& integer = Integer::Cast(*seq_); 220 const Integer& integer = Integer::Cast(*seq_);
218 PrintProperty64("id", integer.AsInt64Value()); 221 PrintProperty64("id", integer.AsInt64Value());
219 } else if (seq_->IsDouble()) { 222 } else if (seq_->IsDouble()) {
220 const Double& dbl = Double::Cast(*seq_); 223 const Double& dbl = Double::Cast(*seq_);
221 PrintProperty("id", dbl.value()); 224 PrintProperty("id", dbl.value());
222 } else if (seq_->IsNull()) { 225 } else if (seq_->IsNull()) {
223 // JSON-RPC 2.0 says that a request with a null ID shouldn't get a reply. 226 // JSON-RPC 2.0 says that a request with a null ID shouldn't get a reply.
224 PostNullReply(port); 227 PostNullReply(port);
225 return; 228 return;
226 } 229 }
227 buffer_.AddChar('}'); 230 buffer_.AddChar('}');
228 231
229 const String& reply = String::Handle(String::New(ToCString())); 232 char* cstr;
230 ASSERT(!reply.IsNull()); 233 intptr_t length;
234 Steal(&cstr, &length);
231 235
232 uint8_t* data = NULL; 236 bool result;
233 MessageWriter writer(&data, &allocator, false); 237 {
234 writer.WriteMessage(reply); 238 TransitionVMToNative transition(Thread::Current());
235 bool result = PortMap::PostMessage(new Message(port, data, 239 Dart_CObject bytes;
236 writer.BytesWritten(), 240 bytes.type = Dart_CObject_kExternalTypedData;
237 Message::kNormalPriority)); 241 bytes.value.as_external_typed_data.type = Dart_TypedData_kUint8;
242 bytes.value.as_external_typed_data.length = length;
243 bytes.value.as_external_typed_data.data = reinterpret_cast<uint8_t*>(cstr);
244 bytes.value.as_external_typed_data.peer = cstr;
245 bytes.value.as_external_typed_data.callback = Finalizer;
246 Dart_CObject* elements[1];
247 elements[0] = &bytes;
248 Dart_CObject message;
249 message.type = Dart_CObject_kArray;
250 message.value.as_array.length = 1;
251 message.value.as_array.values = elements;
252 result = Dart_PostCObject(port, &message);
253 }
254
255 if (!result) {
256 free(cstr);
257 }
258
238 if (FLAG_trace_service) { 259 if (FLAG_trace_service) {
239 Isolate* isolate = Isolate::Current(); 260 Isolate* isolate = Isolate::Current();
240 ASSERT(isolate != NULL); 261 ASSERT(isolate != NULL);
241 const char* isolate_name = isolate->name(); 262 const char* isolate_name = isolate->name();
242 int64_t total_time = OS::GetCurrentTimeMicros() - setup_time_micros_; 263 int64_t total_time = OS::GetCurrentTimeMicros() - setup_time_micros_;
243 if (result) { 264 if (result) {
244 OS::Print("[+%" Pd64 "ms] Isolate %s processed service request %s " 265 OS::Print("[+%" Pd64 "ms] Isolate %s processed service request %s "
245 "(%" Pd64 "us)\n", 266 "(%" Pd64 "us)\n",
246 Dart::timestamp(), isolate_name, method_, total_time); 267 Dart::timestamp(), isolate_name, method_, total_time);
247 } else { 268 } else {
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); 686 intptr_t len2 = OS::VSNPrint(p, len+1, format, args);
666 va_end(args); 687 va_end(args);
667 ASSERT(len == len2); 688 ASSERT(len == len2);
668 buffer_.AddChar('"'); 689 buffer_.AddChar('"');
669 AddEscapedUTF8String(p); 690 AddEscapedUTF8String(p);
670 buffer_.AddChar('"'); 691 buffer_.AddChar('"');
671 free(p); 692 free(p);
672 } 693 }
673 694
674 695
675 void JSONStream::Steal(const char** buffer, intptr_t* buffer_length) { 696 void JSONStream::Steal(char** buffer, intptr_t* buffer_length) {
676 ASSERT(buffer != NULL); 697 ASSERT(buffer != NULL);
677 ASSERT(buffer_length != NULL); 698 ASSERT(buffer_length != NULL);
678 *buffer_length = buffer_.length(); 699 *buffer_length = buffer_.length();
679 *buffer = buffer_.Steal(); 700 *buffer = buffer_.Steal();
680 } 701 }
681 702
682 703
683 void JSONStream::set_reply_port(Dart_Port port) { 704 void JSONStream::set_reply_port(Dart_Port port) {
684 reply_port_ = port; 705 reply_port_ = port;
685 } 706 }
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 ASSERT(len == len2); 983 ASSERT(len == len2);
963 stream_->buffer_.AddChar('"'); 984 stream_->buffer_.AddChar('"');
964 stream_->AddEscapedUTF8String(p); 985 stream_->AddEscapedUTF8String(p);
965 stream_->buffer_.AddChar('"'); 986 stream_->buffer_.AddChar('"');
966 free(p); 987 free(p);
967 } 988 }
968 989
969 #endif // !PRODUCT 990 #endif // !PRODUCT
970 991
971 } // namespace dart 992 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/json_stream.h ('k') | runtime/vm/service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698