rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: less allocation in CString::try_from
@ 2025-09-15  6:54 Onur Özkan
  2025-09-15  7:26 ` Alice Ryhl
  2025-09-15 15:38 ` Daniel Almeida
  0 siblings, 2 replies; 5+ messages in thread
From: Onur Özkan @ 2025-09-15  6:54 UTC (permalink / raw)
  To: rust-for-linux
  Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, dakr, tamird, daniel,
	linux-kernel, 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`.

Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
 rust/kernel/str.rs | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 6c892550c0ba..98d41d995e45 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: less allocation in CString::try_from
  2025-09-15  6:54 [PATCH] rust: less allocation in CString::try_from Onur Özkan
@ 2025-09-15  7:26 ` Alice Ryhl
  2025-09-15  7:29   ` Onur
  2025-09-15 15:38 ` Daniel Almeida
  1 sibling, 1 reply; 5+ messages in thread
From: Alice Ryhl @ 2025-09-15  7:26 UTC (permalink / raw)
  To: Onur Özkan
  Cc: rust-for-linux, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, tmgross, dakr, tamird, daniel, linux-kernel

On Mon, Sep 15, 2025 at 8:54 AM Onur Özkan <work@onurozkan.dev> wrote:
>
> 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`.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>
> ---
>  rust/kernel/str.rs | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index 6c892550c0ba..98d41d995e45 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: less allocation in CString::try_from
  2025-09-15  7:26 ` Alice Ryhl
@ 2025-09-15  7:29   ` Onur
  0 siblings, 0 replies; 5+ messages in thread
From: Onur @ 2025-09-15  7:29 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: rust-for-linux, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, tmgross, dakr, tamird, daniel, linux-kernel

On Mon, 15 Sep 2025 09:26:00 +0200
Alice Ryhl <aliceryhl@google.com> wrote:

> On Mon, Sep 15, 2025 at 8:54 AM Onur Özkan <work@onurozkan.dev> wrote:
> >
> > 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`.
> >
> > Signed-off-by: Onur Özkan <work@onurozkan.dev>
> > ---
> >  rust/kernel/str.rs | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> > index 6c892550c0ba..98d41d995e45 100644
> > --- a/rust/kernel/str.rs
> > +++ b/rust/kernel/str.rs
> > @@ -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

Good to know, thanks!

Onur

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: less allocation in CString::try_from
  2025-09-15  6:54 [PATCH] rust: less allocation in CString::try_from Onur Özkan
  2025-09-15  7:26 ` Alice Ryhl
@ 2025-09-15 15:38 ` Daniel Almeida
  2025-09-15 16:53   ` Onur Özkan
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Almeida @ 2025-09-15 15:38 UTC (permalink / raw)
  To: Onur Özkan
  Cc: rust-for-linux, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross, dakr, tamird, daniel,
	linux-kernel

Hi Onur, thanks for working on this :)

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

— Daniel



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: less allocation in CString::try_from
  2025-09-15 15:38 ` Daniel Almeida
@ 2025-09-15 16:53   ` Onur Özkan
  0 siblings, 0 replies; 5+ messages in thread
From: Onur Özkan @ 2025-09-15 16:53 UTC (permalink / raw)
  To: Daniel Almeida
  Cc: rust-for-linux, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross, dakr, tamird, daniel,
	linux-kernel

On Mon, 15 Sep 2025 17:38:32 +0200
Daniel Almeida <daniel.almeida@collabora.com> wrote:

> 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-09-15 16:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-15  6:54 [PATCH] rust: less allocation in CString::try_from Onur Özkan
2025-09-15  7:26 ` Alice Ryhl
2025-09-15  7:29   ` Onur
2025-09-15 15:38 ` Daniel Almeida
2025-09-15 16:53   ` Onur Özkan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).