Skip to content

Commit 237080d

Browse files
authored
perf(profiling): avoid copy of func name when utf8 (#3700)
This does pessimize it when it isn't utf8, but nearly all functions will be utf8.
1 parent 7d767af commit 237080d

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

profiling/src/profiling/stack_walking.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,18 @@ pub fn extract_function_name(func: &zend_function) -> Option<Cow<'static, str>>
9797

9898
buffer.extend_from_slice(method_name);
9999

100-
// When replacing the string to make it valid utf-8, it may get a bit
101-
// longer, but this usually doesn't happen. This limit is a soft-limit
102-
// at the moment anyway, so this is okay.
103-
let string = String::from_utf8_lossy(buffer.as_slice()).into_owned();
100+
// All or nearly all functions should be valid UTF8, so we're going to
101+
// pessimize the error case to avoid having to make copies on the happy
102+
// case.
103+
let string = if core::str::from_utf8(&buffer).is_ok() {
104+
// SAFETY: we just validate it's valid UTF-8.
105+
unsafe { String::from_utf8_unchecked(buffer) }
106+
} else {
107+
// When replacing the string to make it valid utf-8, it may get a bit
108+
// longer, but this usually doesn't happen. This limit is a soft-limit
109+
// at the moment anyway, so this is okay.
110+
String::from_utf8_lossy(&buffer).into_owned()
111+
};
104112
Some(Cow::Owned(string))
105113
}
106114

0 commit comments

Comments
 (0)