From: Robert Obkircher <r.obkircher@proxmox.com>
To: Proxmox Backup Server development discussion
<pbs-devel@lists.proxmox.com>
Subject: Re: [PATCH v2 proxmox 2/6] io: request zeroed memory instead of manually clearing it
Date: Tue, 29 Sep 2026 16:35:14 +0200 [thread overview]
Message-ID: <0e1e495e-8dac-403f-9752-466857ca16bd@proxmox.com> (raw)
In-Reply-To: <20260812082549.21561-3-r.obkircher@proxmox.com>
On 12.08.26 10:25, Robert Obkircher wrote:
> A quick comparison using cargo bench showed no notable differences in
> release mode, but in debug mode 1 GiB allocations by themselves got
> 43879 times faster. This is because the standard library continued to
> call alloc_zeroed, matching release mode, while the previous version
> actually had to touch the memory. With an additional .fill(1) the
> speedup shrank to around 1.09x.
>
> Signed-off-by: Robert Obkircher <r.obkircher@proxmox.com>
> ---
> proxmox-io/src/boxed.rs | 7 ++-----
> proxmox-io/src/vec/mod.rs | 6 +-----
> 2 files changed, 3 insertions(+), 10 deletions(-)
>
> diff --git a/proxmox-io/src/boxed.rs b/proxmox-io/src/boxed.rs
> index 7af51cfc..fb67bfd7 100644
> --- a/proxmox-io/src/boxed.rs
> +++ b/proxmox-io/src/boxed.rs
> @@ -12,9 +12,6 @@ pub fn uninitialized(len: usize) -> Box<[u8]> {
>
> /// Zero-initialized bytes, meant for large sizes to avoid busting the stack.
> pub fn zeroed(len: usize) -> Box<[u8]> {
> - let mut bytes = uninitialized(len);
> - unsafe {
> - std::ptr::write_bytes(bytes.as_mut_ptr(), 0, bytes.len());
> - }
> - bytes
> + // vec! guarantees not to over-allocate, so shrink_to_fit inside into_boxed_slice does nothing
> + vec![0; len].into_boxed_slice()
The comment is wrong about the over-allocation, but a sane allocator
wouldn't hand out a larger allocation if it could shrink it immediately.
I'm not sure if using Box::new_zeroed() with unsafe is worth it,
because the callers could easily switch to Vec.
> }
> diff --git a/proxmox-io/src/vec/mod.rs b/proxmox-io/src/vec/mod.rs
> index b68fc750..7fe106e8 100644
> --- a/proxmox-io/src/vec/mod.rs
> +++ b/proxmox-io/src/vec/mod.rs
> @@ -69,11 +69,7 @@ pub fn clear(data: &mut [u8]) {
> /// Create a newly allocated, zero initialized byte vector.
> #[inline]
> pub fn zeroed(len: usize) -> Vec<u8> {
> - unsafe {
> - let mut out = uninitialized(len);
> - clear(&mut out);
> - out
> - }
> + vec![0; len]
> }
>
> /// Create a newly allocated byte vector of a specific size with "undefined" content.
next prev parent reply other threads:[~2026-09-29 14:36 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-12 8:23 [PATCH v2 proxmox 0/6] uninitialized memory allocations fixes Robert Obkircher
2026-08-12 8:23 ` [PATCH v2 proxmox 1/6] uuid: avoid potential null dereference and memory leaks Robert Obkircher
2026-08-12 8:23 ` [PATCH v2 proxmox 2/6] io: request zeroed memory instead of manually clearing it Robert Obkircher
2026-09-29 14:35 ` Robert Obkircher [this message]
2026-08-12 8:23 ` [PATCH v2 proxmox 3/6] io: avoid potential null dereference and memory leak on error path Robert Obkircher
2026-08-12 8:23 ` [PATCH v2 proxmox 4/6] io: remove boxed::uninitialized because it is unsound Robert Obkircher
2026-08-12 8:23 ` [PATCH v2 proxmox 5/6] io: remove unused append_to_vec functions Robert Obkircher
2026-08-12 8:23 ` [PATCH v2 proxmox 6/6] io: remove unused ByteVecExt trait with grow_ and resize_uninitialized Robert Obkircher
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=0e1e495e-8dac-403f-9752-466857ca16bd@proxmox.com \
--to=r.obkircher@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.