Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeTools/DeclStats.cc

    r424931d r6a5f0e7  
    3131       
    3232        class DeclStats : public Visitor {
    33                 template<typename T>
    34                 static void sum(T& a, const T& b) { a += b; }
    35 
    36                 static void sum(VectorMap<unsigned>& a, const VectorMap<unsigned>& b) {
    37                         a.reserve( b.size() );
    38                         for ( unsigned i = 0; i < b.size(); ++i ) {
    39                                 a[i] += b[i];
    40                         }
    41                 }
    42 
    43                 template<typename K>
    44                 static void sum(std::map<K, unsigned>& a, const std::map<K, unsigned>& b) {
    45                         for ( const auto& entry : b ) {
    46                                 a[ entry.first ] += entry.second;
    47                         }
    48                 }
    49 
    50                 template<typename K>
    51                 static void sum(std::unordered_map<K, unsigned>& a, const std::unordered_map<K, unsigned>& b) {
    52                         for ( const auto& entry : b ) {
    53                                 a[ entry.first ] += entry.second;
    54                         }
    55                 }
    56 
    57                 struct ArgPackStats {
    58                         VectorMap<unsigned> n;                 ///< Count of decls with each number of elements
    59                         VectorMap<unsigned> n_basic;           ///< Count of decls with each number of basic type elements
    60                         VectorMap<unsigned> n_poly;            ///< Count of decls with each number of polymorphic elements
    61                         std::map<unsigned, unsigned> p_basic;  ///< Count of decls with each percentage of basic type elements
    62                         std::map<unsigned, unsigned> p_poly;   ///< Count of decls with each percentage of polymorphic elements
    63 
    64                         ArgPackStats& operator+= (const ArgPackStats& o) {
    65                                 sum(n, o.n);
    66                                 sum(n_basic, o.n_basic);
    67                                 sum(n_poly, o.n_poly);
    68                                 sum(p_basic, o.p_basic);
    69                                 sum(p_poly, o.p_poly);
    70                                
    71                                 return *this;
    72                         }
    73 
    74                         /// Update based on a declaration list
    75                         ArgPackStats& operator+= ( std::list<DeclarationWithType*>& decls ) {
    76                                 unsigned nn = 0;
    77                                 unsigned nn_basic = 0;
    78                                 unsigned nn_poly = 0;
    79                                 for ( auto decl : decls ) {
    80                                         nn += decl->get_type()->size();
    81                                         if ( dynamic_cast<BasicType*>( decl->get_type() ) ) ++nn_basic;
    82                                         else if ( GenPoly::hasPolyBase( decl->get_type() ) ) ++nn_poly;
    83                                 }
    84                                 ++n.at( nn );
    85                                 ++n_basic.at( nn_basic );
    86                                 ++n_poly.at( nn_poly );
    87                                 if ( nn > 0 ) {
    88                                         ++p_basic[ nn_basic*100/nn ];
    89                                         ++p_poly[ nn_poly*100/nn ];
    90                                 }
    91                                
    92                                 return *this;
    93                         }
    94                 };
    95                
    9633                struct Stats {
    9734                        unsigned n_decls;     ///< Total number of declarations
     
    10037                        /// Count of declarations with each name
    10138                        std::unordered_map<std::string, unsigned> by_name;
    102                         /// Stats for the parameter list
    103                         ArgPackStats params;
    104                         /// Stats for the return list
    105                         ArgPackStats returns;
    106                        
     39
     40                        /// Count of declarations with each number of parameters
     41                        VectorMap<unsigned> n_params;
     42                        /// Count of declarations with each number of return types
     43                        VectorMap<unsigned> n_returns;
     44                        /// Count of declarations with each number of polymorphic parameters
     45                        VectorMap<unsigned> n_poly_params;
     46                        /// Count of declarations with each number of polymorphic return types
     47                        VectorMap<unsigned> n_poly_returns;
     48                        /// Count of declarations with each percentage of polymorphic parameters
     49                        std::map<unsigned, unsigned> p_poly_params;
     50                        /// Count of declarations with each percentage of polymorphic returns
     51                        std::map<unsigned, unsigned> p_poly_returns;
     52
    10753                        /// Count of declarations with each number of assertions
    108                         std::map<unsigned, unsigned> n_assns;
    109                         /// Stats for the assertions' parameters
    110                         ArgPackStats assn_params;
    111                         /// Stats for the assertions' return types
    112                         ArgPackStats assn_returns;
    113                        
    114                         Stats() : n_decls(0), n_type_params(), by_name(), params(), returns(), n_assns(), assn_params(), assn_returns() {}
     54                        std::map<unsigned, unsigned> n_assertions;
     55                        /// Count of assertions with each number of parameters
     56                        VectorMap<unsigned> n_assn_params;
     57                        /// Count of assertions with each number of return types
     58                        VectorMap<unsigned> n_assn_returns;
     59                        /// Count of assertions with each number of polymorphic parameters
     60                        VectorMap<unsigned> n_assn_poly_params;
     61                        /// Count of assertions with each number of polymorphic return types
     62                        VectorMap<unsigned> n_assn_poly_returns;
     63                        /// Count of assertions with each percentage of polymorphic parameters
     64                        std::map<unsigned, unsigned> p_assn_poly_params;
     65                        /// Count of assertions with each percentage of polymorphic returns
     66                        std::map<unsigned, unsigned> p_assn_poly_returns;
     67
     68                        Stats()
     69                                : n_decls(0), n_type_params(), by_name(),
     70                                  n_params(), n_returns(), n_poly_params(), n_poly_returns(), p_poly_params(), p_poly_returns(),
     71                                  n_assertions(), n_assn_params(), n_assn_returns(), n_assn_poly_params(), n_assn_poly_returns(), p_assn_poly_params(), p_assn_poly_returns() {}
     72
     73                private:
     74                        template<typename T>
     75                        static void sum(T& a, const T& b) { a += b; }
     76
     77                        static void sum(VectorMap<unsigned>& a, const VectorMap<unsigned>& b) {
     78                                a.reserve( b.size() );
     79                                for ( unsigned i = 0; i < b.size(); ++i ) {
     80                                        a[i] += b[i];
     81                                }
     82                        }
     83
     84                        template<typename K>
     85                        static void sum(std::map<K, unsigned>& a, const std::map<K, unsigned>& b) {
     86                                for ( const auto& entry : b ) {
     87                                        a[ entry.first ] += entry.second;
     88                                }
     89                        }
     90
     91                        template<typename K>
     92                        static void sum(std::unordered_map<K, unsigned>& a, const std::unordered_map<K, unsigned>& b) {
     93                                for ( const auto& entry : b ) {
     94                                        a[ entry.first ] += entry.second;
     95                                }
     96                        }
    11597
    11698                public:
     
    119101                                sum( n_type_params, o.n_type_params );
    120102                                sum( by_name, o.by_name );
    121                                 sum( params, o.params );
    122                                 sum( returns, o.returns );
    123                                 sum( n_assns, o.n_assns );
    124                                 sum( assn_params, o.assn_params );
    125                                 sum( assn_returns, o.assn_returns );
    126103                               
     104                                sum( n_params, o.n_params );
     105                                sum( n_returns, o.n_returns );
     106                                sum( n_poly_params, o.n_poly_params );
     107                                sum( n_poly_returns, o.n_poly_returns );
     108                                sum( p_poly_params, o.p_poly_params );
     109                                sum( p_poly_returns, o.p_poly_returns );
     110
     111                                sum( n_assertions, o.n_assertions );
     112                                sum( n_assn_params, o.n_assn_params );
     113                                sum( n_assn_returns, o.n_assn_returns );
     114                                sum( n_assn_poly_params, o.n_assn_poly_params );
     115                                sum( n_assn_poly_returns, o.n_assn_poly_returns );
     116                                sum( p_assn_poly_params, o.p_assn_poly_params );
     117                                sum( p_assn_poly_returns, o.p_assn_poly_returns );
     118
    127119                                return *this;
    128120                        }
     
    133125                Stats total;
    134126
    135                 void analyzeFunc( FunctionType* fnTy, ArgPackStats& params, ArgPackStats& returns ) {
    136                         params += fnTy->get_parameters();
    137                         returns += fnTy->get_returnVals();
     127                void analyzeFunc( FunctionType* fnTy, VectorMap<unsigned>& by_params, VectorMap<unsigned>& by_poly_params,
     128                                  VectorMap<unsigned>& by_returns, VectorMap<unsigned>& by_poly_returns,
     129                                  std::map<unsigned, unsigned>& p_poly_params, std::map<unsigned, unsigned>& p_poly_returns ) {
     130                        unsigned n_params = 0;
     131                        unsigned n_poly_params = 0;
     132                        for ( auto pdecl : fnTy->get_parameters() ) {
     133                                n_params += pdecl->get_type()->size();
     134                                if ( GenPoly::hasPolyBase( pdecl->get_type() ) ) ++n_poly_params;
     135                        }
     136                        ++by_params.at( n_params );
     137                        ++by_poly_params.at( n_poly_params );
     138                        if ( n_params > 0 ) ++p_poly_params[ n_poly_params*100/n_params ];
     139
     140                        unsigned n_returns = 0;
     141                        unsigned n_poly_returns = 0;
     142                        for ( auto rdecl : fnTy->get_returnVals() ) {
     143                                n_returns += rdecl->get_type()->size();
     144                                if ( GenPoly::hasPolyBase( rdecl->get_type() ) ) ++n_poly_returns;
     145                        }
     146                        ++by_returns.at( n_returns );
     147                        ++by_poly_returns.at( n_poly_returns );
     148                        if ( n_returns > 0 ) ++p_poly_returns[ n_poly_returns*100/n_returns ];
    138149                }
    139150
     
    152163                        const Type::ForallList& forall = fnTy->get_forall();
    153164                        ++stats.n_type_params.at( forall.size() );
    154                         unsigned n_assns = 0;
     165                        unsigned n_assertions = 0;
    155166                        for ( TypeDecl* fdecl : forall ) {
    156                                 n_assns += fdecl->get_assertions().size();
     167                                n_assertions += fdecl->get_assertions().size();
    157168                                for ( DeclarationWithType* assn : fdecl->get_assertions() ) {
    158169                                        FunctionType *assnTy = 0;
     
    164175                                                assnTy = assnDecl->get_functionType();
    165176                                        }
    166                                         if ( assnTy ) analyzeFunc( assnTy, stats.assn_params, stats.assn_returns );
    167                                 }
    168                         }
    169                         ++stats.n_assns[ n_assns ];
     177                                        if ( assnTy ) analyzeFunc( assnTy, stats.n_assn_params, stats.n_assn_poly_params, stats.n_assn_returns,
     178                                                                   stats.n_assn_poly_returns, stats.p_assn_poly_params, stats.p_assn_poly_returns );
     179                                }
     180                        }
     181                        ++stats.n_assertions[ n_assertions ];
    170182
    171183                        ++stats.by_name[ decl->get_name() ];
    172184
    173                         analyzeFunc( fnTy, stats.params, stats.returns );
     185                        analyzeFunc( fnTy, stats.n_params, stats.n_poly_params, stats.n_returns,
     186                                     stats.n_poly_returns, stats.p_poly_params, stats.p_poly_returns );
    174187                }
    175188
    176189        private:
    177190                template<typename F>
    178                 void printAll( const std::string& name, F extract ) {
     191                void printAll( const char* name, F extract ) {
    179192                        std::cout << "\"" << name << "\",";
    180193                        for ( const auto& stats : for_linkage ) {
     
    185198
    186199                template<typename F>
    187                 void printAllMap( const std::string& name, F extract ) {
     200                void printAllMap( const char* name, F extract ) {
    188201                        for ( const auto& entry : extract(total) ) {
    189202                                const auto& key = entry.first;
     
    200213
    201214                template<typename F>
    202                 void printAllHisto( const std::string& name, F extract ) {
     215                void printAllHisto( const char* name, F extract ) {
    203216                        VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs];
    204217                        VectorMap<unsigned> thisto;
     
    223236
    224237                template<typename F>
    225                 void printAllSparseHisto( const std::string& name, F extract ) {
     238                void printAllSparseHisto( const char* name, F extract ) {
    226239                        std::map<unsigned, unsigned> histos[LinkageSpec::NoOfSpecs];
    227240                        std::map<unsigned, unsigned> thisto;
     
    244257                        }
    245258                }
    246 
    247                 template<typename F>
    248                 void printAllPack( const std::string& name, F extract ) {
    249                         printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; });
    250                         printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; });
    251                         printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; });
    252                         printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; });
    253                         printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; });
    254                 }
    255259               
    256260        public:
     
    266270                        printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); });
    267271                        printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; });
    268                         printAllPack("params", [](const Stats& stats) { return stats.params; });
    269                         printAllPack("returns", [](const Stats& stats) { return stats.returns; });
    270                         printAllMap("n_assns", [](const Stats& stats) { return stats.n_assns; });
    271                         printAllPack("assn_params", [](const Stats& stats) { return stats.assn_params; });
    272                         printAllPack("assn_returns", [](const Stats& stats) { return stats.assn_returns; });
     272                       
     273                        printAllMap("n_poly_params", [](const Stats& stats) { return stats.n_poly_params; });
     274                        printAllMap("n_params", [](const Stats& stats) { return stats.n_params; });
     275                        printAllMap("%_poly_params", [](const Stats& stats) { return stats.p_poly_params; });
     276                        printAllMap("n_poly_returns", [](const Stats& stats) { return stats.n_poly_returns; });
     277                        printAllMap("n_returns", [](const Stats& stats) { return stats.n_returns; });
     278                        printAllMap("%_poly_returns", [](const Stats& stats) { return stats.p_poly_returns; });
     279
     280                        printAllMap("n_assertions", [](const Stats& stats) { return stats.n_assertions; });
     281                        printAllMap("n_assn_poly_params", [](const Stats& stats) { return stats.n_assn_poly_params; });
     282                        printAllMap("n_assn_params", [](const Stats& stats) { return stats.n_assn_params; });
     283                        printAllMap("%_assn_poly_params", [](const Stats& stats) { return stats.p_assn_poly_params; });
     284                        printAllMap("n_assn_poly_returns", [](const Stats& stats) { return stats.n_assn_poly_returns; });
     285                        printAllMap("n_assn_returns", [](const Stats& stats) { return stats.n_assn_returns; });
     286                        printAllMap("%_assn_poly_returns", [](const Stats& stats) { return stats.p_assn_poly_returns; });
    273287                }
    274288        };
Note: See TracChangeset for help on using the changeset viewer.