public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Onur <work@onurozkan.dev>
To: Alice Ryhl <aliceryhl@google.com>
Cc: rust-for-linux@vger.kernel.org, ojeda@kernel.org,
	alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, lossin@kernel.org,
	a.hindborg@kernel.org, tmgross@umich.edu, dakr@kernel.org,
	tamird@gmail.com, daniel@sedlak.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: less allocation in CString::try_from
Date: Mon, 15 Sep 2025 10:29:18 +0300	[thread overview]
Message-ID: <20250915102918.77adc04a@nimda.home> (raw)
In-Reply-To: <CAH5fLgi-VrhDSEjJA4Bg+5Wmr2JDAmmOc1FkfZAZ29SMqkX++w@mail.gmail.com>

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

  reply	other threads:[~2025-09-15  7:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2025-09-15 15:38 ` Daniel Almeida
2025-09-15 16:53   ` Onur Özkan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250915102918.77adc04a@nimda.home \
    --to=work@onurozkan.dev \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel@sedlak.dev \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox