From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 122EA1FF0E4 for ; Tue, 11 Aug 2026 17:03:13 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 89B4F21579; Tue, 11 Aug 2026 17:03:10 +0200 (CEST) From: Robert Obkircher To: pbs-devel@lists.proxmox.com Subject: [PATCH v1 proxmox 2/6] io: request zeroed memory instead of manually clearing it Date: Tue, 11 Aug 2026 17:02:01 +0200 Message-ID: <20260811150237.527116-3-r.obkircher@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260811150237.527116-1-r.obkircher@proxmox.com> References: <20260811150237.527116-1-r.obkircher@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1786460573885 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.837 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: 36GR7GUZPMGAPHVOIVYO4VKI2PC5FMX4 X-Message-ID-Hash: 36GR7GUZPMGAPHVOIVYO4VKI2PC5FMX4 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: 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() } 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. -- 2.47.3