From 250e85f84abd24c4ddab2efc6aa0ddddab09c55d Mon Sep 17 00:00:00 2001 From: alph Date: Thu, 15 Dec 2016 10:03:45 -0800 Subject: [PATCH] [tracing] Make TracedValue accept const char* strings This should speed up trace generation for cpu profile as it eliminates creation of temporary std::string objects. Review-Url: https://codereview.chromium.org/2555043003 Cr-Commit-Position: refs/heads/master@{#41736} --- src/tracing/traced-value.cc | 34 +++++++++++++++------------------- src/tracing/traced-value.h | 8 ++++++-- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/tracing/traced-value.cc b/src/tracing/traced-value.cc index 3406bbf6fe..58d0f5d5ba 100644 --- a/src/tracing/traced-value.cc +++ b/src/tracing/traced-value.cc @@ -23,40 +23,36 @@ const bool kStackTypeArray = true; #define DEBUG_POP_CONTAINER() ((void)0) #endif -std::string EscapeString(const std::string& value) { - std::string result; - result.reserve(value.length() + 2); - result += '"'; - size_t length = value.length(); +void EscapeAndAppendString(const char* value, std::string* result) { + *result += '"'; char number_buffer[10]; - for (size_t src = 0; src < length; ++src) { - char c = value[src]; + while (*value) { + char c = *value++; switch (c) { case '\t': - result += "\\t"; + *result += "\\t"; break; case '\n': - result += "\\n"; + *result += "\\n"; break; case '\"': - result += "\\\""; + *result += "\\\""; break; case '\\': - result += "\\\\"; + *result += "\\\\"; break; default: if (c < '\040') { base::OS::SNPrintF( number_buffer, arraysize(number_buffer), "\\u%04X", static_cast(static_cast(c))); - result += number_buffer; + *result += number_buffer; } else { - result += c; + *result += c; } } } - result += '"'; - return result; + *result += '"'; } } // namespace @@ -93,10 +89,10 @@ void TracedValue::SetBoolean(const char* name, bool value) { data_ += value ? "true" : "false"; } -void TracedValue::SetString(const char* name, const std::string& value) { +void TracedValue::SetString(const char* name, const char* value) { DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict); WriteName(name); - data_ += EscapeString(value); + EscapeAndAppendString(value, &data_); } void TracedValue::BeginDictionary(const char* name) { @@ -133,10 +129,10 @@ void TracedValue::AppendBoolean(bool value) { data_ += value ? "true" : "false"; } -void TracedValue::AppendString(const std::string& value) { +void TracedValue::AppendString(const char* value) { DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray); WriteComma(); - data_ += EscapeString(value); + EscapeAndAppendString(value, &data_); } void TracedValue::BeginDictionary() { diff --git a/src/tracing/traced-value.h b/src/tracing/traced-value.h index befa387bc8..7de4c234a2 100644 --- a/src/tracing/traced-value.h +++ b/src/tracing/traced-value.h @@ -29,14 +29,18 @@ class TracedValue : public ConvertableToTraceFormat { void SetInteger(const char* name, int value); void SetDouble(const char* name, double value); void SetBoolean(const char* name, bool value); - void SetString(const char* name, const std::string& value); + void SetString(const char* name, const char* value); + void SetString(const char* name, const std::string& value) { + SetString(name, value.c_str()); + } void BeginDictionary(const char* name); void BeginArray(const char* name); void AppendInteger(int); void AppendDouble(double); void AppendBoolean(bool); - void AppendString(const std::string&); + void AppendString(const char*); + void AppendString(const std::string& value) { AppendString(value.c_str()); } void BeginArray(); void BeginDictionary();