Commit 250e85f8 authored by alph's avatar alph Committed by Commit bot

[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}
parent f435d622
......@@ -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<unsigned>(static_cast<unsigned char>(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() {
......
......@@ -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();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment