jsoncpp 執行效率優化,非常規極限優化,適合linux下, 效率幾乎提高50%以上
這是2年多前, 一個遊戲伺服器專案要上線了,協議訊息處理和資料存放都基本用json的,用的是jsoncpp開源庫
主要邏輯處理部分是單執行緒處理,所以玩家一多cpu就吃不消了, 要優化,
用gprof等工具找啊找研究發現是 主要json部分引起的一些記憶體開銷佔用cpu資源過多。(還有一些智慧指標,按下不表)
找了很多方法優化jsoncpp,
1.比如 http://www.2cto.com/kf/201211/172375.html
只優化了一些。
2.另外, Json::Value物件有個 swap介面,所有的 賦值操作能改 swap的都用swap(因為直接 = 賦值,會做一次物件拷貝)
3. 然後 資料巢狀的 基本不用append , 都用 Json::Value &one = jv_test[jv_test.size()]; 先取出來再賦值,這樣就省了 append時的一次拷貝
4.StyledWriter儘量都變成 FastWriter的格式化
但改了好多程式碼,只是稍稍提高了點效率
5. 繼續修改jsoncpp原始碼 把註釋的處理程式碼去掉,好像用處也不大。
後來仔細看了一下jsoncpp程式碼, 發現 特別是writer裡面, 有個字串document_ 一直再 += , 拼接字串,
原先程式碼 沒用用一個統一的writer格式化, 很多都是用toStyledString()
std::string Value::toStyledString() const {
//StyledWriter writer;
FastWriter writer;
return writer.write(*this);
}
可想而知 這個document_ 這個字串容器在 拼接字串要分配多少次記憶體哈,不可想象。
如果改程式碼,量太大
就直接改底層的
一、writer.h裡 注掉
//std::string document_;
二、json_writer.h裡改一下程式碼
使用一個 執行緒級的 全域性靜態變數 替換 document_;
std::string valueToQuotedString(const char *value, <strong>std::string* document</strong> /* = NULL*/) {
if (value == NULL)
return "";
// Not sure how to handle unicode...
if (strpbrk(value, "\"\\\b\f\n\r\t") == NULL &&
!containsControlCharacter(value))
{<strong>
if(document != NULL)
{
std::string tmp = std::string("\"") + value + "\"";
*document += tmp;
return "";
}
else
{
return std::string("\"") + value + "\"";
}</strong>
}
// We have to walk value and escape any special characters.
// Appending to std::string is not efficient, but this should be rare.
// (Note: forward slashes are *not* rare, but I am not escaping them.)
std::string::size_type maxsize =
strlen(value) * 2 + 3; // allescaped+quotes+NULL
std::string new_result;
std::string* result = &new_result;
if(document != NULL)
{
result = document;
}
else
{
(*result).reserve(maxsize); // to avoid lots of mallocs
}
(*result) += "\"";
for (const char *c = value; *c != 0; ++c) {
switch (*c) {
case '\"':
(*result) += "\\\"";
break;
case '\\':
(*result) += "\\\\";
break;
case '\b':
(*result) += "\\b";
break;
case '\f':
(*result) += "\\f";
break;
case '\n':
(*result) += "\\n";
break;
case '\r':
(*result) += "\\r";
break;
case '\t':
(*result) += "\\t";
break;
// case '/':
// Even though \/ is considered a legal escape in JSON, a bare
// slash is also legal, so I see no reason to escape it.
// (I hope I am not misunderstanding something.
// blep notes: actually escaping \/ may be useful in javascript to avoid </
// sequence.
// Should add a flag to allow this compatibility mode and prevent this
// sequence from occurring.
default:
if (isControlCharacter(*c)) {
std::ostringstream oss;
oss << "\\u" << std::hex << std::uppercase << std::setfill('0')
<< std::setw(4) << static_cast<int>(*c);
(*result) += oss.str();
} else {
(*result) += *c;
}
break;
}
}
(*result) += "\"";
return new_result;
}
<strong>
//暫不考慮釋放問題,如果執行緒不停建立使用,自動釋放請參考http://www.searchtb.com/2012/09/tls.html
static __thread std::string* jw_document_ = NULL;
#define document_ (*jw_document_)</strong>
// Class Writer
// //////////////////////////////////////////////////////////////////
Writer::~Writer() {}
// Class FastWriter
// //////////////////////////////////////////////////////////////////
FastWriter::FastWriter()
: yamlCompatiblityEnabled_(false), dropNullPlaceholders_(false) {
<strong> if(NULL == jw_document_)
{
jw_document_ = new std::string();
//printf("###FastWriter::FastWriter() new string()\n");
}</strong>
}
void FastWriter::enableYAMLCompatibility() { yamlCompatiblityEnabled_ = true; }
void FastWriter::dropNullPlaceholders() { dropNullPlaceholders_ = true; }
std::string FastWriter::write(const Value &root) {
<strong> document_.clear();</strong>
writeValue(root);
//document_ += "\n";
return document_;
}
void FastWriter::writeValue(const Value &value) {
switch (value.type()) {
case nullValue:
if (!dropNullPlaceholders_)
document_ += "null";
break;
case intValue:
document_ += valueToString(value.asLargestInt());
break;
case uintValue:
document_ += valueToString(value.asLargestUInt());
break;
case realValue:
document_ += valueToString(value.asDouble());
break;
case stringValue:<strong>
//document_ += valueToQuotedString(value.asCString());
valueToQuotedString(value.asCString(),&document_);</strong>
break;
case booleanValue:
document_ += valueToString(value.asBool());
break;
case arrayValue: {
document_ += "[";
int size = value.size();
for (int index = 0; index < size; ++index) {
if (index > 0)
document_ += ",";
writeValue(value[index]);
}
document_ += "]";
} break;
case objectValue: {
document_ += "{";
Value::ObjectValues* value_map = value.getValueMap();
if(value_map != NULL)
{
Value::ObjectValues::iterator it = value_map->begin();
for ( Value::ObjectValues::iterator it = value_map->begin();
it != value_map->end();
++it )
{
if ( it != value_map->begin() )
document_ += ",";
const char* name = it->first.c_str();
valueToQuotedString( name, &document_ );
document_ += yamlCompatiblityEnabled_ ? ": " : ":";
writeValue( it->second );
}
}
/*
Value::Members members(value.getMemberNames());
document_ += "{";
for (Value::Members::iterator it = members.begin(); it != members.end();
++it) {
const std::string &name = *it;
if (it != members.begin())
document_ += ",";
document_ += valueToQuotedString(name.c_str());
document_ += yamlCompatiblityEnabled_ ? ": " : ":";
writeValue(value[name]);
}
*/
document_ += "}";
} break;
}
整體效率一下提高了50% - -!
<strong>__thread </strong>
是保證多執行緒下沒有問題。
reader也可以優化看看。
這個優化如果有需要可以拿去試試。 個人覺得全域性 統一用一個writer來輸出也不錯(不過要注意多執行緒的問題)。