浏览代码

AvailableDescriptionLanguages: Use one string for all iterations

Do not create strings within the loop, that creates one string
per language and does more work than needed. Instead, reserve
enough space at the beginning and assign the prefix, and then
resize and append inside the loop.

Also call exists with the string itself instead of the c_str(),
this means that the lookup uses the size information in the
string now and does not have to call strlen() on it.
Julian Andres Klode 10 年之前
父节点
当前提交
4a666d28d7
共有 1 个文件被更改,包括 9 次插入2 次删除
  1. 9 2
      apt-pkg/deb/deblistparser.cc

+ 9 - 2
apt-pkg/deb/deblistparser.cc

@@ -258,12 +258,19 @@ std::vector<std::string> debListParser::AvailableDescriptionLanguages()
 {
    std::vector<std::string> const understood = APT::Configuration::getLanguages();
    std::vector<std::string> avail;
+   static constexpr int prefixLen = 12;
+   static constexpr int avgLanguageLen = 5;
+   std::string tagname;
+
+   tagname.reserve(prefixLen + avgLanguageLen);
+   tagname.assign("Description-");
    if (Section.Exists("Description") == true)
       avail.push_back("");
    for (std::vector<std::string>::const_iterator lang = understood.begin(); lang != understood.end(); ++lang)
    {
-      std::string const tagname = "Description-" + *lang;
-      if (Section.Exists(tagname.c_str()) == true)
+      tagname.resize(prefixLen);
+      tagname.append(*lang);
+      if (Section.Exists(tagname) == true)
 	 avail.push_back(*lang);
    }
    return avail;