refactor(tvix/derivation): use BTreeSet for derivation outputs

When constructing derivations inside builtins.derivationStrict, we'd
have to very frequently check whether certain outputs have already
been inserted into the derivation inputs.

Using a set type is much easier, especially as this has to be ordered
and the source data that is being inserted also comes from a set,
which might let us pass this more efficiently in the future.

Note that the validate function no longer checks the order of the
entries, as that is now guaranteed by the type.

Change-Id: I2fbb984facba3e668075f6f8df8992092368c63d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7826
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Vincent Ambo 2023-01-13 20:19:48 +03:00 committed by tazjin
parent 9a500c3e9b
commit a35dadf9f0
4 changed files with 13 additions and 16 deletions

View file

@ -3,6 +3,7 @@ use crate::output::{Hash, Output};
use crate::write;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::BTreeSet;
use std::{collections::BTreeMap, fmt, fmt::Write};
use tvix_store::nixbase32::NIXBASE32;
use tvix_store::store_path::{ParseStorePathError, StorePath, STORE_DIR};
@ -18,7 +19,7 @@ pub struct Derivation {
pub environment: BTreeMap<String, String>,
#[serde(rename = "inputDrvs")]
pub input_derivations: BTreeMap<String, Vec<String>>,
pub input_derivations: BTreeMap<String, BTreeSet<String>>,
#[serde(rename = "inputSrcs")]
pub input_sources: Vec<String>,
@ -173,13 +174,14 @@ impl Derivation {
hasher.finalize()
}
None => {
let mut replaced_input_derivations: BTreeMap<String, Vec<String>> = BTreeMap::new();
let mut replaced_input_derivations: BTreeMap<String, BTreeSet<String>> =
BTreeMap::new();
// For each input_derivation, look up the replacement.
for (drv_path, input_derivation) in &self.input_derivations {
replaced_input_derivations.insert(
fn_get_drv_replacement(drv_path).to_string(),
input_derivation.to_vec(),
input_derivation.clone(),
);
}

View file

@ -1,5 +1,6 @@
use crate::derivation::Derivation;
use crate::output::{Hash, Output};
use std::collections::BTreeSet;
use std::fs::File;
use std::io::Read;
use std::path::Path;
@ -272,9 +273,10 @@ fn output_path_construction() {
);
// assemble foo input_derivations
foo_drv
.input_derivations
.insert(bar_drv_path.to_absolute_path(), vec!["out".to_string()]);
foo_drv.input_derivations.insert(
bar_drv_path.to_absolute_path(),
BTreeSet::from(["out".to_string()]),
);
// calculate foo output paths
let foo_calc_result = foo_drv.calculate_output_paths(

View file

@ -50,21 +50,13 @@ impl Derivation {
);
}
for (i, output_name) in output_names.iter().enumerate() {
for output_name in output_names.iter() {
if output_name.is_empty() {
bail!(
"output name entry for {} may not be empty",
input_derivation_path
)
}
// if i is at least 1, peek at the previous element to ensure output_names are sorted.
if i > 0 && (output_names[i - 1] >= *output_name) {
bail!(
"invalid input derivation output order: {} < {}",
output_name,
output_names[i - 1],
);
}
}
}

View file

@ -1,5 +1,6 @@
use crate::output::Output;
use crate::string_escape::escape_string;
use std::collections::BTreeSet;
use std::{collections::BTreeMap, fmt, fmt::Write};
pub const DERIVATION_PREFIX: &str = "Derive";
@ -84,7 +85,7 @@ pub fn write_outputs(
pub fn write_input_derivations(
writer: &mut impl Write,
input_derivations: &BTreeMap<String, Vec<String>>,
input_derivations: &BTreeMap<String, BTreeSet<String>>,
) -> Result<(), fmt::Error> {
writer.write_char(COMMA)?;
writer.write_char(BRACKET_OPEN)?;