From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 680491FF0AE for ; Tue, 29 Sep 2026 16:36:36 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 65DCD214F6; Tue, 29 Sep 2026 16:36:34 +0200 (CEST) Message-ID: <0e1e495e-8dac-403f-9752-466857ca16bd@proxmox.com> Date: Tue, 29 Sep 2026 16:35:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 proxmox 2/6] io: request zeroed memory instead of manually clearing it To: Proxmox Backup Server development discussion References: <20260812082549.21561-1-r.obkircher@proxmox.com> <20260812082549.21561-3-r.obkircher@proxmox.com> Content-Language: en-US, de-AT From: Robert Obkircher In-Reply-To: <20260812082549.21561-3-r.obkircher@proxmox.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1790692514411 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.519 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 24BA23OM6G2BAJDO4REW4C55CENR2DDV X-Message-ID-Hash: 24BA23OM6G2BAJDO4REW4C55CENR2DDV X-MailFrom: r.obkircher@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Backup Server development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 > --- > 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 { > - 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.