浏览代码

Kernel: Avoid some allocations in command line parsing (#3213)

1. Preallocate args hashmap to prevent rehashing.
2. Use move to prevent string copies.
Muhammad Zahalqa 4 年之前
父节点
当前提交
7506adbece
共有 1 个文件被更改,包括 5 次插入3 次删除
  1. 5 3
      Kernel/CommandLine.cpp

+ 5 - 3
Kernel/CommandLine.cpp

@@ -46,7 +46,9 @@ CommandLine::CommandLine(const String& string)
 {
     s_the = this;
 
-    for (auto str : m_string.split(' ')) {
+    const auto& args = m_string.split(' ');
+    m_params.ensure_capacity(args.size());
+    for (auto&& str : args) {
         if (str == "") {
             continue;
         }
@@ -54,9 +56,9 @@ CommandLine::CommandLine(const String& string)
         auto pair = str.split_limit('=', 2);
 
         if (pair.size() == 1) {
-            m_params.set(pair[0], "");
+            m_params.set(move(pair[0]), "");
         } else {
-            m_params.set(pair[0], pair[1]);
+            m_params.set(move(pair[0]), move(pair[1]));
         }
     }
 }