mkclass: Optimize struct layout

fixes #7615
This commit is contained in:
Gunnar Beutner 2014-11-07 23:07:02 +01:00
parent ade12dc27a
commit 85a380c443
3 changed files with 36 additions and 0 deletions

View File

@ -194,6 +194,8 @@ class: class_attribute_list T_CLASS T_IDENTIFIER inherits_specifier type_base_sp
$$->Fields = *$7;
delete $7;
ClassCompiler::OptimizeStructLayout($$->Fields);
}
;

View File

@ -106,6 +106,38 @@ unsigned long ClassCompiler::SDBM(const std::string& str, size_t len = std::stri
return hash;
}
static int TypePreference(const std::string& type)
{
if (type == "Value")
return 0;
else if (type == "String")
return 1;
else if (type == "double")
return 2;
else if (type.find("::Ptr") != std::string::npos)
return 3;
else if (type == "int")
return 4;
else
return 5;
}
static bool FieldLayoutCmp(const Field& a, const Field& b)
{
return TypePreference(a.Type) < TypePreference(b.Type);
}
static bool FieldTypeCmp(const Field& a, const Field& b)
{
return a.Type < b.Type;
}
void ClassCompiler::OptimizeStructLayout(std::vector<Field>& fields)
{
std::sort(fields.begin(), fields.end(), FieldTypeCmp);
std::stable_sort(fields.begin(), fields.end(), FieldLayoutCmp);
}
void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&)
{
std::vector<Field>::const_iterator it;

View File

@ -145,6 +145,8 @@ public:
static void CompileFile(const std::string& path);
static void CompileStream(const std::string& path, std::istream *stream);
static void OptimizeStructLayout(std::vector<Field>& fields);
private:
std::string m_Path;
std::istream *m_Input;