Use string_view more often when reading /proc files on Linux
This avoids many small string copies, which can add up when reading many
/proc files.
To support this, adds SplitStringIntoKeyValuePairs variants that return
string_view.
Bug: 416524813
Change-Id: I8d692708488ff8b90bd37d255eef026778206563
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6557063
Reviewed-by: Francois Pierre Doray <[email protected]>
Commit-Queue: Joe Mason <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1461628}
diff --git a/base/process/internal_linux.h b/base/process/internal_linux.h
index 9b621410..3fa22ec 100644
--- a/base/process/internal_linux.h
+++ b/base/process/internal_linux.h
@@ -57,12 +57,14 @@
// e.g. /proc/self/ will return 0, whereas /proc/1234 will return 1234.
pid_t ProcDirSlotToPid(std::string_view d_name);
-// Read |filename| in /proc/<pid>/, split the entries into key/value pairs, and
-// trim the key and value. On success, return true and write the trimmed
-// key/value pairs into |key_value_pairs|.
-bool ReadProcFileToTrimmedStringPairs(pid_t pid,
- std::string_view filename,
- StringPairs* key_value_pairs);
+// Read `filename` in /proc/<pid>/, split the entries into key/value pairs, and
+// trim the key and value. On success, writes the file contents into `buffer`
+// and returns the trimmed key/value pairs as views into that buffer. On
+// failure, returns nullopt (the buffer contents may or may not be changed).
+std::optional<StringViewPairs> ReadProcFileToTrimmedStringPairs(
+ pid_t pid,
+ std::string_view filename,
+ std::string* buffer);
// Read /proc/<pid>/status and return the value for |field|, or 0 on failure.
// Only works for fields in the form of "Field: value kB".
@@ -79,11 +81,11 @@
// and is non-empty.
bool ReadProcStats(pid_t pid, std::string* buffer);
-// Takes |stats_data| and populates |proc_stats| with the values split by
-// spaces. Taking into account the 2nd field may, in itself, contain spaces.
-// Returns true if successful.
-bool ParseProcStats(const std::string& stats_data,
- std::vector<std::string>* proc_stats);
+// Takes `stats_data` and populates `proc_stats` with the values split by
+// spaces, as views into `stats_data`, taking into account the 2nd field may
+// itself contain spaces. Returns true if successful.
+bool ParseProcStats(std::string_view stats_data,
+ std::vector<std::string_view>* proc_stats);
// Fields from /proc/<pid>/stat, 0-based. See man 5 proc.
// If the ordering ever changes, carefully review functions that use these
@@ -106,18 +108,18 @@
// Reads the |field_num|th field from |proc_stats|. Returns 0 on failure.
// This version does not handle the first 3 values, since the first value is
// simply |pid|, and the next two values are strings.
-int64_t GetProcStatsFieldAsInt64(const std::vector<std::string>& proc_stats,
+int64_t GetProcStatsFieldAsInt64(base::span<std::string_view> proc_stats,
ProcStatsFields field_num);
// Reads the `field_num`th field from `proc_stats`. Asserts that `field_num` is
// a valid index into `proc_stats`. Returns nullopt if the field doesn't contain
// a valid integer.
std::optional<int64_t> GetProcStatsFieldAsOptionalInt64(
- base::span<const std::string> proc_stats,
+ base::span<std::string_view> proc_stats,
ProcStatsFields field_num);
// Same as GetProcStatsFieldAsInt64(), but for size_t values.
-size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats,
+size_t GetProcStatsFieldAsSizeT(base::span<std::string_view> proc_stats,
ProcStatsFields field_num);
// Convenience wrappers around GetProcStatsFieldAsInt64(), ParseProcStats() and