Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1232)

Unified Diff: base/linux_util.cc

Issue 2882583003: Re-land: base: Avoid unnecessary allocations in base::FindThreadID. (Closed)
Patch Set: fix DCHECK Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/linux_util.cc
diff --git a/base/linux_util.cc b/base/linux_util.cc
index bf504718f06f2accdfb9656c8714a5ba6c536e9f..851a6c67ab0c8e6da77e53c1fbe019b93f853d1d 100644
--- a/base/linux_util.cc
+++ b/base/linux_util.cc
@@ -22,6 +22,7 @@
#include "base/process/launch.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
+#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "build/build_config.h"
@@ -199,25 +200,24 @@ pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) {
std::string status;
if (!ReadFileToString(FilePath(buf), &status))
return -1;
- StringPairs pairs;
- SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs);
- for (const auto& pair : pairs) {
- const std::string& key = pair.first;
- const std::string& value_str = pair.second;
- if (key == "NSpid") {
- if (ns_pid_supported)
- *ns_pid_supported = true;
- std::vector<StringPiece> split_value_str = SplitStringPiece(
- value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
- DCHECK_NE(split_value_str.size(), 0u);
- int value;
- // The last value in the list is the PID in the namespace.
- if (StringToInt(split_value_str.back(), &value) && value == ns_tid) {
- // The first value in the list is the real PID.
- if (StringToInt(split_value_str.front(), &value))
- return value;
- }
+ StringTokenizer tokenizer(status, "\n");
+ while (tokenizer.GetNext()) {
+ StringPiece value_str(tokenizer.token_piece());
+ if (!value_str.starts_with("NSpid"))
+ continue;
+ if (ns_pid_supported)
+ *ns_pid_supported = true;
+ std::vector<StringPiece> split_value_str = SplitStringPiece(
+ value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
+ DCHECK_GE(split_value_str.size(), 2u);
+ int value;
+ // The last value in the list is the PID in the namespace.
+ if (StringToInt(split_value_str.back(), &value) && value == ns_tid) {
+ // The second value in the list is the real PID.
+ if (StringToInt(split_value_str[1], &value))
+ return value;
}
+ break;
}
}
return -1;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698