Skip to content

Commit

Permalink
Merge pull request #235 from serpent-os/fix-determinism
Browse files Browse the repository at this point in the history
all: Switch to deterministic BTreeMap/BTreeSet
  • Loading branch information
ikeycode committed Jun 8, 2024
2 parents 0f79ff2 + d018311 commit 8ca5186
Show file tree
Hide file tree
Showing 27 changed files with 161 additions and 156 deletions.
2 changes: 1 addition & 1 deletion boulder/src/architecture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Architecture {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Display)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Display)]
pub enum BuildTarget {
#[display(fmt = "{_0}")]
Native(Architecture),
Expand Down
12 changes: 6 additions & 6 deletions boulder/src/build/job/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
//
// SPDX-License-Identifier: MPL-2.0

use std::collections::HashSet;

use itertools::Itertools;
use std::collections::BTreeSet;

use stone_recipe::{
script,
tuning::{self, Toolchain},
Script,
};

use tui::Styled;

use super::{work_dir, Error};
use crate::build::pgo;
use crate::{architecture::BuildTarget, util, Macros, Paths, Recipe};

use super::{work_dir, Error};

pub fn list(pgo_stage: Option<pgo::Stage>) -> Vec<Phase> {
if matches!(pgo_stage, Some(pgo::Stage::One | pgo::Stage::Two)) {
Phase::WORKLOAD.to_vec()
Expand All @@ -25,7 +25,7 @@ pub fn list(pgo_stage: Option<pgo::Stage>) -> Vec<Phase> {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, strum::Display)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, strum::Display)]
pub enum Phase {
Prepare,
Setup,
Expand Down Expand Up @@ -316,7 +316,7 @@ fn add_tuning(
flags
.map(|s| s.trim())
.filter(|s| s.len() > 1)
.collect::<HashSet<_>>()
.collect::<BTreeSet<_>>()
.into_iter()
.join(" ")
}
Expand Down
2 changes: 1 addition & 1 deletion boulder/src/build/pgo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn stages(recipe: &Recipe, target: BuildTarget) -> Option<Vec<Stage>> {
})
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, strum::Display)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, strum::Display)]
pub enum Stage {
#[strum(serialize = "stage1")]
One,
Expand Down
7 changes: 4 additions & 3 deletions boulder/src/build/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
//
// SPDX-License-Identifier: MPL-2.0

use std::collections::HashSet;
use std::collections::BTreeSet;
use std::{fs, io};

use thiserror::Error;

use moss::{repository, runtime, Installation};
use stone_recipe::{tuning::Toolchain, Upstream};
use thiserror::Error;

use crate::build::Builder;
use crate::{container, timing, util, Timing};
Expand Down Expand Up @@ -149,7 +150,7 @@ fn packages(builder: &Builder) -> Vec<&str> {
.into_iter()
.chain(extra_deps)
// Remove dupes
.collect::<HashSet<_>>()
.collect::<BTreeSet<_>>()
.into_iter()
.collect()
}
Expand Down
24 changes: 13 additions & 11 deletions boulder/src/cli/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
//
// SPDX-License-Identifier: MPL-2.0

use std::{collections::HashMap, io};
use std::{collections::BTreeMap, io};

use boulder::{profile, Env, Profile};
use clap::Parser;
use itertools::Itertools;
use moss::{repository, runtime, Installation, Repository};
use thiserror::Error;
use url::Url;

use boulder::{profile, Env, Profile};
use moss::{repository, runtime, Installation, Repository};

#[derive(Debug, Parser)]
#[command(about = "Manage boulder profiles")]
pub struct Command {
Expand All @@ -27,13 +28,13 @@ pub enum Subcommand {
#[arg(help = "profile name")]
name: String,
#[arg(
short = 'r',
long = "repo",
required = true,
help = "profile repositories",
value_parser = parse_repository,
help = "repository to add to profile, can be passed multiple times",
long_help = "repository to add to profile\n\nExample: --repo name=volatile,uri=https://dev.serpentos.com/volatile/x86_64/stone.index,priority=100"
short = 'r',
long = "repo",
required = true,
help = "profile repositories",
value_parser = parse_repository,
help = "repository to add to profile, can be passed multiple times",
long_help = "repository to add to profile\n\nExample: --repo name=volatile,uri=https://dev.serpentos.com/volatile/x86_64/stone.index,priority=100"
)]
repos: Vec<(repository::Id, Repository)>,
},
Expand All @@ -49,7 +50,7 @@ fn parse_repository(s: &str) -> Result<(repository::Id, Repository), String> {
let key_values = s
.split(',')
.filter_map(|kv| kv.split_once('='))
.collect::<HashMap<_, _>>();
.collect::<BTreeMap<_, _>>();

let id = repository::Id::new(key_values.get("name").ok_or("missing name")?.to_string());
let uri = key_values
Expand Down Expand Up @@ -140,6 +141,7 @@ pub fn update<'a>(env: &'a Env, manager: profile::Manager<'a>, profile: &profile

Ok(())
}

