Skip to contentSkip to conversation

rust: less allocation in CString::try_from

PATCH In review Onur Özkan 3 participants 5 messages Sep 15, 2025 Rust

lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

PATCH 1/1 rust: less allocation in CString::try_from

Allocates buffer with the correct capacity upfront by using the length of the CStr to avoid extra and unnecessary re-allocation when converting from CStr to CString.

---
 rust/kernel/str.rs | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
rust/kernel/str.rs +3 -2
index 6c892550c0ba..98d41d995e45 100644
@@ -946,9 +946,10 @@ impl<'a> TryFrom<&'a CStr> for CString {
     type Error = AllocError;
 
     fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
-        let mut buf = KVec::new();
+        let bytes = cstr.to_bytes_with_nul();
 
-        buf.extend_from_slice(cstr.to_bytes_with_nul(), GFP_KERNEL)?;
+        let mut buf = KVec::with_capacity(bytes.len(), GFP_KERNEL)?;
+        buf.extend_from_slice(bytes, GFP_KERNEL)?;
 
         // INVARIANT: The `CStr` and `CString` types have the same invariants for
         // the string data, and we copied it over without changes.

--
2.51.0

reviewer
#1 lore raw
Quoted 15 lines · Onur Özkan

Allocates buffer with the correct capacity upfront by using the length of the CStr to avoid extra and unnecessary re-allocation when converting from CStr to CString.

---
 rust/kernel/str.rs | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
rust/kernel/str.rs +0 -0
index 6c892550c0ba..98d41d995e45 100644

rust/kernel/str.rs impl<'a> TryFrom<&'a CStr> for CString { · line 952

Quoted 9 lines
rust/kernel/str.rs +3 -2
@@ -946,9 +946,10 @@ impl<'a> TryFrom<&'a CStr> for CString {
     type Error = AllocError;
 
     fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
-        let mut buf = KVec::new();
+        let bytes = cstr.to_bytes_with_nul();
 
-        buf.extend_from_slice(cstr.to_bytes_with_nul(), GFP_KERNEL)?;
+        let mut buf = KVec::with_capacity(bytes.len(), GFP_KERNEL)?;
+        buf.extend_from_slice(bytes, GFP_KERNEL)?;

I don't think this changes the number of allocations. KVec::new() does not allocate.

Alice

author reviewer
#2 lore raw
Quoted 31 lines · On Mon, 15 Sep 2025 09:26:00 +0200 Alice Ryhl <[email protected]> wrote:
Quoted 16 lines · Onur Özkan

Allocates buffer with the correct capacity upfront by using the length of the CStr to avoid extra and unnecessary re-allocation when converting from CStr to CString.

---
 rust/kernel/str.rs | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
rust/kernel/str.rs +0 -0
index 6c892550c0ba..98d41d995e45 100644
@@ -946,9 +946,10 @@ impl<'a> TryFrom<&'a CStr> for CString {
     type Error = AllocError;

rust/kernel/str.rs impl<'a> TryFrom<&'a CStr> for CString { · line 954

Quoted 10 lines
rust/kernel/str.rs +3 -2
 
     fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
-        let mut buf = KVec::new();
+        let bytes = cstr.to_bytes_with_nul();
 
-        buf.extend_from_slice(cstr.to_bytes_with_nul(),
 GFP_KERNEL)?;
+        let mut buf = KVec::with_capacity(bytes.len(),
 GFP_KERNEL)?;
+        buf.extend_from_slice(bytes, GFP_KERNEL)?;

I don't think this changes the number of allocations. KVec::new() does not allocate.

Quoted 1 line

Alice

Good to know, thanks!

Onur

author reviewer
#4 lore raw
Quoted 7 lines · On Mon, 15 Sep 2025 17:38:32 +0200 Daniel Almeida…

Hi Onur, thanks for working on this :)

The commit title doesn’t parse very well, can you rework it?

— Daniel

Hi Daniel,

As Alice mentioned in [0], my assumption about reducing allocations was wrong. Looking at the KVec implementation details this patch doesn't actually change anything other than how the code looks. We can safely ignore it. I should have checked that, sorry.

[0]:
https://lore.kernel.org/all/CAH5fLgi-VrhDSEjJA4Bg+5Wmr2JDAmmOc1FkfZAZ29SMqkX++w@mail.gmail.com/

Thanks,
Onur