pub struct Millis(/* private fields */);Expand description
A duration in whole milliseconds.
Weights are integers rather than floating-point seconds so that ordering is total, sums are exact, and identical input produces identical output on every platform. Cargo reports two decimal places of seconds, so milliseconds lose nothing that was ever measured.
Arithmetic saturates rather than overflowing, so a graph with absurd weights produces an absurd answer instead of a panic or a wrapped one.
use critpath_core::Millis;
let a = Millis::new(1_500);
let b = Millis::new(250);
assert_eq!(a.saturating_add(b), Millis::new(1_750));
assert_eq!(b.saturating_sub(a), Millis::ZERO);
assert_eq!(a.to_string(), "1500ms");Implementations§
Source§impl Millis
impl Millis
Sourcepub const fn new(ms: u64) -> Self
pub const fn new(ms: u64) -> Self
A duration of ms milliseconds.
Examples found in repository?
31fn build(units: usize, fan_in: usize) -> Result<BuildGraph, Box<dyn Error>> {
32 let tenths = NonZeroU64::new(10).ok_or("ten is not zero")?;
33 let mut builder = GraphBuilder::with_capacity(units);
34 let mut ids = Vec::with_capacity(units);
35 for i in 0..units {
36 let total = weight(i);
37 let unit = Unit {
38 package: format!("p{i}"),
39 version: "1.0.0".to_owned(),
40 target: format!("t{i}"),
41 kind: if is_opaque(i) {
42 UnitKind::ProcMacro
43 } else {
44 UnitKind::Lib
45 },
46 total: Millis::new(total),
47 // A library emits metadata about seven tenths of the way through, which is what the
48 // dev-profile builds measured in docs/validation.md do.
49 metadata_at: if is_opaque(i) {
50 None
51 } else {
52 Some(Millis::new(total).scaled(7, tenths))
53 },
54 requires_full_closure: is_opaque(i),
55 };
56 ids.push(builder.add_unit(unit)?);
57 }
58 for i in 0..units {
59 for k in 1..=fan_in {
60 let Some(back) = i.checked_sub(k * 3) else {
61 continue;
62 };
63 let (Some(&from), Some(&to)) = (ids.get(back), ids.get(i)) else {
64 continue;
65 };
66 // A metadata edge requires the producer to emit metadata and the consumer not to link.
67 let kind = if !is_opaque(back) && !is_opaque(i) {
68 EdgeKind::Metadata
69 } else {
70 EdgeKind::Full
71 };
72 builder.add_edge(from, to, kind)?;
73 }
74 }
75 Ok(builder.build()?)
76}Sourcepub const fn saturating_add(self, other: Self) -> Self
pub const fn saturating_add(self, other: Self) -> Self
The sum, clamped to the maximum representable duration.
Sourcepub const fn saturating_sub(self, other: Self) -> Self
pub const fn saturating_sub(self, other: Self) -> Self
The difference, clamped at zero. There are no negative durations.
Sourcepub fn div_ceil_by(self, divisor: NonZeroU64) -> Self
pub fn div_ceil_by(self, divisor: NonZeroU64) -> Self
This duration divided by divisor, rounded up.
Rounding up matters because this computes the area bound total_work / J, which is a lower
bound on the makespan. Rounding down would report a bound that no schedule can achieve.
Sourcepub fn scaled(self, numerator: u64, denominator: NonZeroU64) -> Self
pub fn scaled(self, numerator: u64, denominator: NonZeroU64) -> Self
This duration scaled by the fraction numerator / denominator, rounded to nearest.
Used to divide a unit’s weight between the halves of a hypothetical split. Widening to 128 bits keeps the product exact for any duration a real build could produce.
Examples found in repository?
31fn build(units: usize, fan_in: usize) -> Result<BuildGraph, Box<dyn Error>> {
32 let tenths = NonZeroU64::new(10).ok_or("ten is not zero")?;
33 let mut builder = GraphBuilder::with_capacity(units);
34 let mut ids = Vec::with_capacity(units);
35 for i in 0..units {
36 let total = weight(i);
37 let unit = Unit {
38 package: format!("p{i}"),
39 version: "1.0.0".to_owned(),
40 target: format!("t{i}"),
41 kind: if is_opaque(i) {
42 UnitKind::ProcMacro
43 } else {
44 UnitKind::Lib
45 },
46 total: Millis::new(total),
47 // A library emits metadata about seven tenths of the way through, which is what the
48 // dev-profile builds measured in docs/validation.md do.
49 metadata_at: if is_opaque(i) {
50 None
51 } else {
52 Some(Millis::new(total).scaled(7, tenths))
53 },
54 requires_full_closure: is_opaque(i),
55 };
56 ids.push(builder.add_unit(unit)?);
57 }
58 for i in 0..units {
59 for k in 1..=fan_in {
60 let Some(back) = i.checked_sub(k * 3) else {
61 continue;
62 };
63 let (Some(&from), Some(&to)) = (ids.get(back), ids.get(i)) else {
64 continue;
65 };
66 // A metadata edge requires the producer to emit metadata and the consumer not to link.
67 let kind = if !is_opaque(back) && !is_opaque(i) {
68 EdgeKind::Metadata
69 } else {
70 EdgeKind::Full
71 };
72 builder.add_edge(from, to, kind)?;
73 }
74 }
75 Ok(builder.build()?)
76}