Support cargo's unstable `unit-graph` feature

finchie
May 8, 2024, 9:31 AM
3NPBHM5FRTNMFB6RK5H765YZEVGYA7B4YAMEYBOHYUBRM7QSXSOQC

Dependencies

  • [2] WFBF7VS6 Split `annotations` and `graph` into 2 separate modules
  • [3] 7CVIL7UJ Create simple metadata parser
  • [4] B2L26LOA Store index of dependency nodes
  • [5] XVQXXAGZ Add support for generating treemaps
  • [6] BRXHJFU7 Refactor measurements into `annotations` module
  • [7] C43IWI7G Move visualization logic into separate module
  • [8] LOR3KOXG Parse JSON output from `cargo build --timings`
  • [9] YA5ITLOV Add support for Sankey diagrams
  • [10] ZEN3WUPD Add support for histogram charts
  • [11] JVYWRCPT Add basic chart visualisation
  • [12] ZPFD3275 Switch from `cargo_metadata`+`petgraph` to `guppy`
  • [13] PJPTNU2S Skip re-generating `--timings=json` on every run

Change contents

  • replacement in src/main.rs at line 13
    [3.328][3.328:387](),[3.387][3.3103:3169]()
    let source_data = include_bytes!("../timings.stdout");
    let timings = annotations::timings::Output::new(source_data);
    [3.328]
    [2.799]
    let timings_data = include_bytes!("../timings.stdout");
    let timings = annotations::timings::Output::new(timings_data);
  • edit in src/main.rs at line 16
    [2.875]
    [3.2757]
    // Skip rebuilding every time by caching the output from `--unit-graph` as a local file
    let unit_graph_data = include_str!("../unit-graph.json");
    let unit_graph = annotations::unit_graph::UnitGraph::new(unit_graph_data).unwrap();
  • replacement in src/main.rs at line 24
    [3.897][2.876:953]()
    let graph = AnnotationGraph::new(&package_graph, timings, self_profile);
    [3.897]
    [3.3253]
    let graph = AnnotationGraph::new(&package_graph, timings, self_profile, unit_graph);
  • replacement in src/graph/mod.rs at line 1
    [3.3449][2.1053:1102]()
    use crate::annotations::{self_profile, timings};
    [3.3449]
    [3.3481]
    use crate::annotations::{self_profile, timings, unit_graph};
  • edit in src/graph/mod.rs at line 47
    [2.1274]
    [3.4539]
    unit_graph: unit_graph::UnitGraph,
  • edit in src/graph/mod.rs at line 49
    [3.4555]
    [2.1275]
    dbg!(unit_graph.roots);
  • file addition: unit_graph.rs (----------)
    [3.3345]
    use cargo_metadata::PackageId;
    use serde::{Deserialize, Serialize};
    /// See https://doc.rust-lang.org/nightly/nightly-rustc/cargo/core/profiles/enum.PanicStrategy.html
    #[derive(Clone, Debug, Serialize, Deserialize)]
    #[serde(rename_all = "kebab-case")]
    pub enum PanicStrategy {
    Unwind,
    Abort,
    }
    /// See https://doc.rust-lang.org/nightly/nightly-rustc/cargo/core/profiles/struct.Profile.html
    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct Profile {
    pub name: String,
    pub opt_level: String,
    pub lto: String,
    pub codegen_units: Option<usize>,
    pub debuginfo: usize,
    pub debug_assertions: bool,
    pub overflow_checks: bool,
    pub rpath: bool,
    pub incremental: bool,
    pub panic: PanicStrategy,
    }
    /// See https://doc.rust-lang.org/nightly/nightly-rustc/cargo/core/compiler/unit_graph/struct.UnitDep.html
    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct UnitDep {
    pub index: usize,
    pub extern_crate_name: String,
    pub public: bool,
    pub noprelude: bool,
    }
    /// See https://doc.rust-lang.org/nightly/nightly-rustc/cargo/core/compiler/unit/struct.UnitInner.html
    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct Unit {
    pub pkg_id: PackageId,
    pub target: super::Target,
    pub profile: Profile,
    pub platform: Option<String>,
    pub mode: super::Mode,
    pub features: Vec<String>,
    pub is_std: Option<bool>,
    pub dependencies: Vec<UnitDep>,
    }
    /// See https://doc.rust-lang.org/cargo/reference/unstable.html#unit-graph
    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct UnitGraph {
    version: usize,
    pub units: Vec<Unit>,
    pub roots: Vec<usize>,
    }
    impl UnitGraph {
    pub fn new(source: &str) -> Result<Self, serde_json::Error> {
    let graph: Self = serde_json::from_str(source)?;
    assert_eq!(graph.version, 1);
    Ok(graph)
    }
    }
  • edit in src/annotations/mod.rs at line 7
    [2.1636]
    [2.1636]
    pub mod unit_graph;