#[derive(Debug, Error)]
pub enum Error {
#[error("config")]
Expand Down
17 changes: 7 additions & 10 deletions boulder/src/draft/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// SPDX-FileCopyrightText: Copyright © 2020-2024 Serpent OS Developers
//
// SPDX-License-Identifier: MPL-2.0
use std::{
collections::{HashMap, HashSet},
fmt,
num::NonZeroU64,
};
use std::collections::{BTreeMap, BTreeSet};
use std::{fmt, num::NonZeroU64};

use moss::Dependency;

Expand All @@ -20,7 +17,7 @@ mod python;
pub type Error = Box<dyn std::error::Error>;

/// A build system
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, strum::Display)]
#[strum(serialize_all = "lowercase")]
pub enum System {
Autotools,
Expand Down Expand Up @@ -102,7 +99,7 @@ impl fmt::Display for Phases {
/// State passed to each system when processing paths
struct State<'a> {
/// Any dependencies that need to be recorded
dependencies: &'a mut HashSet<Dependency>,
dependencies: &'a mut BTreeSet<Dependency>,
/// Total confidence level of the current build [`System`]
confidence: u64,
}
Expand All @@ -124,14 +121,14 @@ pub struct Analysis {
/// The detected build [`System`], if any
pub detected_system: Option<System>,
/// All detected dependencies
pub dependencies: HashSet<Dependency>,
pub dependencies: BTreeSet<Dependency>,
}

/// Analyze the provided paths to determine which build [`System`]
/// the project uses and any dependencies that are identified
pub fn analyze(files: &[File]) -> Result<Analysis, Error> {
let mut dependencies = HashSet::new();
let mut confidences = HashMap::new();
let mut dependencies = BTreeSet::new();
let mut confidences = BTreeMap::new();

for system in System::ALL {
let mut state = State {
Expand Down
7 changes: 4 additions & 3 deletions boulder/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
//
// SPDX-License-Identifier: MPL-2.0

use std::{collections::HashMap, fs, io, path::Path};
use std::collections::BTreeMap;
use std::{fs, io, path::Path};

use thiserror::Error;

use crate::{util, Env};

#[derive(Debug)]
pub struct Macros {
pub arch: HashMap<String, stone_recipe::Macros>,
pub arch: BTreeMap<String, stone_recipe::Macros>,
pub actions: Vec<stone_recipe::Macros>,
}

Expand All @@ -25,7 +26,7 @@ impl Macros {
let arch_files = util::enumerate_files(&arch_dir, matcher).map_err(Error::ArchFiles)?;
let action_files = util::enumerate_files(&actions_dir, matcher).map_err(Error::ActionFiles)?;

let mut arch = HashMap::new();
let mut arch = BTreeMap::new();
let mut actions = vec![];

for file in arch_files {
Expand Down
20 changes: 9 additions & 11 deletions boulder/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// SPDX-FileCopyrightText: Copyright © 2020-2024 Serpent OS Developers
//
// SPDX-License-Identifier: MPL-2.0
use std::{
collections::{hash_map, HashMap},
fs, io,
num::NonZeroU64,
};
use std::collections::{btree_map, BTreeMap};
use std::{fs, io, num::NonZeroU64};

use itertools::Itertools;
use thiserror::Error;

use stone::write::digest;
use stone_recipe::{script, Package};
use thiserror::Error;

use crate::{build, container, timing, util, Macros, Paths, Recipe, Timing};

Expand All @@ -24,7 +22,7 @@ mod emit;
pub struct Packager<'a> {
paths: &'a Paths,
recipe: &'a Recipe,
packages: HashMap<String, Package>,
packages: BTreeMap<String, Package>,
collector: Collector,
build_release: NonZeroU64,
}
Expand Down Expand Up @@ -119,13 +117,13 @@ fn resolve_packages(
macros: &Macros,
recipe: &Recipe,
collector: &mut Collector,
) -> Result<HashMap<String, Package>, Error> {
) -> Result<BTreeMap<String, Package>, Error> {
let mut parser = script::Parser::new();
parser.add_definition("name", &recipe.parsed.source.name);
parser.add_definition("version", &recipe.parsed.source.version);
parser.add_definition("release", recipe.parsed.source.release);

let mut packages = HashMap::new();
let mut packages = BTreeMap::new();

// Add a package, ensuring it's fully expanded
//
Expand Down Expand Up @@ -165,10 +163,10 @@ fn resolve_packages(
}

match packages.entry(name.clone()) {
hash_map::Entry::Vacant(entry) => {
btree_map::Entry::Vacant(entry) => {
entry.insert(package);
}
hash_map::Entry::Occupied(entry) => {
btree_map::Entry::Occupied(entry) => {
let prev = entry.remove();

package.run_deps = package.run_deps.into_iter().chain(prev.run_deps).sorted().collect();
Expand Down
8 changes: 5 additions & 3 deletions boulder/src/package/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
//
// SPDX-License-Identifier: MPL-2.0

use std::collections::BTreeMap;
use std::{
collections::{BTreeSet, HashMap, VecDeque},
collections::{BTreeSet, VecDeque},
path::PathBuf,
};

use moss::{Dependency, Provider};
use stone::write::digest;
use tui::{ProgressBar, ProgressStyle, Styled};

use super::collect::{Collector, PathInfo};
use crate::{Paths, Recipe};

use super::collect::{Collector, PathInfo};

mod handler;

pub type BoxError = Box<dyn std::error::Error>;
Expand All @@ -24,7 +26,7 @@ pub struct Chain<'a> {
paths: &'a Paths,
collector: &'a Collector,
hasher: &'a mut digest::Hasher,
pub buckets: HashMap<String, Bucket>,
pub buckets: BTreeMap<String, Bucket>,
}

impl<'a> Chain<'a> {
Expand Down
14 changes: 7 additions & 7 deletions boulder/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
//
// SPDX-License-Identifier: MPL-2.0

use std::collections::HashMap;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use thiserror::Error;

use config::Config;
use derive_more::Display;
use moss::repository;
pub use moss::{repository::Priority, Repository};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::Env;

/// A unique [`Profile`] identifier
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd, Display)]
#[serde(from = "String")]
pub struct Id(String);

Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct Profile {

/// A map of profiles
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Map(HashMap<Id, Profile>);
pub struct Map(BTreeMap<Id, Profile>);

impl Map {
pub fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Map {

impl IntoIterator for Map {
type Item = (Id, Profile);
type IntoIter = std::collections::hash_map::IntoIter<Id, Profile>;
type IntoIter = std::collections::btree_map::IntoIter<Id, Profile>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
Expand Down
12 changes: 5 additions & 7 deletions crates/fnmatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
//! let result = pattern.match_path("/usr/lib/modules/6.2.3/kernel").expect("no kernel match");
//! ```

use std::{
collections::{HashMap, HashSet},
convert::Infallible,
str::FromStr,
};
use std::collections::{BTreeMap, BTreeSet};
use std::{convert::Infallible, str::FromStr};

use regex::Regex;
use serde::{de, Deserialize};
use thiserror::Error;

#[derive(Debug)]
enum Fragment {
/// `?`
Expand Down Expand Up @@ -116,7 +114,7 @@ pub struct Match {
pub path: String,

/// Captured variables, as defined by the [`Pattern::groups()`]
pub variables: HashMap<String, String>,
pub variables: BTreeMap<String, String>,
}

impl Pattern {
Expand Down Expand Up @@ -243,7 +241,7 @@ impl FromStr for Pattern {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let fragments = fragments_from_string(s)?;
let mut groups = HashSet::new();
let mut groups = BTreeSet::new();

let compiled = fragments
.iter()
Expand Down
Loading

0 comments on commit 8ca5186

Please sign in to comment.