[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <D8KBFC9M74H5.4ZJ2SJK06SGR@proton.me>
Date: Wed, 19 Mar 2025 14:34:13 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Tamir Duberstein <tamird@...il.com>
Cc: Andrew Ballance <andrewjballance@...il.com>, dakr@...nel.org, airlied@...il.com, simona@...ll.ch, maarten.lankhorst@...ux.intel.com, mripard@...nel.org, tzimmermann@...e.de, corbet@....net, ojeda@...nel.org, alex.gaynor@...il.com, boqun.feng@...il.com, gary@...yguo.net, bjorn3_gh@...tonmail.com, a.hindborg@...nel.org, aliceryhl@...gle.com, tmgross@...ch.edu, acourbot@...dia.com, nouveau@...ts.freedesktop.org, dri-devel@...ts.freedesktop.org, linux-doc@...r.kernel.org, linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org
Subject: Re: [PATCH v2 2/3] rust: alloc: add Vec::resize method
On Wed Mar 19, 2025 at 2:42 PM CET, Tamir Duberstein wrote:
> On Tue, Mar 18, 2025 at 8:50 PM Benno Lossin <benno.lossin@...ton.me> wrote:
>>
>> On Tue Mar 18, 2025 at 9:12 PM CET, Tamir Duberstein wrote:
>> > On Sun, Mar 16, 2025 at 7:17 AM Andrew Ballance
>> > <andrewjballance@...il.com> wrote:
>> >> + pub fn resize(&mut self, new_len: usize, value: T, flags: Flags) -> Result<(), AllocError> {
>> >> + if new_len > self.len() {
>> >> + self.extend_with(new_len - self.len(), value, flags)
>> >> + } else {
>> >> + self.truncate(new_len);
>> >> + Ok(())
>> >> + }
>> >> + }
>> >
>> > You can avoid underflow checking in debug builds by using `checked_sub`:
>>
>> `checked_sub` doesn't only avoid underflow in debug builds, but rather
>> in all builds. But the code below is a good suggestion.
>
> Yes, I know :)
>
> I included that language because the underflow check is likely
> optimized away in release builds.
If the function is inlined and the compiler can argue that `new_len >
self.len()`, then yes, but otherwise I'm pretty sure it won't be
optimized away.
Also if it is optimized away, then the check was still "executed", so I
find it a bit misleading to say "in debug builds" (making it sound like
it wouldn't do it in non-debug builds).
---
Cheers,
Benno
Powered by blists - more mailing lists