Skip to content

Commit

Permalink
Kernel: Avoid some allocations in command line parsing (SerenityOS#3213)
Browse files Browse the repository at this point in the history
1. Preallocate args hashmap to prevent rehashing.
2. Use move to prevent string copies.
  • Loading branch information
tryfinally committed Aug 23, 2020
1 parent 1b075ff commit 7506adb
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Kernel/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ 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;
}

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]));
}
}
}
Expand Down

0 comments on commit 7506adb

Please sign in to comment.