Skip to content

Commit 0b94344

Browse files
committed
serialize and benchmark it
1 parent 215714e commit 0b94344

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

benches/example_benchmarks.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ fn run_example(filename: &str, program: &str, no_messages: bool) {
99
egraph
1010
.parse_and_run_program(Some(filename.to_owned()), program)
1111
.unwrap();
12+
// test performance of serialization as well
13+
let _ = egraph.serialize(egglog::SerializeConfig::default());
1214
}
1315

1416
pub fn criterion_benchmark(c: &mut Criterion) {

src/serialize.rs

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ pub struct SerializeConfig {
1313
pub root_eclasses: Vec<(ArcSort, Value)>,
1414
}
1515

16+
struct Serializer<'a> {
17+
extractor: Extractor<'a>,
18+
node_ids: NodeIDs,
19+
result: egraph_serialize::EGraph,
20+
}
21+
1622
/// Default is used for exporting JSON and will output all nodes.
1723
impl Default for SerializeConfig {
1824
fn default() -> Self {
@@ -136,11 +142,15 @@ impl EGraph {
136142
},
137143
);
138144

139-
let mut egraph = egraph_serialize::EGraph::default();
145+
let mut serializer = Serializer {
146+
extractor: Extractor::new(self, &mut TermDag::default()),
147+
node_ids,
148+
result: egraph_serialize::EGraph::default(),
149+
};
150+
140151
for (func, input, output, class_id, node_id) in all_calls {
141152
self.serialize_value(
142-
&mut egraph,
143-
&mut node_ids,
153+
&mut serializer,
144154
&func.schema.output,
145155
&output.value,
146156
&class_id,
@@ -151,16 +161,10 @@ impl EGraph {
151161
.iter()
152162
.zip(&func.schema.input)
153163
.map(|(v, sort)| {
154-
self.serialize_value(
155-
&mut egraph,
156-
&mut node_ids,
157-
sort,
158-
v,
159-
&self.value_to_class_id(sort, v),
160-
)
164+
self.serialize_value(&mut serializer, sort, v, &self.value_to_class_id(sort, v))
161165
})
162166
.collect();
163-
egraph.nodes.insert(
167+
serializer.result.nodes.insert(
164168
node_id,
165169
egraph_serialize::Node {
166170
op: func.decl.name.to_string(),
@@ -172,13 +176,13 @@ impl EGraph {
172176
);
173177
}
174178

175-
egraph.root_eclasses = config
179+
serializer.result.root_eclasses = config
176180
.root_eclasses
177181
.iter()
178182
.map(|(sort, v)| self.value_to_class_id(sort, v))
179183
.collect();
180184

181-
egraph
185+
serializer.result
182186
}
183187

184188
/// Gets the serialized class ID for a value.
@@ -264,29 +268,31 @@ impl EGraph {
264268
/// When this is called on an input of a node, we only use the node ID to know which node to point to.
265269
fn serialize_value(
266270
&self,
267-
egraph: &mut egraph_serialize::EGraph,
268-
node_ids: &mut NodeIDs,
271+
serializer: &mut Serializer,
269272
sort: &ArcSort,
270273
value: &Value,
271274
class_id: &egraph_serialize::ClassId,
272275
) -> egraph_serialize::NodeId {
273276
let node_id = if sort.is_eq_sort() {
274-
let node_ids = node_ids.entry(class_id.clone()).or_insert_with(|| {
275-
// If we don't find node IDs for this class, it means that all nodes for it were omitted due to size constraints
276-
// In this case, add a dummy node in this class to represent the missing nodes
277-
let node_id = self.to_node_id(Some(sort), SerializedNode::Dummy(*value));
278-
egraph.nodes.insert(
279-
node_id.clone(),
280-
egraph_serialize::Node {
281-
op: "[...]".to_string(),
282-
eclass: class_id.clone(),
283-
cost: NotNan::new(f64::INFINITY).unwrap(),
284-
children: vec![],
285-
subsumed: false,
286-
},
287-
);
288-
VecDeque::from(vec![node_id])
289-
});
277+
let node_ids = serializer
278+
.node_ids
279+
.entry(class_id.clone())
280+
.or_insert_with(|| {
281+
// If we don't find node IDs for this class, it means that all nodes for it were omitted due to size constraints
282+
// In this case, add a dummy node in this class to represent the missing nodes
283+
let node_id = self.to_node_id(Some(sort), SerializedNode::Dummy(*value));
284+
serializer.result.nodes.insert(
285+
node_id.clone(),
286+
egraph_serialize::Node {
287+
op: "[...]".to_string(),
288+
eclass: class_id.clone(),
289+
cost: NotNan::new(f64::INFINITY).unwrap(),
290+
children: vec![],
291+
subsumed: false,
292+
},
293+
);
294+
VecDeque::from(vec![node_id])
295+
});
290296
node_ids.rotate_left(1);
291297
node_ids.front().unwrap().clone()
292298
} else {
@@ -298,28 +304,21 @@ impl EGraph {
298304
.inner_values(value)
299305
.into_iter()
300306
.map(|(s, v)| {
301-
self.serialize_value(
302-
egraph,
303-
node_ids,
304-
&s,
305-
&v,
306-
&self.value_to_class_id(&s, &v),
307-
)
307+
self.serialize_value(serializer, &s, &v, &self.value_to_class_id(&s, &v))
308308
})
309309
.collect();
310310
// If this is a container sort, use the name, otherwise use the value
311311
let op = if sort.is_container_sort() {
312312
sort.serialized_name(value).to_string()
313313
} else {
314-
let mut termdag = TermDag::default();
315-
let extractor = Extractor::new(self, &mut termdag);
314+
let termdag = &mut TermDag::default();
316315
let (_, term) = sort
317-
.extract_term(self, *value, &extractor, &mut termdag)
316+
.extract_term(self, *value, &serializer.extractor, termdag)
318317
.expect("Extraction should be successful since extractor has been fully initialized");
319318

320319
termdag.term_to_expr(&term, Span::Panic).to_string()
321320
};
322-
egraph.nodes.insert(
321+
serializer.result.nodes.insert(
323322
node_id.clone(),
324323
egraph_serialize::Node {
325324
op,
@@ -332,7 +331,7 @@ impl EGraph {
332331
};
333332
node_id
334333
};
335-
egraph.class_data.insert(
334+
serializer.result.class_data.insert(
336335
class_id.clone(),
337336
egraph_serialize::ClassData {
338337
typ: Some(sort.name().to_string()),

0 commit comments

Comments
 (0)