[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}
This commit is contained in:
alph 2016-12-15 10:03:45 -08:00 committed by Commit bot
parent f435d6222f
commit 250e85f84a
2 changed files with 21 additions and 21 deletions

View File

@ -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() {

View File

@ -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();