From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 4E55EB9A3 for ; Thu, 7 Apr 2022 17:05:44 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 3CBA16B9C for ; Thu, 7 Apr 2022 17:05:14 +0200 (CEST) Received: from kerio.tuxis.nl (alrami.saas.tuxis.net [31.3.111.57]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 4FE746B90 for ; Thu, 7 Apr 2022 17:05:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=tuxis.nl; s=mail; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=PlX1c7LziFz3A6B0U1kqS1CPqyxrIt0VAD9Llv2EiCI=; b=LcUmnFq+3/ZDOEXW7rvV66fk4Dmlz4LWiziiIQ7RoLDPZb7CJW4JhmKJvFB1GwMB+j9TQvzZIo39i k67Px3c+VlrPIz2linfLFJtOjNYK9+88N4npY7Hp1er2i9s/fhJo/A03bbg3q04RCWFDjcUeAnGH5N CQofKeMR1dKrTcJyEvPuuU3RgBmEt5jOpYU+GVaahZTTduS2eJ7O7nejGVPWwx4wWtTi9SyQ9fwLZE OSF7hhvz4lyr1i/xWczCb06NYTL2Y7hLbzBIWy3DQtYgfH+TLPWJX0pGKWW2jUBO8LtOWGUL8yQ2YM N7HbBDcB+AEJnelli28uqk9iO/1bvog== X-Footer: dHV4aXMubmw= Received: from smtpclient.apple ([2a03:7900:64::1001]) (authenticated user mark@tuxis.nl) by kerio.tuxis.nl (Kerio Connect 9.4.1) with ESMTPSA (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)); Thu, 7 Apr 2022 17:05:05 +0200 Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.80.82.1.1\)) From: Mark Schouten In-Reply-To: <20220406125112.37327-1-w.bumiller@proxmox.com> Date: Thu, 7 Apr 2022 17:04:53 +0200 Cc: pbs-devel Content-Transfer-Encoding: quoted-printable Message-Id: References: <20220406125112.37327-1-w.bumiller@proxmox.com> To: Wolfgang Bumiller X-Mailer: Apple Mail (2.3696.80.82.1.1) X-SPAM-LEVEL: Spam detection results: 0 AWL 0.031 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [tfa.rs, proxmox.com, tuxis.nl] Subject: Re: [pbs-devel] [PATCH backup] use BufReader/Writer for Files passed to serde_json::from_reader/writer X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Apr 2022 15:05:44 -0000 Hi Wolfgang, Thanks for the quick response (even though I somehow missed it). = Reported-by is fine with me! =E2=80=94=20 Mark Schouten, CTO Tuxis B.V. mark@tuxis.nl > On 6 Apr 2022, at 14:51, Wolfgang Bumiller = wrote: >=20 > As serde_json will otherwise read files 1 byte at a time. > Writing is a bit better, but syntacitcal elements (quotes, braces, > commas) still often show up as single write syscalls, so use BufWriter > there as well. >=20 > Note that while we do store the file in the resulting objects, we do = not > need to keep the buffered read/writers as we always `seek` to the > beginning on further file operations. >=20 > Reported-by: Mark Schouten > Link: = https://lists.proxmox.com/pipermail/pbs-devel/2022-April/004909.html > Signed-off-by: Wolfgang Bumiller > --- > @Mark: Hope the Reported-by tag is fine with you. >=20 > proxmox-file-restore/src/block_driver_qemu.rs | 8 ++++---- > src/config/tfa.rs | 6 +++--- > 2 files changed, 7 insertions(+), 7 deletions(-) >=20 > diff --git a/proxmox-file-restore/src/block_driver_qemu.rs = b/proxmox-file-restore/src/block_driver_qemu.rs > index 0176f7f1..ac61f917 100644 > --- a/proxmox-file-restore/src/block_driver_qemu.rs > +++ b/proxmox-file-restore/src/block_driver_qemu.rs > @@ -1,7 +1,7 @@ > //! Block file access via a small QEMU restore VM using the PBS block = driver in QEMU > use std::collections::HashMap; > use std::fs::{File, OpenOptions}; > -use std::io::{prelude::*, SeekFrom}; > +use std::io::{prelude::*, BufReader, BufWriter, SeekFrom}; >=20 > use anyhow::{bail, Error}; > use futures::FutureExt; > @@ -51,7 +51,7 @@ impl VMStateMap { > fn load() -> Result { > let mut file =3D Self::open_file_raw(true)?; > lock_file(&mut file, true, = Some(std::time::Duration::from_secs(120)))?; > - let map =3D = serde_json::from_reader(&file).unwrap_or_default(); > + let map =3D serde_json::from_reader(BufReader::new(&mut = file)).unwrap_or_default(); > Ok(Self { map, file }) > } >=20 > @@ -59,14 +59,14 @@ impl VMStateMap { > /// shell auto-completion, for anything requiring consistency use = load() ! > fn load_read_only() -> Result, Error> { > let file =3D Self::open_file_raw(false)?; > - Ok(serde_json::from_reader(&file).unwrap_or_default()) > + = Ok(serde_json::from_reader(BufReader::new(file)).unwrap_or_default()) > } >=20 > /// Write back a potentially modified state map, consuming the = held lock > fn write(mut self) -> Result<(), Error> { > self.file.seek(SeekFrom::Start(0))?; > self.file.set_len(0)?; > - serde_json::to_writer(self.file, &self.map)?; > + serde_json::to_writer(BufWriter::new(&mut self.file), = &self.map)?; >=20 > // drop ourselves including file lock > Ok(()) > diff --git a/src/config/tfa.rs b/src/config/tfa.rs > index 790e0960..7120553c 100644 > --- a/src/config/tfa.rs > +++ b/src/config/tfa.rs > @@ -40,7 +40,7 @@ pub fn read() -> Result { > Err(err) =3D> return Err(err.into()), > }; >=20 > - Ok(serde_json::from_reader(file)?) > + Ok(serde_json::from_reader(io::BufReader::new(file))?) > } >=20 > pub(crate) fn webauthn_config_digest(config: &WebauthnConfig) -> = Result<[u8; 32], Error> { > @@ -116,7 +116,7 @@ impl TfaUserChallengeData { > fn save(mut self) -> Result<(), Error> { > self.rewind()?; >=20 > - serde_json::to_writer(&mut &self.lock, = &self.inner).map_err(|err| { > + serde_json::to_writer(io::BufWriter::new(&mut &self.lock), = &self.inner).map_err(|err| { > format_err!("failed to update challenge file {:?}: {}", = self.path, err) > })?; >=20 > @@ -293,7 +293,7 @@ impl proxmox_tfa::api::OpenUserChallengeData for = UserAccess { >=20 > proxmox_sys::fs::lock_file(&mut file, true, None)?; >=20 > - let inner =3D serde_json::from_reader(&mut = file).map_err(|err| { > + let inner =3D serde_json::from_reader(io::BufReader::new(&mut = file)).map_err(|err| { > format_err!("failed to read challenge data for user {}: = {}", userid, err) > })?; >=20 > --=20 > 2.30.2 >=20 >=20