Skip to content

Commit 550d10f

Browse files
authored
feat: remove uuid crate dependency
`uuid` pulls in a bunch of WASM dependencies, even when Codspeed is pulled in with `default-features = false`. For some projects, that means vetting a lot of extra code. This PR replaces the `uuid` dependency and uses `getrandom` to generate an UUIDv4-style string. (I'm not sure if anything depends on things being named with UUIDs; if not, this PR would be even simpler.)
1 parent 6c92e06 commit 550d10f

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

Cargo.lock

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cargo-codspeed/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ codspeed = { path = "../codspeed", version = "=4.0.5" }
3434
assert_cmd = "2.0.15"
3535
fs_extra = "1.3.0"
3636
predicates = "3.1.2"
37-
uuid = { version = "1.10.0", features = ["v4"] }
3837

3938
[features]
4039
vendored-openssl = []

crates/cargo-codspeed/tests/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use assert_cmd::Command;
77
use fs_extra::dir::copy;
88
use fs_extra::dir::create;
99
use fs_extra::dir::remove;
10-
use uuid::Uuid;
1110

1211
fn replace_in_file(path: &str, from: &str, to: &str) {
1312
let mut contents = std::fs::read_to_string(path).unwrap();
@@ -27,7 +26,8 @@ pub enum Project {
2726

2827
pub fn setup(dir: &str, project: Project) -> String {
2928
//Create a new unique named temp directory
30-
let tmp_dir = temp_dir().join(format!("cargo-codspeed-test-{}", Uuid::new_v4()));
29+
let unique_id = codspeed::utils::generate_unique_id();
30+
let tmp_dir = temp_dir().join(format!("cargo-codspeed-test-{unique_id}"));
3131
create(&tmp_dir, false).unwrap();
3232
let mut copy_opts = fs_extra::dir::CopyOptions::new();
3333
copy_opts.content_only = true;

crates/codspeed/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ colored = "2.0.0"
2323
glob = "0.3.2"
2424
libc = "^0.2"
2525
nix = { version = "0.30.1", features = ["time"] }
26+
getrandom = "0.2"
2627
serde = { workspace = true }
2728
serde_json = { workspace = true }
2829
statrs = { version = "0.18.0", default-features = false }
29-
uuid = { version = "1.12.1", features = ["v4"] }
3030

3131
[[bench]]
3232
name = "native"

crates/codspeed/src/utils.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ pub fn is_perf_enabled() -> bool {
4646
std::env::var("CODSPEED_PERF_ENABLED").is_ok()
4747
}
4848

49+
/// Generate a statistically unique ID in a format resembling UUID v4.
50+
pub fn generate_unique_id() -> String {
51+
// Generate random bytes for UUID v4
52+
let mut bytes = [0u8; 16];
53+
getrandom::getrandom(&mut bytes).expect("Failed to generate random bytes");
54+
55+
// Extract values from bytes
56+
let r1 = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
57+
let r2 = u16::from_be_bytes([bytes[4], bytes[5]]);
58+
let r3 = u16::from_be_bytes([bytes[6], bytes[7]]);
59+
let r4 = u16::from_be_bytes([bytes[8], bytes[9]]);
60+
let r5 = u32::from_be_bytes([bytes[10], bytes[11], bytes[12], bytes[13]]);
61+
let r6 = u16::from_be_bytes([bytes[14], bytes[15]]);
62+
63+
// Set version (4) and variant bits according to UUID v4 spec
64+
let r3_v4 = (r3 & 0x0fff) | 0x4000; // Version 4
65+
let r4_variant = (r4 & 0x3fff) | 0x8000; // Variant 10
66+
67+
// Format as standard UUID: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
68+
// where y is one of 8, 9, A, or B
69+
format!("{r1:08x}-{r2:04x}-{r3_v4:04x}-{r4_variant:04x}-{r5:08x}{r6:04x}")
70+
}
71+
4972
#[cfg(test)]
5073
mod tests {
5174
use super::*;

crates/codspeed/src/walltime_results.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::{
77
use serde::{Deserialize, Serialize};
88
use statrs::statistics::{Data, Distribution, Max, Min, OrderStatistics};
99

10+
use crate::utils::generate_unique_id;
11+
1012
const IQR_OUTLIER_FACTOR: f64 = 1.5;
1113
const STDEV_OUTLIER_FACTOR: f64 = 3.0;
1214

@@ -191,7 +193,7 @@ impl WalltimeBenchmark {
191193
fn dump_to_results(&self, workspace_root: &Path, scope: &str) {
192194
let output_dir = result_dir_from_workspace_root(workspace_root).join(scope);
193195
std::fs::create_dir_all(&output_dir).unwrap();
194-
let bench_id = uuid::Uuid::new_v4().to_string();
196+
let bench_id = generate_unique_id();
195197
let output_path = output_dir.join(format!("{bench_id}.json"));
196198
let mut writer = std::fs::File::create(&output_path).expect("Failed to create the file");
197199
serde_json::to_writer_pretty(&mut writer, self).expect("Failed to write the data");

0 commit comments

Comments
 (0)