Skip to main content

compile

Function compile 

Source
pub fn compile(pack: &SourcePack) -> Result<Vec<u8>, String>
Expand description

Compile a validated source pack into deterministic indexed bytes.

ยงErrors

Returns an error when the source model is invalid or a compiled size or index conversion fails.

Examples found in repository?
examples/rule_benchmark.rs (line 93)
66fn benchmark_external() -> Result<(Duration, Duration, usize), String> {
67    let directory = TempDir::new().map_err(|error| error.to_string())?;
68    let source_root = directory.path().join("source");
69    let rules_root = source_root.join("rules");
70    fs::create_dir_all(&rules_root).map_err(|error| error.to_string())?;
71    let mut paths = Vec::with_capacity(EXTERNAL_RULES);
72    for index in 0..EXTERNAL_RULES {
73        let relative = format!("rules/rule-{index:04}.json");
74        let id = format!("bench/rule-{index:04}");
75        let program = format!("bench{index:04}");
76        let body = format!(
77            "{{\"id\":{id:?},\"match\":{{\"program\":[{program:?}]}},\"action\":\"reduce\",\"reducer\":{{\"kind\":\"list_summary\"}},\"success\":{{\"max_line_bytes\":16384,\"max_output_bytes\":32768,\"min_savings_bytes\":120,\"min_savings_basis_points\":1000}},\"failure\":{{\"max_line_bytes\":16384,\"max_output_bytes\":65536,\"min_savings_bytes\":120,\"min_savings_basis_points\":500}}}}"
78        );
79        fs::write(source_root.join(&relative), body).map_err(|error| error.to_string())?;
80        paths.push(relative);
81    }
82    let manifest = serde_json::json!({
83        "schema_version": 1,
84        "id": "benchmark-pack",
85        "rules": paths,
86    });
87    fs::write(
88        source_root.join("pack.json"),
89        serde_json::to_vec(&manifest).map_err(|error| error.to_string())?,
90    )
91    .map_err(|error| error.to_string())?;
92    let source = SourcePack::load(&source_root)?;
93    let body = compile(&source)?;
94    let pack_path = directory.path().join("benchmark.yrp");
95    fs::write(&pack_path, &body).map_err(|error| error.to_string())?;
96    let arguments = vec!["bench0999".to_owned()];
97    let request = PackRequest {
98        path: pack_path,
99        expected_digest: Some(source.source_digest),
100        expected_compiled_digest: None,
101    };
102    let cold_started = Instant::now();
103    let mut cold_registry = Registry::load(std::slice::from_ref(&request))?;
104    if !matches!(
105        cold_registry.select(black_box(&arguments))?,
106        Selection::Reduce(_)
107    ) {
108        return Err("external benchmark command was not selected".to_owned());
109    }
110    let cold = cold_started.elapsed();
111    let mut samples = Vec::with_capacity(MATCH_SAMPLES);
112    for _ in 0..MATCH_SAMPLES {
113        let started = Instant::now();
114        let mut registry = Registry::load(std::slice::from_ref(&request))?;
115        let selection = registry.select(black_box(&arguments))?;
116        if !matches!(selection, Selection::Reduce(_)) {
117            return Err("external benchmark command was not selected".to_owned());
118        }
119        samples.push(started.elapsed());
120    }
121    Ok((cold, p95(samples), body.len()))
122}