Improve performance for icinga::Deserialize.

Refs #5327
This commit is contained in:
Gunnar Beutner 2013-12-13 12:54:14 +01:00
parent f8d7f7799e
commit e7184225f3
5 changed files with 90 additions and 19 deletions

View File

@ -161,21 +161,24 @@ static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary
if (!instance)
instance = type->Instantiate();
for (int i = 0; i < type->GetFieldCount(); i++) {
Field field = type->GetFieldInfo(i);
BOOST_FOREACH(const Dictionary::Pair& kv, input) {
if (kv.first.IsEmpty())
continue;
int fid = type->GetFieldId(kv.first);
if (fid < 0)
continue;
Field field = type->GetFieldInfo(fid);
if ((field.Attributes & attributeTypes) == 0)
continue;
Value value = input->Get(field.Name);
if (value.IsEmpty())
continue;
try {
instance->SetField(i, Deserialize(value, attributeTypes));
instance->SetField(fid, Deserialize(kv.second, attributeTypes));
} catch (const std::exception&) {
instance->SetField(i, Empty);
instance->SetField(fid, Empty);
}
}

View File

@ -705,12 +705,18 @@ String Utility::GetThreadName(void)
return *name;
}
unsigned long Utility::SDBM(const String& str)
unsigned long Utility::SDBM(const String& str, size_t len)
{
unsigned long hash = 0;
size_t current = 0;
BOOST_FOREACH(char c, str) {
if (current >= len)
break;
hash = c + (hash << 6) + (hash << 16) - hash;
current++;
}
return hash;

View File

@ -102,7 +102,7 @@ public:
static void SetThreadName(const String& name, bool os = true);
static String GetThreadName(void);
static unsigned long SDBM(const String& str);
static unsigned long SDBM(const String& str, size_t len = String::NPos);
static int CompareVersion(const String& v1, const String& v2);

View File

@ -21,6 +21,8 @@
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <map>
#include <vector>
using namespace icinga;
@ -77,6 +79,27 @@ void ClassCompiler::HandleCode(const std::string& code, const ClassDebugInfo& lo
std::cout << code << std::endl;
}
unsigned long ClassCompiler::SDBM(const std::string& str, size_t len = std::string::npos)
{
unsigned long hash = 0;
size_t current = 0;
std::string::const_iterator it;
for (it = str.begin(); it != str.end(); it++) {
if (current >= len)
break;
char c = *it;
hash = c + (hash << 6) + (hash << 16) - hash;
current++;
}
return hash;
}
void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
{
std::vector<Field>::const_iterator it;
@ -136,11 +159,47 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
std::cout << ";" << std::endl << std::endl;
int num = 0;
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
std::cout << "\t\t" << "if (name == \"" << it->Name << "\")" << std::endl
<< "\t\t\t" << "return offset + " << num << ";" << std::endl;
num++;
std::map<int, std::vector<std::pair<int, std::string> > > jumptable;
int hlen = 0, collisions = 0;
do {
int num = 0;
hlen++;
jumptable.clear();
collisions = 0;
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
int hash = static_cast<int>(SDBM(it->Name, hlen));
jumptable[hash].push_back(std::make_pair(num, it->Name));
num++;
if (jumptable[hash].size() > 1)
collisions++;
}
} while (collisions >= 5 && hlen < 8);
if (!klass.Fields.empty()) {
std::cout << "\t\tswitch (static_cast<int>(Utility::SDBM(name, " << hlen << "))) {" << std::endl;
std::map<int, std::vector<std::pair<int, std::string> > >::const_iterator itj;
for (itj = jumptable.begin(); itj != jumptable.end(); itj++) {
std::cout << "\t\t\tcase " << itj->first << ":" << std::endl;
std::vector<std::pair<int, std::string> >::const_iterator itf;
for (itf = itj->second.begin(); itf != itj->second.end(); itf++) {
std::cout << "\t\t\t\t" << "if (name == \"" << itf->second << "\")" << std::endl
<< "\t\t\t\t\t" << "return offset + " << itf->first << ";" << std::endl;
}
std::cout << std::endl
<< "\t\t\t\tbreak;" << std::endl;
}
std::cout << "\t\t}" << std::endl;
}
std::cout << std::endl
@ -178,7 +237,7 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
std::cout << ") {" << std::endl;
num = 0;
size_t num = 0;
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
std::cout << "\t\t\t" << "case " << num << ":" << std::endl
<< "\t\t\t\t" << "return Field(" << num << ", \"" << it->Name << "\", " << it->Attributes << ");" << std::endl;
@ -259,7 +318,7 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
std::cout << ") {" << std::endl;
num = 0;
size_t num = 0;
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
std::cout << "\t\t\t" << "case " << num << ":" << std::endl
<< "\t\t\t\t" << "Set" << it->GetFriendlyName() << "(";
@ -417,7 +476,8 @@ void ClassCompiler::CompileStream(const std::string& path, std::istream *stream)
<< "#include \"base/debug.h\"" << std::endl
<< "#include \"base/value.h\"" << std::endl
<< "#include \"base/array.h\"" << std::endl
<< "#include \"base/dictionary.h\"" << std::endl << std::endl
<< "#include \"base/dictionary.h\"" << std::endl
<< "#include \"base/utility.h\"" << std::endl << std::endl
<< "#ifdef _MSC_VER" << std::endl
<< "#pragma warning( push )" << std::endl
<< "#pragma warning( disable : 4244 )" << std::endl

View File

@ -142,6 +142,8 @@ private:
std::string m_Path;
std::istream *m_Input;
void *m_Scanner;
static unsigned long SDBM(const std::string& str, size_t len);
};
}