pub fn apply(
graph: &BuildGraph,
transform: &Transform,
) -> Result<Applied, TransformError>Expand description
Applies transform to graph, leaving graph untouched.
Each transformation copies the graph once, visiting every unit and edge a constant number of
times. Handing the edges to GraphBuilder puts each one into an ordered map, so the cost is
O(V + E log E), of which validating the result is O(V + E).
ยงErrors
TransformError::UnknownUnit if the transformation names an index outside the graph,
TransformError::UnsplittableKind and TransformError::FractionAboveOne if a split is
impossible or asks for more than the whole unit, TransformError::MissingEdge if an edge
removal names an edge that is not there, and TransformError::Rebuild if the produced graph
is rejected, which for a split of a graph already at crate::MAX_UNITS means there is no
room for the extra unit.
use core::num::NonZeroU64;
use critpath_core::{
apply, GraphBuilder, Millis, SplitDependents, Transform, Unit, UnitKind,
};
let mut builder = GraphBuilder::new();
let heavy = builder.add_unit(Unit {
package: "heavy".to_owned(),
version: "1.0.0".to_owned(),
target: "heavy".to_owned(),
kind: UnitKind::Lib,
total: Millis::new(1_000),
metadata_at: Some(Millis::new(700)),
requires_full_closure: false,
})?;
let graph = builder.build()?;
let applied = apply(
&graph,
&Transform::SplitUnit {
unit: heavy,
numerator: 1,
denominator: NonZeroU64::new(2).ok_or("2 is not zero")?,
dependents: SplitDependents::Pessimistic,
overhead: Millis::new(50),
},
)?;
// 1000ms divided in half is 500ms in each part, and each part pays the 50ms overhead.
assert_eq!(applied.graph().len(), 2);
assert_eq!(applied.graph().total_work(), Millis::new(1_100));