Skip to main content

SourcePack

Struct SourcePack 

Source
pub struct SourcePack {
    pub root: PathBuf,
    pub manifest: PackManifest,
    pub rules: Vec<Rule>,
    pub source_digest: [u8; 32],
}

Fields§

§root: PathBuf§manifest: PackManifest§rules: Vec<Rule>§source_digest: [u8; 32]

Implementations§

Source§

impl SourcePack

Source

pub fn load(root: &Path) -> Result<Self, String>

Load and validate one explicit source-pack directory.

§Errors

Returns an error for unsafe paths, invalid JSON, schema errors, conflicts, or size limits.

Examples found in repository?
examples/rule_benchmark.rs (line 92)
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}

Trait Implementations§

Source§

impl Clone for SourcePack

Source§

fn clone(&self) -> SourcePack

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SourcePack

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.