]> granicus.if.org Git - icinga2/commitdiff
mkclass: Optimize struct layout
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 7 Nov 2014 22:07:02 +0000 (23:07 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 7 Nov 2014 22:07:28 +0000 (23:07 +0100)
fixes #7615

tools/mkclass/class_parser.yy
tools/mkclass/classcompiler.cpp
tools/mkclass/classcompiler.hpp

index 5ab6621c02e71dd12a9592b34ef15accd9b624ae..ae4185a164a124b8ea64d3e3319335e6c418a3ba 100644 (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);
        }
        ;
 
index e4be9d8250ac38448392e5af09b9403d7cf3e394..5387b394f107da1dd37c6640740d594267960db6 100644 (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;
index f317492f9aee663595b0a59ec6ef5c925564825c..a8ee7530be4e33c09f4285232646c344d2fae74b 100644 (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;