From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 05B931FF16B for ; Thu, 14 Nov 2024 15:16:10 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 3511F319C2; Thu, 14 Nov 2024 15:16:12 +0100 (CET) From: Christoph Heiss To: pve-devel@lists.proxmox.com Date: Thu, 14 Nov 2024 15:15:28 +0100 Message-ID: <20241114141533.694491-3-c.heiss@proxmox.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241114141533.694491-1-c.heiss@proxmox.com> References: <20241114141533.694491-1-c.heiss@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.028 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [main.rs] Subject: [pve-devel] [PATCH installer v3 2/2] assistant: avoid regex for simple prefix matching X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" udev properties are very easy to parse and can be done by doing a line-based scan and matching the prefix, splitting once for properties. Avoids the use of regexes and signicantly reduces binary size by about -46%(!). Tested by comparing the output of `proxmox-auto-install-assistant device-info`, running it before and after the changes. Stripped binary size for release builds: before: 3869304 bytes ~ 3.69MiB after: 2091608 bytes ~ 1.99MiB text data bss dec hex filename 3580692 280920 545 3862157 3aee8d assistant-before 2031252 52336 505 2084093 1fccfd assistant-after No functional changes. Reviewed-By: Aaron Lauterer Tested-By: Aaron Lauterer Signed-off-by: Christoph Heiss --- Changes v1 -> v2: * rebased on latest master Changes v2 -> v3: * rebased on latest master proxmox-auto-install-assistant/src/main.rs | 57 +++++++++------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/proxmox-auto-install-assistant/src/main.rs b/proxmox-auto-install-assistant/src/main.rs index da1ebda..bdcf067 100644 --- a/proxmox-auto-install-assistant/src/main.rs +++ b/proxmox-auto-install-assistant/src/main.rs @@ -1,7 +1,6 @@ use anyhow::{bail, format_err, Result}; use clap::{Args, Parser, Subcommand, ValueEnum}; use glob::Pattern; -use regex::Regex; use serde::Serialize; use std::{ collections::BTreeMap, @@ -454,13 +453,9 @@ fn get_disks() -> Result>> { Pattern::new("sr[0-9]*")?, ]; - // compile Regex here once and not inside the loop - let re_disk = Regex::new(r"(?m)^E: DEVTYPE=disk")?; - let re_cdrom = Regex::new(r"(?m)^E: ID_CDROM")?; - let re_iso9660 = Regex::new(r"(?m)^E: ID_FS_TYPE=iso9660")?; - - let re_name = Regex::new(r"(?m)^N: (.*)$")?; - let re_props = Regex::new(r"(?m)^E: ([^=]+)=(.*)$")?; + const PROP_DEVTYP_PREFIX: &str = "E: DEVTYPE="; + const PROP_CDROM: &str = "E: ID_CDROM"; + const PROP_ISO9660_FS: &str = "E: ID_FS_TYPE=iso9660"; let mut disks: BTreeMap> = BTreeMap::new(); @@ -482,30 +477,27 @@ fn get_disks() -> Result>> { } }; - if !re_disk.is_match(&output) { - continue 'outer; - }; - if re_cdrom.is_match(&output) { - continue 'outer; - }; - if re_iso9660.is_match(&output) { - continue 'outer; - }; - let mut name = filename; - if let Some(cap) = re_name.captures(&output) { - if let Some(res) = cap.get(1) { - name = String::from(res.as_str()); + let mut udev_props: BTreeMap = BTreeMap::new(); + for line in output.lines() { + if let Some(prop) = line.strip_prefix(PROP_DEVTYP_PREFIX) { + if prop != "disk" { + continue 'outer; + } } - } - let mut udev_props: BTreeMap = BTreeMap::new(); + if line.starts_with(PROP_CDROM) || line.starts_with(PROP_ISO9660_FS) { + continue 'outer; + } - for line in output.lines() { - if let Some(caps) = re_props.captures(line) { - let key = String::from(caps.get(1).unwrap().as_str()); - let value = String::from(caps.get(2).unwrap().as_str()); - udev_props.insert(key, value); + if let Some(prop) = line.strip_prefix("N: ") { + name = prop.to_owned(); + }; + + if let Some(prop) = line.strip_prefix("E: ") { + if let Some((key, val)) = prop.split_once('=') { + udev_props.insert(key.to_owned(), val.to_owned()); + } } } @@ -515,7 +507,6 @@ fn get_disks() -> Result>> { } fn get_nics() -> Result>> { - let re_props = Regex::new(r"(?m)^E: (.*)=(.*)$")?; let mut nics: BTreeMap> = BTreeMap::new(); let links = get_nic_list()?; @@ -533,10 +524,10 @@ fn get_nics() -> Result>> { let mut udev_props: BTreeMap = BTreeMap::new(); for line in output.lines() { - if let Some(caps) = re_props.captures(line) { - let key = String::from(caps.get(1).unwrap().as_str()); - let value = String::from(caps.get(2).unwrap().as_str()); - udev_props.insert(key, value); + if let Some(prop) = line.strip_prefix("E: ") { + if let Some((key, val)) = prop.split_once('=') { + udev_props.insert(key.to_owned(), val.to_owned()); + } } } -- 2.47.0 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel