From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer 4/4] assistant: avoid regex for simple prefix matching
Date: Tue, 7 May 2024 15:21:37 +0200 [thread overview]
Message-ID: <20240507132220.1218652-5-c.heiss@proxmox.com> (raw)
In-Reply-To: <20240507132220.1218652-1-c.heiss@proxmox.com>
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
-38%(!).
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: 3103104 bytes ~ 2.96MiB
after: 1935744 bytes ~ 1.85MiB
text data bss dec hex filename
2906765 187144 537 3094446 2f37ae proxmox-auto-install-assistant-before
1871090 55736 497 1927323 1d689b proxmox-auto-install-assistant-after
No functional changes.
Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
proxmox-auto-install-assistant/Cargo.toml | 1 -
proxmox-auto-install-assistant/src/main.rs | 57 +++++++++-------------
2 files changed, 24 insertions(+), 34 deletions(-)
diff --git a/proxmox-auto-install-assistant/Cargo.toml b/proxmox-auto-install-assistant/Cargo.toml
index 0286c80..766b445 100644
--- a/proxmox-auto-install-assistant/Cargo.toml
+++ b/proxmox-auto-install-assistant/Cargo.toml
@@ -15,7 +15,6 @@ anyhow = "1.0"
clap = { version = "4.0", features = ["derive"] }
glob = "0.3"
proxmox-auto-installer = { path = "../proxmox-auto-installer" }
-regex = "1.7"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
toml = "0.7"
diff --git a/proxmox-auto-install-assistant/src/main.rs b/proxmox-auto-install-assistant/src/main.rs
index 906f144..828ba76 100644
--- a/proxmox-auto-install-assistant/src/main.rs
+++ b/proxmox-auto-install-assistant/src/main.rs
@@ -1,7 +1,6 @@
use anyhow::{bail, Result};
use clap::{Args, Parser, Subcommand, ValueEnum};
use glob::Pattern;
-use regex::Regex;
use serde::Serialize;
use std::{
collections::BTreeMap,
@@ -401,13 +400,9 @@ fn get_disks() -> Result<BTreeMap<String, BTreeMap<String, String>>> {
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<String, BTreeMap<String, String>> = BTreeMap::new();
@@ -429,30 +424,27 @@ fn get_disks() -> Result<BTreeMap<String, BTreeMap<String, String>>> {
}
};
- 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<String, String> = 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<String, String> = 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());
+ }
}
}
@@ -462,7 +454,6 @@ fn get_disks() -> Result<BTreeMap<String, BTreeMap<String, String>>> {
}
fn get_nics() -> Result<BTreeMap<String, BTreeMap<String, String>>> {
- let re_props = Regex::new(r"(?m)^E: (.*)=(.*)$")?;
let mut nics: BTreeMap<String, BTreeMap<String, String>> = BTreeMap::new();
let links = get_nic_list()?;
@@ -480,10 +471,10 @@ fn get_nics() -> Result<BTreeMap<String, BTreeMap<String, String>>> {
let mut udev_props: BTreeMap<String, String> = 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.44.0
_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
next prev parent reply other threads:[~2024-05-07 13:22 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-07 13:21 [pve-devel] [PATCH installer 0/4] assistant: clean up glob patterns & regexes Christoph Heiss
2024-05-07 13:21 ` [pve-devel] [PATCH installer 1/4] tree-wide: run rustfmt, fix clippy warnings Christoph Heiss
2024-05-07 13:21 ` [pve-devel] [PATCH installer 2/4] assistant: drop unused `log` dependency Christoph Heiss
2024-05-07 13:21 ` [pve-devel] [PATCH installer 3/4] assistant: pre-compile ignored block device patterns Christoph Heiss
2024-05-07 13:21 ` Christoph Heiss [this message]
2024-05-13 9:18 ` [pve-devel] [PATCH installer 0/4] assistant: clean up glob patterns & regexes Aaron Lauterer
2024-05-13 9:50 ` Christoph Heiss
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=20240507132220.1218652-5-c.heiss@proxmox.com \
--to=c.heiss@proxmox.com \
--cc=pve-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.