Skip to content

Commit 311a1d0

Browse files
committed
Micro-optimize code size of /proc/maps parsing code
The vec move is very funny, as moving it removes the vec drop from the early return, which is fairly irrelevant but more than 0 bytes.
1 parent 344344b commit 311a1d0

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/symbolize/gimli/parse_running_mmaps_unix.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ pub(super) struct MapsEntry {
4444
}
4545

4646
pub(super) fn parse_maps() -> Result<Vec<MapsEntry>, &'static str> {
47-
let mut v = Vec::new();
4847
let mut proc_self_maps =
4948
File::open("/proc/self/maps").map_err(|_| "Couldn't open /proc/self/maps")?;
5049
let mut buf = String::new();
5150
let _bytes_read = proc_self_maps
5251
.read_to_string(&mut buf)
5352
.map_err(|_| "Couldn't read /proc/self/maps")?;
53+
54+
let mut v = Vec::new();
5455
for line in buf.lines() {
5556
v.push(line.parse()?);
5657
}
@@ -84,41 +85,50 @@ impl FromStr for MapsEntry {
8485
// Note that paths may contain spaces, so we can't use `str::split` for parsing (until
8586
// Split::remainder is stabilized #77998).
8687
fn from_str(s: &str) -> Result<Self, Self::Err> {
88+
fn error(msg: &str) -> &str {
89+
if cfg!(debug_assertions) {
90+
msg
91+
} else {
92+
"invalid map entry"
93+
}
94+
}
95+
96+
8797
let (range_str, s) = s.trim_start().split_once(' ').unwrap_or((s, ""));
8898
if range_str.is_empty() {
89-
return Err("Couldn't find address");
99+
return Err(error("Couldn't find address"));
90100
}
91101

92102
let (perms_str, s) = s.trim_start().split_once(' ').unwrap_or((s, ""));
93103
if perms_str.is_empty() {
94-
return Err("Couldn't find permissions");
104+
return Err(error("Couldn't find permissions"));
95105
}
96106

97107
let (offset_str, s) = s.trim_start().split_once(' ').unwrap_or((s, ""));
98108
if offset_str.is_empty() {
99-
return Err("Couldn't find offset");
109+
return Err(error("Couldn't find offset"));
100110
}
101111

102112
let (dev_str, s) = s.trim_start().split_once(' ').unwrap_or((s, ""));
103113
if dev_str.is_empty() {
104-
return Err("Couldn't find dev");
114+
return Err(error("Couldn't find dev"));
105115
}
106116

107117
let (inode_str, s) = s.trim_start().split_once(' ').unwrap_or((s, ""));
108118
if inode_str.is_empty() {
109-
return Err("Couldn't find inode");
119+
return Err(error("Couldn't find inode"));
110120
}
111121

112122
// Pathname may be omitted in which case it will be empty
113123
let pathname_str = s.trim_start();
114124

115-
let hex = |s| usize::from_str_radix(s, 16).map_err(|_| "Couldn't parse hex number");
116-
let hex64 = |s| u64::from_str_radix(s, 16).map_err(|_| "Couldn't parse hex number");
125+
let hex = |s| usize::from_str_radix(s, 16).map_err(|_| error("Couldn't parse hex number"));
126+
let hex64 = |s| u64::from_str_radix(s, 16).map_err(|_| error("Couldn't parse hex number"));
117127

118128
let address = if let Some((start, limit)) = range_str.split_once('-') {
119129
(hex(start)?, hex(limit)?)
120130
} else {
121-
return Err("Couldn't parse address range");
131+
return Err(error("Couldn't parse address range"));
122132
};
123133

124134
let offset = hex64(offset_str)?;

0 commit comments

Comments
 (0)