about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2024-10-13T10·19+0300
committerclbot <clbot@tvl.fyi>2024-10-13T14·31+0000
commit5faf7c9d7b5fcf8fb2795b5879aece45d7c62b21 (patch)
tree4ccc1959a7ee0047f4724ccd0ff7e60a7b166c9c
parentcb032b250e7b46ebc28038c8efb48f6a9c0ac75b (diff)
refactor(tvix/nix-compat): remove use of lazy_static r/8802
This is now supported in the standard library via std::sync::LazyLock, but
requires some manual shuffling around of code.

I found at least one dead variable along the way, which I deleted.

Change-Id: I8600c87c49078fb5ff72671994c77b919259e67b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12608
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Autosubmit: tazjin <tazjin@tvl.su>
-rw-r--r--tvix/Cargo.lock1
-rw-r--r--tvix/Cargo.nix4
-rw-r--r--tvix/nix-compat/Cargo.toml1
-rw-r--r--tvix/nix-compat/benches/narinfo_parse.rs27
-rw-r--r--tvix/nix-compat/src/derivation/parser.rs105
-rw-r--r--tvix/nix-compat/src/narinfo/mod.rs28
-rw-r--r--tvix/nix-compat/src/narinfo/signature.rs22
-rw-r--r--tvix/nix-compat/src/wire/bytes/reader/mod.rs7
-rw-r--r--tvix/nix-compat/src/wire/bytes/writer.rs7
9 files changed, 92 insertions, 110 deletions
diff --git a/tvix/Cargo.lock b/tvix/Cargo.lock
index badc48a2ca31..d6a0aee59495 100644
--- a/tvix/Cargo.lock
+++ b/tvix/Cargo.lock
@@ -2285,7 +2285,6 @@ dependencies = [
  "futures",
  "glob",
  "hex-literal",
- "lazy_static",
  "mimalloc",
  "nix-compat-derive",
  "nom",
diff --git a/tvix/Cargo.nix b/tvix/Cargo.nix
index deeb10171f29..d2cb0c4c514f 100644
--- a/tvix/Cargo.nix
+++ b/tvix/Cargo.nix
@@ -7254,10 +7254,6 @@ rec {
             packageId = "hex-literal";
           }
           {
-            name = "lazy_static";
-            packageId = "lazy_static";
-          }
-          {
             name = "mimalloc";
             packageId = "mimalloc";
           }
diff --git a/tvix/nix-compat/Cargo.toml b/tvix/nix-compat/Cargo.toml
index 58137e4de2e1..f430a5461829 100644
--- a/tvix/nix-compat/Cargo.toml
+++ b/tvix/nix-compat/Cargo.toml
@@ -41,7 +41,6 @@ optional = true
 criterion = { workspace = true, features = ["html_reports"] }
 futures = { workspace = true }
 hex-literal = { workspace = true }
-lazy_static = { workspace = true }
 mimalloc = { workspace = true }
 pretty_assertions = { workspace = true }
 rstest = { workspace = true }
diff --git a/tvix/nix-compat/benches/narinfo_parse.rs b/tvix/nix-compat/benches/narinfo_parse.rs
index f35ba8468a88..ee2665a310d3 100644
--- a/tvix/nix-compat/benches/narinfo_parse.rs
+++ b/tvix/nix-compat/benches/narinfo_parse.rs
@@ -1,8 +1,9 @@
+use std::sync::LazyLock;
+use std::{io, str};
+
 use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
-use lazy_static::lazy_static;
 use mimalloc::MiMalloc;
 use nix_compat::narinfo::NarInfo;
-use std::{io, str};
 
 #[global_allocator]
 static GLOBAL: MiMalloc = MiMalloc;
@@ -19,18 +20,16 @@ Deriver: 2ch8jx910qk6721mp4yqsmvdfgj5c8ir-banking-0.3.0.drv
 Sig: cache.nixos.org-1:xcL67rBZPcdVZudDLpLeddkBa0KaFTw5A0udnaa0axysjrQ6Nvd9p3BLZ4rhKgl52/cKiU3c6aq60L8+IcE5Dw==
 "#;
 
-lazy_static! {
-    static ref CASES: &'static [&'static str] = {
-        let data =
-            zstd::decode_all(io::Cursor::new(include_bytes!("../testdata/narinfo.zst"))).unwrap();
-        let data = str::from_utf8(Vec::leak(data)).unwrap();
-        Vec::leak(
-            data.split_inclusive("\n\n")
-                .map(|s| s.strip_suffix('\n').unwrap())
-                .collect::<Vec<_>>(),
-        )
-    };
-}
+static CASES: LazyLock<&'static [&'static str]> = LazyLock::new(|| {
+    let data =
+        zstd::decode_all(io::Cursor::new(include_bytes!("../testdata/narinfo.zst"))).unwrap();
+    let data = str::from_utf8(Vec::leak(data)).unwrap();
+    Vec::leak(
+        data.split_inclusive("\n\n")
+            .map(|s| s.strip_suffix('\n').unwrap())
+            .collect::<Vec<_>>(),
+    )
+});
 
 pub fn parse(c: &mut Criterion) {
     let mut g = c.benchmark_group("parse");
diff --git a/tvix/nix-compat/src/derivation/parser.rs b/tvix/nix-compat/src/derivation/parser.rs
index 4fff7181ba40..8e9804157da3 100644
--- a/tvix/nix-compat/src/derivation/parser.rs
+++ b/tvix/nix-compat/src/derivation/parser.rs
@@ -333,6 +333,7 @@ where
 mod tests {
     use crate::store_path::StorePathRef;
     use std::collections::{BTreeMap, BTreeSet};
+    use std::sync::LazyLock;
 
     use crate::{
         derivation::{
@@ -342,49 +343,48 @@ mod tests {
     };
     use bstr::{BString, ByteSlice};
     use hex_literal::hex;
-    use lazy_static::lazy_static;
     use rstest::rstest;
 
     const DIGEST_SHA256: [u8; 32] =
         hex!("a5ce9c155ed09397614646c9717fc7cd94b1023d7b76b618d409e4fefd6e9d39");
 
-    lazy_static! {
-        pub static ref NIXHASH_SHA256: NixHash = NixHash::Sha256(DIGEST_SHA256);
-        static ref EXP_MULTI_OUTPUTS: BTreeMap<String, Output> = {
-            let mut b = BTreeMap::new();
-            b.insert(
-                "lib".to_string(),
-                Output {
-                    path: Some(
-                        StorePath::from_bytes(
-                            b"2vixb94v0hy2xc6p7mbnxxcyc095yyia-has-multi-out-lib",
-                        )
-                        .unwrap(),
-                    ),
-                    ca_hash: None,
-                },
-            );
-            b.insert(
-                "out".to_string(),
-                Output {
-                    path: Some(
-                        StorePath::from_bytes(
-                            b"55lwldka5nyxa08wnvlizyqw02ihy8ic-has-multi-out".as_bytes(),
-                        )
+    static NIXHASH_SHA256: NixHash = NixHash::Sha256(DIGEST_SHA256);
+    static EXP_MULTI_OUTPUTS: LazyLock<BTreeMap<String, Output>> = LazyLock::new(|| {
+        let mut b = BTreeMap::new();
+        b.insert(
+            "lib".to_string(),
+            Output {
+                path: Some(
+                    StorePath::from_bytes(b"2vixb94v0hy2xc6p7mbnxxcyc095yyia-has-multi-out-lib")
                         .unwrap(),
-                    ),
-                    ca_hash: None,
-                },
-            );
-            b
-        };
-        static ref EXP_AB_MAP: BTreeMap<String, BString> = {
-            let mut b = BTreeMap::new();
-            b.insert("a".to_string(), b"1".into());
-            b.insert("b".to_string(), b"2".into());
-            b
-        };
-        static ref EXP_INPUT_DERIVATIONS_SIMPLE: BTreeMap<StorePath<String>, BTreeSet<String>> = {
+                ),
+                ca_hash: None,
+            },
+        );
+        b.insert(
+            "out".to_string(),
+            Output {
+                path: Some(
+                    StorePath::from_bytes(
+                        b"55lwldka5nyxa08wnvlizyqw02ihy8ic-has-multi-out".as_bytes(),
+                    )
+                    .unwrap(),
+                ),
+                ca_hash: None,
+            },
+        );
+        b
+    });
+
+    static EXP_AB_MAP: LazyLock<BTreeMap<String, BString>> = LazyLock::new(|| {
+        let mut b = BTreeMap::new();
+        b.insert("a".to_string(), b"1".into());
+        b.insert("b".to_string(), b"2".into());
+        b
+    });
+
+    static EXP_INPUT_DERIVATIONS_SIMPLE: LazyLock<BTreeMap<StorePath<String>, BTreeSet<String>>> =
+        LazyLock::new(|| {
             let mut b = BTreeMap::new();
             b.insert(
                 StorePath::from_bytes(b"8bjm87p310sb7r2r0sg4xrynlvg86j8k-hello-2.12.1.tar.gz.drv")
@@ -406,21 +406,22 @@ mod tests {
                 },
             );
             b
-        };
-        static ref EXP_INPUT_DERIVATIONS_SIMPLE_ATERM: String = {
-            format!(
-                "[(\"{0}\",[\"out\"]),(\"{1}\",[\"out\",\"lib\"])]",
-                "/nix/store/8bjm87p310sb7r2r0sg4xrynlvg86j8k-hello-2.12.1.tar.gz.drv",
-                "/nix/store/p3jc8aw45dza6h52v81j7lk69khckmcj-bash-5.2-p15.drv"
-            )
-        };
-        static ref EXP_INPUT_SOURCES_SIMPLE: BTreeSet<String> = {
-            let mut b = BTreeSet::new();
-            b.insert("/nix/store/55lwldka5nyxa08wnvlizyqw02ihy8ic-has-multi-out".to_string());
-            b.insert("/nix/store/2vixb94v0hy2xc6p7mbnxxcyc095yyia-has-multi-out-lib".to_string());
-            b
-        };
-    }
+        });
+
+    static EXP_INPUT_DERIVATIONS_SIMPLE_ATERM: LazyLock<String> = LazyLock::new(|| {
+        format!(
+            "[(\"{0}\",[\"out\"]),(\"{1}\",[\"out\",\"lib\"])]",
+            "/nix/store/8bjm87p310sb7r2r0sg4xrynlvg86j8k-hello-2.12.1.tar.gz.drv",
+            "/nix/store/p3jc8aw45dza6h52v81j7lk69khckmcj-bash-5.2-p15.drv"
+        )
+    });
+
+    static EXP_INPUT_SOURCES_SIMPLE: LazyLock<BTreeSet<String>> = LazyLock::new(|| {
+        let mut b = BTreeSet::new();
+        b.insert("/nix/store/55lwldka5nyxa08wnvlizyqw02ihy8ic-has-multi-out".to_string());
+        b.insert("/nix/store/2vixb94v0hy2xc6p7mbnxxcyc095yyia-has-multi-out-lib".to_string());
+        b
+    });
 
     /// Ensure parsing KVs works
     #[rstest]
diff --git a/tvix/nix-compat/src/narinfo/mod.rs b/tvix/nix-compat/src/narinfo/mod.rs
index 21aecf80b5a2..35146a927b39 100644
--- a/tvix/nix-compat/src/narinfo/mod.rs
+++ b/tvix/nix-compat/src/narinfo/mod.rs
@@ -417,8 +417,8 @@ const DUMMY_VERIFYING_KEY: &str =
 #[cfg(test)]
 mod test {
     use hex_literal::hex;
-    use lazy_static::lazy_static;
     use pretty_assertions::assert_eq;
+    use std::sync::LazyLock;
     use std::{io, str};
 
     use crate::{
@@ -428,20 +428,18 @@ mod test {
 
     use super::{Flags, NarInfo};
 
-    lazy_static! {
-        static ref CASES: &'static [&'static str] = {
-            let data = zstd::decode_all(io::Cursor::new(include_bytes!(
-                "../../testdata/narinfo.zst"
-            )))
-            .unwrap();
-            let data = str::from_utf8(Vec::leak(data)).unwrap();
-            Vec::leak(
-                data.split_inclusive("\n\n")
-                    .map(|s| s.strip_suffix('\n').unwrap())
-                    .collect::<Vec<_>>(),
-            )
-        };
-    }
+    static CASES: LazyLock<&'static [&'static str]> = LazyLock::new(|| {
+        let data = zstd::decode_all(io::Cursor::new(include_bytes!(
+            "../../testdata/narinfo.zst"
+        )))
+        .unwrap();
+        let data = str::from_utf8(Vec::leak(data)).unwrap();
+        Vec::leak(
+            data.split_inclusive("\n\n")
+                .map(|s| s.strip_suffix('\n').unwrap())
+                .collect::<Vec<_>>(),
+        )
+    });
 
     #[test]
     fn roundtrip() {
diff --git a/tvix/nix-compat/src/narinfo/signature.rs b/tvix/nix-compat/src/narinfo/signature.rs
index 37a7b0a30605..8df77019cd8f 100644
--- a/tvix/nix-compat/src/narinfo/signature.rs
+++ b/tvix/nix-compat/src/narinfo/signature.rs
@@ -150,32 +150,24 @@ mod test {
     use data_encoding::BASE64;
     use ed25519_dalek::VerifyingKey;
     use hex_literal::hex;
-    use lazy_static::lazy_static;
+    use std::sync::LazyLock;
 
     use super::Signature;
     use rstest::rstest;
 
     const FINGERPRINT: &str = "1;/nix/store/syd87l2rxw8cbsxmxl853h0r6pdwhwjr-curl-7.82.0-bin;sha256:1b4sb93wp679q4zx9k1ignby1yna3z7c4c2ri3wphylbc2dwsys0;196040;/nix/store/0jqd0rlxzra1rs38rdxl43yh6rxchgc6-curl-7.82.0,/nix/store/6w8g7njm4mck5dmjxws0z1xnrxvl81xa-glibc-2.34-115,/nix/store/j5jxw3iy7bbz4a57fh9g2xm2gxmyal8h-zlib-1.2.12,/nix/store/yxvjs9drzsphm9pcf42a4byzj1kb9m7k-openssl-1.1.1n";
 
-    // The signing key labelled as `cache.nixos.org-1`,
-    lazy_static! {
-        static ref PUB_CACHE_NIXOS_ORG_1: VerifyingKey = ed25519_dalek::VerifyingKey::from_bytes(
+    /// The signing key labelled as `cache.nixos.org-1`,
+    static PUB_CACHE_NIXOS_ORG_1: LazyLock<VerifyingKey> = LazyLock::new(|| {
+        ed25519_dalek::VerifyingKey::from_bytes(
             BASE64
                 .decode(b"6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=")
                 .unwrap()[..]
                 .try_into()
-                .unwrap()
+                .unwrap(),
         )
-        .unwrap();
-        static ref PUB_TEST_1: VerifyingKey = ed25519_dalek::VerifyingKey::from_bytes(
-            BASE64
-                .decode(b"tLAEn+EeaBUJYqEpTd2yeerr7Ic6+0vWe+aXL/vYUpE=")
-                .unwrap()[..]
-                .try_into()
-                .unwrap()
-        )
-        .unwrap();
-    }
+        .expect("embedded public key is valid")
+    });
 
     #[rstest]
     #[case::valid_cache_nixos_org_1(&PUB_CACHE_NIXOS_ORG_1, &"cache.nixos.org-1:TsTTb3WGTZKphvYdBHXwo6weVILmTytUjLB+vcX89fOjjRicCHmKA4RCPMVLkj6TMJ4GMX3HPVWRdD1hkeKZBQ==", FINGERPRINT, true)]
diff --git a/tvix/nix-compat/src/wire/bytes/reader/mod.rs b/tvix/nix-compat/src/wire/bytes/reader/mod.rs
index 77950496ed6b..a6209a6e6dad 100644
--- a/tvix/nix-compat/src/wire/bytes/reader/mod.rs
+++ b/tvix/nix-compat/src/wire/bytes/reader/mod.rs
@@ -299,11 +299,11 @@ fn with_limited<R>(buf: &mut ReadBuf, n: u64, f: impl FnOnce(&mut ReadBuf) -> R)
 
 #[cfg(test)]
 mod tests {
+    use std::sync::LazyLock;
     use std::time::Duration;
 
     use crate::wire::bytes::{padding_len, write_bytes};
     use hex_literal::hex;
-    use lazy_static::lazy_static;
     use rstest::rstest;
     use tokio::io::{AsyncReadExt, BufReader};
     use tokio_test::io::Builder;
@@ -314,9 +314,8 @@ mod tests {
     /// cases.
     const MAX_LEN: u64 = 1024;
 
-    lazy_static! {
-        pub static ref LARGE_PAYLOAD: Vec<u8> = (0..255).collect::<Vec<u8>>().repeat(4 * 1024);
-    }
+    pub static LARGE_PAYLOAD: LazyLock<Vec<u8>> =
+        LazyLock::new(|| (0..255).collect::<Vec<u8>>().repeat(4 * 1024));
 
     /// Helper function, calling the (simpler) write_bytes with the payload.
     /// We use this to create data we want to read from the wire.
diff --git a/tvix/nix-compat/src/wire/bytes/writer.rs b/tvix/nix-compat/src/wire/bytes/writer.rs
index f5632771e961..8b9b59aa1b85 100644
--- a/tvix/nix-compat/src/wire/bytes/writer.rs
+++ b/tvix/nix-compat/src/wire/bytes/writer.rs
@@ -232,19 +232,18 @@ where
 
 #[cfg(test)]
 mod tests {
+    use std::sync::LazyLock;
     use std::time::Duration;
 
     use crate::wire::bytes::write_bytes;
     use hex_literal::hex;
-    use lazy_static::lazy_static;
     use tokio::io::AsyncWriteExt;
     use tokio_test::{assert_err, assert_ok, io::Builder};
 
     use super::*;
 
-    lazy_static! {
-        pub static ref LARGE_PAYLOAD: Vec<u8> = (0..255).collect::<Vec<u8>>().repeat(4 * 1024);
-    }
+    pub static LARGE_PAYLOAD: LazyLock<Vec<u8>> =
+        LazyLock::new(|| (0..255).collect::<Vec<u8>>().repeat(4 * 1024));
 
     /// Helper function, calling the (simpler) write_bytes with the payload.
     /// We use this to create data we want to see on the wire.