llvm.org GIT mirror llvm / 461c876
Try to speed up the slowest parts of the CommandLine library - Replace std::map<std::string with StringMap - Eliminate unnecessary std::string copies - ~10% speed-up for clang's testsuite on my machine (debug build) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82312 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 10 years ago
1 changed file(s) with 31 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
2424 #include "llvm/System/Host.h"
2525 #include "llvm/System/Path.h"
2626 #include "llvm/ADT/OwningPtr.h"
27 #include "llvm/ADT/StringMap.h"
2728 #include "llvm/Config/config.h"
28 #include
2929 #include
3030 #include
3131 #include
104104 /// structures that are easier to handle.
105105 static void GetOptionInfo(std::vector &PositionalOpts,
106106 std::vector &SinkOpts,
107 std::mapOption*> &OptionsMap) {
107 StringMap<Option*> &OptionsMap) {
108108 std::vector OptionNames;
109109 Option *CAOpt = 0; // The ConsumeAfter option if it exists.
110110 for (Option *O = RegisteredOptionList; O; O = O->getNextRegisteredOption()) {
117117 // Handle named options.
118118 for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
119119 // Add argument to the argument map!
120 if (!OptionsMap.insert(std::pair(OptionNames[i],
121 O)).second) {
120 if (OptionsMap.GetOrCreateValue(OptionNames[i], O).second != O) {
122121 errs() << ProgramName << ": CommandLine Error: Argument '"
123122 << OptionNames[i] << "' defined more than once!\n";
124123 }
150149 /// command line. If there is a value specified (after an equal sign) return
151150 /// that as well.
152151 static Option *LookupOption(const char *&Arg, const char *&Value,
153 std::mapOption*> &OptionsMap) {
152 StringMap<Option*> &OptionsMap) {
154153 while (*Arg == '-') ++Arg; // Eat leading dashes
155154
156155 const char *ArgEnd = Arg;
164163 if (*Arg == 0) return 0;
165164
166165 // Look up the option.
167 std::map::iterator I =
168 OptionsMap.find(std::string(Arg, ArgEnd));
166 StringMap::iterator I =
167 OptionsMap.find(llvm::StringRef(Arg, ArgEnd-Arg));
169168 return I != OptionsMap.end() ? I->second : 0;
170169 }
171170
258257 //
259258 static Option *getOptionPred(std::string Name, size_t &Length,
260259 bool (*Pred)(const Option*),
261 std::map &OptionsMap) {
262
263 std::map::iterator OMI = OptionsMap.find(Name);
260 StringMap &OptionsMap) {
261
262 StringMap::iterator OMI = OptionsMap.find(Name);
264263 if (OMI != OptionsMap.end() && Pred(OMI->second)) {
265264 Length = Name.length();
266265 return OMI->second;
416415 // Process all registered options.
417416 std::vector PositionalOpts;
418417 std::vector SinkOpts;
419 std::mapOption*> Opts;
418 StringMap<Option*> Opts;
420419 GetOptionInfo(PositionalOpts, SinkOpts, Opts);
421420
422421 assert((!Opts.empty() || !PositionalOpts.empty()) &&
726725 }
727726
728727 // Loop over args and make sure all required args are specified!
729 for (std::mapOption*>::iterator I = Opts.begin(),
728 for (StringMap<Option*>::iterator I = Opts.begin(),
730729 E = Opts.end(); I != E; ++I) {
731730 switch (I->second->getNumOccurrencesFlag()) {
732731 case Required:
950949 // argument string. If the option is not found, getNumOptions() is returned.
951950 //
952951 unsigned generic_parser_base::findOption(const char *Name) {
953 unsigned i = 0, e = getNumOptions();
954 std::string N(Name);
955
956 while (i != e)
957 if (getOption(i) == N)
952 unsigned e = getNumOptions();
953
954 for (unsigned i = 0; i != e; ++i) {
955 if (strcmp(getOption(i), Name) == 0)
958956 return i;
959 else
960 ++i;
957 }
961958 return e;
962959 }
963960
10161013 const bool ShowHidden;
10171014
10181015 // isHidden/isReallyHidden - Predicates to be used to filter down arg lists.
1019 inline static bool isHidden(std::pair &OptPair) {
1020 return OptPair.second->getOptionHiddenFlag() >= Hidden;
1021 }
1022 inline static bool isReallyHidden(std::pair &OptPair) {
1023 return OptPair.second->getOptionHiddenFlag() == ReallyHidden;
1016 inline static bool isHidden(Option *Opt) {
1017 return Opt->getOptionHiddenFlag() >= Hidden;
1018 }
1019 inline static bool isReallyHidden(Option *Opt) {
1020 return Opt->getOptionHiddenFlag() == ReallyHidden;
10241021 }
10251022
10261023 public:
10341031 // Get all the options.
10351032 std::vector PositionalOpts;
10361033 std::vector SinkOpts;
1037 std::mapOption*> OptMap;
1034 StringMap<Option*> OptMap;
10381035 GetOptionInfo(PositionalOpts, SinkOpts, OptMap);
10391036
10401037 // Copy Options into a vector so we can sort them as we like...
1041 std::vector > Opts;
1042 copy(OptMap.begin(), OptMap.end(), std::back_inserter(Opts));
1038 std::vector Opts;
1039 for (StringMap::iterator I = OptMap.begin(), E = OptMap.end();
1040 I != E; ++I) {
1041 Opts.push_back(I->second);
1042 }
10431043
10441044 // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden
10451045 Opts.erase(std::remove_if(Opts.begin(), Opts.end(),
10501050 { // Give OptionSet a scope
10511051 std::set OptionSet;
10521052 for (unsigned i = 0; i != Opts.size(); ++i)
1053 if (OptionSet.count(Opts[i].second) == 0)
1054 OptionSet.insert(Opts[i].second); // Add new entry to set
1053 if (OptionSet.count(Opts[i]) == 0)
1054 OptionSet.insert(Opts[i]); // Add new entry to set
10551055 else
10561056 Opts.erase(Opts.begin()+i--); // Erase duplicate
10571057 }
10811081 // Compute the maximum argument length...
10821082 MaxArgLen = 0;
10831083 for (size_t i = 0, e = Opts.size(); i != e; ++i)
1084 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
1084 MaxArgLen = std::max(MaxArgLen, Opts[i]->getOptionWidth());
10851085
10861086 outs() << "OPTIONS:\n";
10871087 for (size_t i = 0, e = Opts.size(); i != e; ++i)
1088 Opts[i].second->printOptionInfo(MaxArgLen);
1088 Opts[i]->printOptionInfo(MaxArgLen);
10891089
10901090 // Print any extra help the user has declared.
10911091 for (std::vector::iterator I = MoreHelp->begin(),