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

Side by Side 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, 8 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 | « no previous file | no next file » | 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 "include/dart_native_api.h"
8 #include "vm/dart_entry.h" 8 #include "vm/dart_entry.h"
9 #include "vm/debugger.h" 9 #include "vm/debugger.h"
10 #include "vm/json_stream.h" 10 #include "vm/json_stream.h"
11 #include "vm/message.h" 11 #include "vm/message.h"
12 #include "vm/metrics.h" 12 #include "vm/metrics.h"
13 #include "vm/object.h" 13 #include "vm/object.h"
14 #include "vm/safepoint.h" 14 #include "vm/safepoint.h"
15 #include "vm/service.h" 15 #include "vm/service.h"
16 #include "vm/service_event.h" 16 #include "vm/service_event.h"
17 #include "vm/thread_registry.h" 17 #include "vm/thread_registry.h"
18 #include "vm/timeline.h" 18 #include "vm/timeline.h"
19 #include "vm/unicode.h" 19 #include "vm/unicode.h"
20 20
21 21
22 namespace dart { 22 namespace dart {
23 23
24 #ifndef PRODUCT 24 #ifndef PRODUCT
25 25
26 class MaybeOnStackBuffer {
27 public:
28 explicit MaybeOnStackBuffer(intptr_t size) {
29 if (size > kOnStackBufferCapacity) {
30 p_ = reinterpret_cast<char*>(malloc(size));
31 } else {
32 p_ = &buffer_[0];
33 }
34 }
35 ~MaybeOnStackBuffer() {
36 if (p_ != &buffer_[0]) free(p_);
37 }
38
39 char* p() { return p_; }
40
41 private:
42 static const intptr_t kOnStackBufferCapacity = 4096;
43 char* p_;
44 char buffer_[kOnStackBufferCapacity];
45 };
46
47
26 void AppendJSONStreamConsumer(Dart_StreamConsumer_State state, 48 void AppendJSONStreamConsumer(Dart_StreamConsumer_State state,
27 const char* stream_name, 49 const char* stream_name,
28 const uint8_t* buffer, 50 const uint8_t* buffer,
29 intptr_t buffer_length, 51 intptr_t buffer_length,
30 void* user_data) { 52 void* user_data) {
31 if ((state == Dart_StreamConsumer_kStart) || 53 if ((state == Dart_StreamConsumer_kStart) ||
32 (state == Dart_StreamConsumer_kFinish)) { 54 (state == Dart_StreamConsumer_kFinish)) {
33 // Ignore. 55 // Ignore.
34 return; 56 return;
35 } 57 }
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 } 511 }
490 512
491 513
492 void JSONStream::PrintfValue(const char* format, ...) { 514 void JSONStream::PrintfValue(const char* format, ...) {
493 PrintCommaIfNeeded(); 515 PrintCommaIfNeeded();
494 516
495 va_list args; 517 va_list args;
496 va_start(args, format); 518 va_start(args, format);
497 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 519 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
498 va_end(args); 520 va_end(args);
499 char* p = reinterpret_cast<char*>(malloc(len + 1)); 521 MaybeOnStackBuffer mosb(len + 1);
522 char* p = mosb.p();
500 va_start(args, format); 523 va_start(args, format);
501 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 524 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
502 va_end(args); 525 va_end(args);
503 ASSERT(len == len2); 526 ASSERT(len == len2);
504 buffer_.AddChar('"'); 527 buffer_.AddChar('"');
505 AddEscapedUTF8String(p); 528 AddEscapedUTF8String(p, len);
506 buffer_.AddChar('"'); 529 buffer_.AddChar('"');
507 free(p);
508 } 530 }
509 531
510 532
511 void JSONStream::PrintValue(const Object& o, bool ref) { 533 void JSONStream::PrintValue(const Object& o, bool ref) {
512 PrintCommaIfNeeded(); 534 PrintCommaIfNeeded();
513 o.PrintJSON(this, ref); 535 o.PrintJSON(this, ref);
514 } 536 }
515 537
516 538
517 void JSONStream::PrintValue(Breakpoint* bpt) { 539 void JSONStream::PrintValue(Breakpoint* bpt) {
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 PrintValue(timeline_event_block); 732 PrintValue(timeline_event_block);
711 } 733 }
712 734
713 735
714 void JSONStream::PrintfProperty(const char* name, const char* format, ...) { 736 void JSONStream::PrintfProperty(const char* name, const char* format, ...) {
715 PrintPropertyName(name); 737 PrintPropertyName(name);
716 va_list args; 738 va_list args;
717 va_start(args, format); 739 va_start(args, format);
718 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 740 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
719 va_end(args); 741 va_end(args);
720 char* p = reinterpret_cast<char*>(malloc(len + 1)); 742 MaybeOnStackBuffer mosb(len + 1);
743 char* p = mosb.p();
721 va_start(args, format); 744 va_start(args, format);
722 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 745 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
723 va_end(args); 746 va_end(args);
724 ASSERT(len == len2); 747 ASSERT(len == len2);
725 buffer_.AddChar('"'); 748 buffer_.AddChar('"');
726 AddEscapedUTF8String(p); 749 AddEscapedUTF8String(p, len);
727 buffer_.AddChar('"'); 750 buffer_.AddChar('"');
728 free(p);
729 } 751 }
730 752
731 753
732 void JSONStream::Steal(char** buffer, intptr_t* buffer_length) { 754 void JSONStream::Steal(char** buffer, intptr_t* buffer_length) {
733 ASSERT(buffer != NULL); 755 ASSERT(buffer != NULL);
734 ASSERT(buffer_length != NULL); 756 ASSERT(buffer_length != NULL);
735 *buffer_length = buffer_.length(); 757 *buffer_length = buffer_.length();
736 *buffer = buffer_.Steal(); 758 *buffer = buffer_.Steal();
737 } 759 }
738 760
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
913 935
914 void JSONObject::AddFixedServiceId(const char* format, ...) const { 936 void JSONObject::AddFixedServiceId(const char* format, ...) const {
915 // Mark that this id is fixed. 937 // Mark that this id is fixed.
916 AddProperty("fixedId", true); 938 AddProperty("fixedId", true);
917 // Add the id property. 939 // Add the id property.
918 stream_->PrintPropertyName("id"); 940 stream_->PrintPropertyName("id");
919 va_list args; 941 va_list args;
920 va_start(args, format); 942 va_start(args, format);
921 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 943 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
922 va_end(args); 944 va_end(args);
923 char* p = reinterpret_cast<char*>(malloc(len + 1)); 945 MaybeOnStackBuffer mosb(len + 1);
946 char* p = mosb.p();
924 va_start(args, format); 947 va_start(args, format);
925 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 948 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
926 va_end(args); 949 va_end(args);
927 ASSERT(len == len2); 950 ASSERT(len == len2);
928 stream_->buffer_.AddChar('"'); 951 stream_->buffer_.AddChar('"');
929 stream_->AddEscapedUTF8String(p); 952 stream_->AddEscapedUTF8String(p, len);
930 stream_->buffer_.AddChar('"'); 953 stream_->buffer_.AddChar('"');
931 free(p);
932 } 954 }
933 955
934 956
935 void JSONObject::AddLocation(const Script& script, 957 void JSONObject::AddLocation(const Script& script,
936 TokenPosition token_pos, 958 TokenPosition token_pos,
937 TokenPosition end_token_pos) const { 959 TokenPosition end_token_pos) const {
938 JSONObject location(this, "location"); 960 JSONObject location(this, "location");
939 location.AddProperty("type", "SourceLocation"); 961 location.AddProperty("type", "SourceLocation");
940 location.AddProperty("script", script); 962 location.AddProperty("script", script);
941 location.AddProperty("tokenPos", token_pos); 963 location.AddProperty("tokenPos", token_pos);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
987 } 1009 }
988 } 1010 }
989 1011
990 1012
991 void JSONObject::AddPropertyF(const char* name, const char* format, ...) const { 1013 void JSONObject::AddPropertyF(const char* name, const char* format, ...) const {
992 stream_->PrintPropertyName(name); 1014 stream_->PrintPropertyName(name);
993 va_list args; 1015 va_list args;
994 va_start(args, format); 1016 va_start(args, format);
995 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 1017 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
996 va_end(args); 1018 va_end(args);
997 char* p = reinterpret_cast<char*>(malloc(len + 1)); 1019 MaybeOnStackBuffer mosb(len + 1);
1020 char* p = mosb.p();
998 va_start(args, format); 1021 va_start(args, format);
999 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 1022 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
1000 va_end(args); 1023 va_end(args);
1001 ASSERT(len == len2); 1024 ASSERT(len == len2);
1002 stream_->buffer_.AddChar('"'); 1025 stream_->buffer_.AddChar('"');
1003 stream_->AddEscapedUTF8String(p); 1026 stream_->AddEscapedUTF8String(p, len);
1004 stream_->buffer_.AddChar('"'); 1027 stream_->buffer_.AddChar('"');
1005 free(p);
1006 } 1028 }
1007 1029
1008 1030
1009 void JSONArray::AddValueF(const char* format, ...) const { 1031 void JSONArray::AddValueF(const char* format, ...) const {
1010 stream_->PrintCommaIfNeeded(); 1032 stream_->PrintCommaIfNeeded();
1011 va_list args; 1033 va_list args;
1012 va_start(args, format); 1034 va_start(args, format);
1013 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 1035 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
1014 va_end(args); 1036 va_end(args);
1015 char* p = reinterpret_cast<char*>(malloc(len + 1)); 1037 MaybeOnStackBuffer mosb(len + 1);
1038 char* p = mosb.p();
1016 va_start(args, format); 1039 va_start(args, format);
1017 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 1040 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
1018 va_end(args); 1041 va_end(args);
1019 ASSERT(len == len2); 1042 ASSERT(len == len2);
1020 stream_->buffer_.AddChar('"'); 1043 stream_->buffer_.AddChar('"');
1021 stream_->AddEscapedUTF8String(p); 1044 stream_->AddEscapedUTF8String(p, len);
1022 stream_->buffer_.AddChar('"'); 1045 stream_->buffer_.AddChar('"');
1023 free(p);
1024 } 1046 }
1025 1047
1026 #endif // !PRODUCT 1048 #endif // !PRODUCT
1027 1049
1028 } // namespace dart 1050 } // namespace dart
OLDNEW
« 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