Skip to content

Commit

Permalink
optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Dec 17, 2024
1 parent 1eeb00d commit 0baf4e4
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 276 deletions.
47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crux_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ anyhow.workspace = true
ascent = "0.7.0"
clap = { version = "4.4.18", features = ["derive"] }
console = "0.15.8"
env_logger = "0.10.2"
guppy = "0.17.4"
ignore = "0.4.23"
iter_tools = "0.24.0"
lazy-regex = "3.3.0"
libc = "0.2.167"
log = "0.4.22"
ramhorns = "1.0.1"
rustdoc-json = "0.9.3"
rustdoc-types = "0.33.0"
Expand Down
45 changes: 25 additions & 20 deletions crux_cli/src/codegen/filter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use ascent::ascent;
use log::info;
use rustdoc_types::Crate;

use super::item::*;
use super::node::{CrateNode, ItemNode, SummaryNode};

ascent! {
#![measure_rule_times]
pub struct Filter;

// ------- facts ------------------
Expand All @@ -13,37 +16,33 @@ ascent! {

// ------- rules ------------------

// this is an optimization to reduce the number of nodes we need to consider
relation subset(ItemNode);
subset(a) <-- item(a), if a.is_subset();

relation has_summary(ItemNode, SummaryNode);
has_summary(n, s) <-- subset(n), summary(s), if n.has_summary(s);
has_summary(n, s) <-- item(n), summary(s), if n.has_summary(s);

relation is_struct(ItemNode);
is_struct(s) <-- subset(s), if s.is_struct();
is_struct(s) <-- item(s), if is_struct(&s.item);

relation is_enum(ItemNode);
is_enum(e) <-- subset(e), if e.is_enum();
is_enum(e) <-- item(e), if is_enum(&e.item);

relation variant(ItemNode, ItemNode);
variant(e, v) <-- is_enum(e), subset(v), if e.has_variant(v);
variant(e, v) <-- is_enum(e), item(v), if e.has_variant(v);

relation field(ItemNode, ItemNode);
field(s, f) <-- is_struct(s), subset(f), if s.has_field(f);
field(v, f) <-- variant(e, v), subset(f), if v.has_field(f);
field(s, f) <-- is_struct(s), item(f), if s.has_field(f);
field(v, f) <-- variant(e, v), item(f), if v.has_field(f);

relation local_type_of(ItemNode, ItemNode);
local_type_of(f, t) <-- subset(f), subset(t), if f.is_of_local_type(t);
local_type_of(f, t) <-- item(f), item(t), if f.is_of_local_type(t);

relation remote_type_of(ItemNode, SummaryNode);
remote_type_of(f, t) <-- subset(f), summary(t), if f.is_of_remote_type(t);
remote_type_of(f, t) <-- item(f), summary(t), if f.is_of_remote_type(t);

// app structs have an implementation of the App trait
relation app(ItemNode, ItemNode);
app(imp, app) <--
is_struct(app),
subset(imp),
item(imp),
if imp.is_impl_for(app, "App");

// app hierarchy
Expand Down Expand Up @@ -79,7 +78,7 @@ ascent! {
effect(app, effect_ffi) <--
root_app(app_impl, app),
is_enum(effect),
subset(effect_impl),
item(effect_impl),
if effect_impl.is_impl_for(effect, "Effect"),
has_summary(app, app_summary),
has_summary(effect, effect_summary),
Expand All @@ -90,8 +89,8 @@ ascent! {
// Capability is a struct/enum with an implementation of the Capability trait
relation capability(ItemNode, ItemNode);
capability(cap, cap_impl) <--
subset(cap),
subset(cap_impl),
item(cap),
item(cap_impl),
if cap_impl.is_impl_for(cap, "Capability");

// Operation is an associated type of an impl of the Capability trait
Expand All @@ -105,7 +104,7 @@ ascent! {
relation output(ItemNode);
output(out) <--
operation(op),
subset(op_impl),
item(op_impl),
if op_impl.is_impl_for(op, "Operation"),
local_type_of(item, out),
if op_impl.has_associated_item(item, "Output");
Expand All @@ -123,7 +122,7 @@ ascent! {
// roots that are unit structs
edge(root, root) <--
root(root),
is_struct(root), if root.is_struct_unit();
is_struct(root), if is_struct_unit(&root.item);
// roots that have fields
edge(root, field) <--
root(root),
Expand Down Expand Up @@ -156,7 +155,7 @@ ascent! {

impl Filter {
pub fn update(&mut self, crate_name: &str, crate_: &Crate) {
println!("Updating filter for {}", crate_name);
info!("Updating filter for {}", crate_name);
self.summary = crate_
.paths
.iter()
Expand All @@ -171,7 +170,13 @@ impl Filter {
self.item = crate_
.index
.values()
.map(|item| (ItemNode::new(crate_name.to_string(), item.clone()),))
.filter_map(|item| {
if is_relevant(item) {
Some((ItemNode::new(crate_name.to_string(), item.clone()),))
} else {
None
}
})
.collect::<Vec<_>>();
self.ext_crate = crate_
.external_crates
Expand Down
55 changes: 9 additions & 46 deletions crux_cli/src/codegen/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use std::collections::BTreeMap;

use ascent::ascent;
use rustdoc_types::{GenericArg, GenericArgs, Item, ItemEnum, Type, Variant, VariantKind};
use rustdoc_types::{GenericArg, GenericArgs, Item, ItemEnum, Type};

use crate::codegen::collect;

use super::{
indexed::Indexed,
item::*,
node::ItemNode,
serde::case::RenameRule,
serde_generate::format::{ContainerFormat, Format, Named, VariantFormat},
};

ascent! {
#![measure_rule_times]
pub struct Formatter;

// ------- facts ------------------
Expand All @@ -21,13 +23,13 @@ ascent! {
// ------- rules ------------------

relation struct_unit(ItemNode);
struct_unit(s) <-- edge(s, _), if s.is_struct_unit();
struct_unit(s) <-- edge(s, _), if is_struct_unit(&s.item);

relation struct_plain(ItemNode);
struct_plain(s) <-- edge(s, _), if s.is_struct_plain();
struct_plain(s) <-- edge(s, _), if is_struct_plain(&s.item);

relation struct_tuple(ItemNode);
struct_tuple(s) <-- edge(s, _), if s.is_struct_tuple();
struct_tuple(s) <-- edge(s, _), if is_struct_tuple(&s.item);

relation field(ItemNode, ItemNode);
field(x, f) <-- edge(x, f), if x.has_field(f);
Expand All @@ -48,13 +50,13 @@ ascent! {
let variants = e.variants(vs);

relation variant_plain(ItemNode, ItemNode);
variant_plain(e, v) <-- variant(e, v), if is_plain_variant(&v);
variant_plain(e, v) <-- variant(e, v), if is_plain_variant(&v.item);

relation variant_tuple(ItemNode, ItemNode);
variant_tuple(e, v) <-- variant(e, v), if is_tuple_variant(&v);
variant_tuple(e, v) <-- variant(e, v), if is_tuple_variant(&v.item);

relation variant_struct(ItemNode, ItemNode);
variant_struct(e, v) <-- variant(e, v), if is_struct_variant(&v);
variant_struct(e, v) <-- variant(e, v), if is_struct_variant(&v.item);

relation format(ItemNode, Indexed<Format>);
format(x, format) <--
Expand Down Expand Up @@ -162,45 +164,6 @@ fn make_named_format(
}
}

fn is_plain_variant(variant: &ItemNode) -> bool {
matches!(
&variant.item,
Item {
inner: ItemEnum::Variant(Variant {
kind: VariantKind::Plain,
..
}),
..
}
)
}

fn is_struct_variant(variant: &ItemNode) -> bool {
matches!(
&variant.item,
Item {
inner: ItemEnum::Variant(Variant {
kind: VariantKind::Struct { .. },
..
}),
..
}
)
}

fn is_tuple_variant(variant: &ItemNode) -> bool {
matches!(
&variant.item,
Item {
inner: ItemEnum::Variant(Variant {
kind: VariantKind::Tuple(_),
..
}),
..
}
)
}

fn make_plain_variant_format(
variant: &ItemNode,
all_variants: &Vec<ItemNode>,
Expand Down
Loading

0 comments on commit 0baf4e4

Please sign in to comment.