From be949fb12298d203edfa9679dab5a625e788d203 Mon Sep 17 00:00:00 2001 From: Sander Date: Thu, 20 Mar 2025 19:40:14 +0000 Subject: [PATCH] refactor(snix/glue): reduce string allocations in the flake ref parser Change-Id: Id6818976b651b6d6ffa51cf7e2066cce0cb01055 Reviewed-on: https://cl.snix.dev/c/snix/+/30203 Reviewed-by: Florian Klink Autosubmit: Sander Melnikov Tested-by: besadii --- snix/eval/src/value/string/mod.rs | 5 +++++ snix/glue/src/builtins/fetchers.rs | 24 +++++++++++------------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/snix/eval/src/value/string/mod.rs b/snix/eval/src/value/string/mod.rs index 4419da777..003b2bf2e 100644 --- a/snix/eval/src/value/string/mod.rs +++ b/snix/eval/src/value/string/mod.rs @@ -633,6 +633,11 @@ impl NixString { unsafe { NixStringInner::data_slice(self.0) } } + /// Returns a &str if the NixString is a valid UTF-8 string. + pub fn as_str(&self) -> Result<&str, std::str::Utf8Error> { + std::str::from_utf8(self.as_bytes()) + } + pub fn into_bstring(self) -> BString { self.as_bstr().to_owned() } diff --git a/snix/glue/src/builtins/fetchers.rs b/snix/glue/src/builtins/fetchers.rs index 613618d52..8db10a462 100644 --- a/snix/glue/src/builtins/fetchers.rs +++ b/snix/glue/src/builtins/fetchers.rs @@ -202,20 +202,18 @@ pub(crate) mod fetcher_builtins { co: GenCo, value: Value, ) -> Result { - let flake_ref_str = value.to_str()?.into_bstring().as_bstr().to_string(); + let flake_ref = value.to_str()?; + let flake_ref_str = flake_ref.to_str()?; - let fetch_args = match flake_ref_str.parse() { - Ok(args) => args, - Err(err) => { - return Err(ErrorKind::SnixError(Rc::new(err))); - } - }; + let fetch_args = flake_ref_str + .parse() + .map_err(|err| ErrorKind::SnixError(Rc::new(err)))?; // Convert the FlakeRef to our Value format let mut attrs = BTreeMap::new(); // Extract type and url based on the variant - match &fetch_args { + match fetch_args { flakeref::FlakeRef::Git { url, .. } => { attrs.insert("type".into(), Value::from("git")); attrs.insert("url".into(), Value::from(url.to_string())); @@ -224,16 +222,16 @@ pub(crate) mod fetcher_builtins { owner, repo, r#ref, .. } => { attrs.insert("type".into(), Value::from("github")); - attrs.insert("owner".into(), Value::from(owner.clone())); - attrs.insert("repo".into(), Value::from(repo.clone())); + attrs.insert("owner".into(), Value::from(owner)); + attrs.insert("repo".into(), Value::from(repo)); if let Some(ref_name) = r#ref { - attrs.insert("ref".into(), Value::from(ref_name.clone())); + attrs.insert("ref".into(), Value::from(ref_name)); } } flakeref::FlakeRef::GitLab { owner, repo, .. } => { attrs.insert("type".into(), Value::from("gitlab")); - attrs.insert("owner".into(), Value::from(owner.clone())); - attrs.insert("repo".into(), Value::from(repo.clone())); + attrs.insert("owner".into(), Value::from(owner)); + attrs.insert("repo".into(), Value::from(repo)); } flakeref::FlakeRef::File { url, .. } => { attrs.insert("type".into(), Value::from("file"));