Skip to main content

Registry

Struct Registry 

Source
pub struct Registry { /* private fields */ }

Implementations§

Source§

impl Registry

Source

pub fn load(requests: &[PackRequest]) -> Result<Self, String>

Load explicit compiled packs and disable deterministic ID conflicts.

§Errors

Returns an error when a requested pack cannot be opened or validated.

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

pub fn builtins_only() -> Self

Examples found in repository?
examples/rule_benchmark.rs (line 55)
47fn benchmark_builtin() -> Result<Duration, String> {
48    let arguments = vec![
49        "cargo".to_owned(),
50        "test".to_owned(),
51        "--workspace".to_owned(),
52    ];
53    let mut samples = Vec::with_capacity(MATCH_SAMPLES);
54    for _ in 0..MATCH_SAMPLES {
55        let mut registry = Registry::builtins_only();
56        let started = Instant::now();
57        let selection = registry.select(black_box(&arguments))?;
58        if !matches!(selection, Selection::Reduce(_)) {
59            return Err("built-in benchmark command was not selected".to_owned());
60        }
61        samples.push(started.elapsed());
62    }
63    Ok(p95(samples))
64}
65
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}
123
124fn benchmark_stream() -> Result<(f64, usize), String> {
125    let mut registry = Registry::builtins_only();
126    let Selection::Reduce(selected) = registry.select(&["rg".to_owned(), "needle".to_owned()])?
127    else {
128        return Err("stream benchmark rule was not selected".to_owned());
129    };
130    let chunk = b"src/file.rs:42:matching line with useful context\n".repeat(160);
131    let bytes = 1_024 * 1_024 * 1_024;
132    let started = Instant::now();
133    let mut reducer = StreamReducer::new(&selected.rule)?;
134    let mut processed = 0;
135    while processed < bytes {
136        reducer.push(black_box(&chunk));
137        processed += chunk.len();
138    }
139    black_box(reducer.finish(true, None));
140    let processed = u32::try_from(processed)
141        .map_err(|_| "stream benchmark byte count does not fit u32".to_owned())?;
142    let throughput = f64::from(processed) / 1_000_000.0 / started.elapsed().as_secs_f64();
143    Ok((throughput, configured_memory_bound(&selected.rule)?))
144}
Source

pub fn select(&mut self, arguments: &[String]) -> Result<Selection, String>

Select one action for an already parsed child argument vector.

§Errors

Returns an error when an indexed external candidate record is corrupt or invalid.

Examples found in repository?
examples/rule_benchmark.rs (line 57)
47fn benchmark_builtin() -> Result<Duration, String> {
48    let arguments = vec![
49        "cargo".to_owned(),
50        "test".to_owned(),
51        "--workspace".to_owned(),
52    ];
53    let mut samples = Vec::with_capacity(MATCH_SAMPLES);
54    for _ in 0..MATCH_SAMPLES {
55        let mut registry = Registry::builtins_only();
56        let started = Instant::now();
57        let selection = registry.select(black_box(&arguments))?;
58        if !matches!(selection, Selection::Reduce(_)) {
59            return Err("built-in benchmark command was not selected".to_owned());
60        }
61        samples.push(started.elapsed());
62    }
63    Ok(p95(samples))
64}
65
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}
123
124fn benchmark_stream() -> Result<(f64, usize), String> {
125    let mut registry = Registry::builtins_only();
126    let Selection::Reduce(selected) = registry.select(&["rg".to_owned(), "needle".to_owned()])?
127    else {
128        return Err("stream benchmark rule was not selected".to_owned());
129    };
130    let chunk = b"src/file.rs:42:matching line with useful context\n".repeat(160);
131    let bytes = 1_024 * 1_024 * 1_024;
132    let started = Instant::now();
133    let mut reducer = StreamReducer::new(&selected.rule)?;
134    let mut processed = 0;
135    while processed < bytes {
136        reducer.push(black_box(&chunk));
137        processed += chunk.len();
138    }
139    black_box(reducer.finish(true, None));
140    let processed = u32::try_from(processed)
141        .map_err(|_| "stream benchmark byte count does not fit u32".to_owned())?;
142    let throughput = f64::from(processed) / 1_000_000.0 / started.elapsed().as_secs_f64();
143    Ok((throughput, configured_memory_bound(&selected.rule)?))
144}
Source

pub fn references(&self) -> &[PackReference]

Source

pub fn diagnostics(&self) -> &[String]

Source

pub fn summaries(&mut self) -> Result<Vec<RuleSummary>, String>

Read summaries for every enabled built-in and external rule.

§Errors

Returns an error when an enabled external record is corrupt or invalid.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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.