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

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

Issue 2791633002: Avoid many temporary allocations when serializing service protocol responses (Closed)
Patch Set: 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"
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 } 483 }
484 484
485 485
486 void JSONStream::PrintValueNoEscape(const char* s) { 486 void JSONStream::PrintValueNoEscape(const char* s) {
487 PrintCommaIfNeeded(); 487 PrintCommaIfNeeded();
488 buffer_.Printf("%s", s); 488 buffer_.Printf("%s", s);
489 } 489 }
490 490
491 491
492 void JSONStream::PrintfValue(const char* format, ...) { 492 void JSONStream::PrintfValue(const char* format, ...) {
493 const intptr_t kOnStackBufferCapacity = 4096;
zra 2017/03/31 17:47:09 Maybe something like: class MaybeOnStackBuffer {
Cutch 2017/03/31 18:04:47 Done.
494 char on_stack_buffer[kOnStackBufferCapacity];
493 PrintCommaIfNeeded(); 495 PrintCommaIfNeeded();
494 496
495 va_list args; 497 va_list args;
496 va_start(args, format); 498 va_start(args, format);
497 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 499 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
498 va_end(args); 500 va_end(args);
499 char* p = reinterpret_cast<char*>(malloc(len + 1)); 501 char* p = &on_stack_buffer[0];
502 if (len + 1 >= kOnStackBufferCapacity) {
siva 2017/03/31 17:59:33 ((len + 1) >= kOnStackBufferCapacity) here and bel
Cutch 2017/03/31 18:04:47 Acknowledged.
503 p = reinterpret_cast<char*>(malloc(len + 1));
504 }
500 va_start(args, format); 505 va_start(args, format);
501 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 506 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
502 va_end(args); 507 va_end(args);
503 ASSERT(len == len2); 508 ASSERT(len == len2);
504 buffer_.AddChar('"'); 509 buffer_.AddChar('"');
505 AddEscapedUTF8String(p); 510 AddEscapedUTF8String(p, len);
506 buffer_.AddChar('"'); 511 buffer_.AddChar('"');
507 free(p); 512 if (p != &on_stack_buffer[0]) {
513 free(p);
514 }
508 } 515 }
509 516
510 517
511 void JSONStream::PrintValue(const Object& o, bool ref) { 518 void JSONStream::PrintValue(const Object& o, bool ref) {
512 PrintCommaIfNeeded(); 519 PrintCommaIfNeeded();
513 o.PrintJSON(this, ref); 520 o.PrintJSON(this, ref);
514 } 521 }
515 522
516 523
517 void JSONStream::PrintValue(Breakpoint* bpt) { 524 void JSONStream::PrintValue(Breakpoint* bpt) {
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 712
706 713
707 void JSONStream::PrintProperty(const char* name, 714 void JSONStream::PrintProperty(const char* name,
708 const TimelineEventBlock* timeline_event_block) { 715 const TimelineEventBlock* timeline_event_block) {
709 PrintPropertyName(name); 716 PrintPropertyName(name);
710 PrintValue(timeline_event_block); 717 PrintValue(timeline_event_block);
711 } 718 }
712 719
713 720
714 void JSONStream::PrintfProperty(const char* name, const char* format, ...) { 721 void JSONStream::PrintfProperty(const char* name, const char* format, ...) {
722 const intptr_t kOnStackBufferCapacity = 4096;
723 char on_stack_buffer[kOnStackBufferCapacity];
715 PrintPropertyName(name); 724 PrintPropertyName(name);
716 va_list args; 725 va_list args;
717 va_start(args, format); 726 va_start(args, format);
718 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 727 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
719 va_end(args); 728 va_end(args);
720 char* p = reinterpret_cast<char*>(malloc(len + 1)); 729 char* p = &on_stack_buffer[0];
730 if (len + 1 >= kOnStackBufferCapacity) {
731 p = reinterpret_cast<char*>(malloc(len + 1));
732 }
733
721 va_start(args, format); 734 va_start(args, format);
722 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 735 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
723 va_end(args); 736 va_end(args);
724 ASSERT(len == len2); 737 ASSERT(len == len2);
725 buffer_.AddChar('"'); 738 buffer_.AddChar('"');
726 AddEscapedUTF8String(p); 739 AddEscapedUTF8String(p, len);
727 buffer_.AddChar('"'); 740 buffer_.AddChar('"');
728 free(p); 741 if (p != &on_stack_buffer[0]) {
742 free(p);
743 }
729 } 744 }
730 745
731 746
732 void JSONStream::Steal(char** buffer, intptr_t* buffer_length) { 747 void JSONStream::Steal(char** buffer, intptr_t* buffer_length) {
733 ASSERT(buffer != NULL); 748 ASSERT(buffer != NULL);
734 ASSERT(buffer_length != NULL); 749 ASSERT(buffer_length != NULL);
735 *buffer_length = buffer_.length(); 750 *buffer_length = buffer_.length();
736 *buffer = buffer_.Steal(); 751 *buffer = buffer_.Steal();
737 } 752 }
738 753
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
905 return (offset > 0) || (limit < length); 920 return (offset > 0) || (limit < length);
906 } 921 }
907 922
908 923
909 JSONObject::JSONObject(const JSONArray* arr) : stream_(arr->stream_) { 924 JSONObject::JSONObject(const JSONArray* arr) : stream_(arr->stream_) {
910 stream_->OpenObject(); 925 stream_->OpenObject();
911 } 926 }
912 927
913 928
914 void JSONObject::AddFixedServiceId(const char* format, ...) const { 929 void JSONObject::AddFixedServiceId(const char* format, ...) const {
930 const intptr_t kOnStackBufferCapacity = 4096;
931 char on_stack_buffer[kOnStackBufferCapacity];
915 // Mark that this id is fixed. 932 // Mark that this id is fixed.
916 AddProperty("fixedId", true); 933 AddProperty("fixedId", true);
917 // Add the id property. 934 // Add the id property.
918 stream_->PrintPropertyName("id"); 935 stream_->PrintPropertyName("id");
919 va_list args; 936 va_list args;
920 va_start(args, format); 937 va_start(args, format);
921 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 938 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
922 va_end(args); 939 va_end(args);
923 char* p = reinterpret_cast<char*>(malloc(len + 1)); 940 char* p = &on_stack_buffer[0];
941 if (len + 1 >= kOnStackBufferCapacity) {
942 p = reinterpret_cast<char*>(malloc(len + 1));
943 }
924 va_start(args, format); 944 va_start(args, format);
925 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 945 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
926 va_end(args); 946 va_end(args);
927 ASSERT(len == len2); 947 ASSERT(len == len2);
928 stream_->buffer_.AddChar('"'); 948 stream_->buffer_.AddChar('"');
929 stream_->AddEscapedUTF8String(p); 949 stream_->AddEscapedUTF8String(p, len);
930 stream_->buffer_.AddChar('"'); 950 stream_->buffer_.AddChar('"');
931 free(p); 951 if (p != &on_stack_buffer[0]) {
952 free(p);
953 }
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 location.AddProperty("column", bpt_loc->requested_column_number()); 1004 location.AddProperty("column", bpt_loc->requested_column_number());
983 } 1005 }
984 } else { 1006 } else {
985 // This unresolved breakpoint was requested at some function entry. 1007 // This unresolved breakpoint was requested at some function entry.
986 location.AddProperty("tokenPos", token_pos); 1008 location.AddProperty("tokenPos", token_pos);
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 {
1014 const intptr_t kOnStackBufferCapacity = 4096;
1015 char on_stack_buffer[kOnStackBufferCapacity];
992 stream_->PrintPropertyName(name); 1016 stream_->PrintPropertyName(name);
993 va_list args; 1017 va_list args;
994 va_start(args, format); 1018 va_start(args, format);
995 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 1019 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
996 va_end(args); 1020 va_end(args);
997 char* p = reinterpret_cast<char*>(malloc(len + 1)); 1021 char* p = &on_stack_buffer[0];
1022 if (len + 1 >= kOnStackBufferCapacity) {
1023 p = reinterpret_cast<char*>(malloc(len + 1));
1024 }
998 va_start(args, format); 1025 va_start(args, format);
999 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 1026 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
1000 va_end(args); 1027 va_end(args);
1001 ASSERT(len == len2); 1028 ASSERT(len == len2);
1002 stream_->buffer_.AddChar('"'); 1029 stream_->buffer_.AddChar('"');
1003 stream_->AddEscapedUTF8String(p); 1030 stream_->AddEscapedUTF8String(p, len);
1004 stream_->buffer_.AddChar('"'); 1031 stream_->buffer_.AddChar('"');
1005 free(p); 1032 if (p != &on_stack_buffer[0]) {
1033 free(p);
1034 }
1006 } 1035 }
1007 1036
1008 1037
1009 void JSONArray::AddValueF(const char* format, ...) const { 1038 void JSONArray::AddValueF(const char* format, ...) const {
1039 const intptr_t kOnStackBufferCapacity = 4096;
1040 char on_stack_buffer[kOnStackBufferCapacity];
1010 stream_->PrintCommaIfNeeded(); 1041 stream_->PrintCommaIfNeeded();
1011 va_list args; 1042 va_list args;
1012 va_start(args, format); 1043 va_start(args, format);
1013 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 1044 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
1014 va_end(args); 1045 va_end(args);
1015 char* p = reinterpret_cast<char*>(malloc(len + 1)); 1046 char* p = &on_stack_buffer[0];
1047 if (len + 1 >= kOnStackBufferCapacity) {
1048 p = reinterpret_cast<char*>(malloc(len + 1));
1049 }
1016 va_start(args, format); 1050 va_start(args, format);
1017 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); 1051 intptr_t len2 = OS::VSNPrint(p, len + 1, format, args);
1018 va_end(args); 1052 va_end(args);
1019 ASSERT(len == len2); 1053 ASSERT(len == len2);
1020 stream_->buffer_.AddChar('"'); 1054 stream_->buffer_.AddChar('"');
1021 stream_->AddEscapedUTF8String(p); 1055 stream_->AddEscapedUTF8String(p, len);
1022 stream_->buffer_.AddChar('"'); 1056 stream_->buffer_.AddChar('"');
1023 free(p); 1057 if (p != &on_stack_buffer[0]) {
1058 free(p);
1059 }
1024 } 1060 }
1025 1061
1026 #endif // !PRODUCT 1062 #endif // !PRODUCT
1027 1063
1028 } // namespace dart 1064 } // 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