Skip to content

Commit 847ac55

Browse files
committed
avoid copying programs
1 parent 6beec31 commit 847ac55

6 files changed

Lines changed: 98 additions & 101 deletions

File tree

cozo-core/src/data/program.rs

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl TempSymbGen {
184184
}
185185
}
186186

187-
#[derive(Debug, Clone)]
187+
#[derive(Debug)]
188188
pub(crate) enum InputInlineRulesOrAlgo {
189189
Rules { rules: Vec<InputInlineRule> },
190190
Algo { algo: AlgoApply },
@@ -209,20 +209,6 @@ pub(crate) struct AlgoApply {
209209
pub(crate) algo_impl: Box<dyn AlgoImpl>,
210210
}
211211

212-
impl Clone for AlgoApply {
213-
fn clone(&self) -> Self {
214-
Self {
215-
algo: self.algo.clone(),
216-
rule_args: self.rule_args.clone(),
217-
options: self.options.clone(),
218-
head: self.head.clone(),
219-
arity: self.arity,
220-
span: self.span,
221-
algo_impl: self.algo.get_impl().unwrap(),
222-
}
223-
}
224-
}
225-
226212
impl AlgoApply {
227213
pub(crate) fn arity(&self) -> Result<usize> {
228214
self.algo_impl.arity(&self.options, &self.head, self.span)
@@ -239,7 +225,6 @@ impl Debug for AlgoApply {
239225
}
240226
}
241227

242-
#[derive(Clone)]
243228
pub(crate) struct MagicAlgoApply {
244229
pub(crate) algo: AlgoHandle,
245230
pub(crate) rule_args: Vec<MagicAlgoRuleArg>,
@@ -325,7 +310,6 @@ impl Debug for MagicAlgoApply {
325310
}
326311
}
327312

328-
#[derive(Clone)]
329313
pub(crate) enum AlgoRuleArg {
330314
InMem {
331315
name: Symbol,
@@ -374,7 +358,7 @@ impl Display for AlgoRuleArg {
374358
}
375359
}
376360

377-
#[derive(Debug, Clone)]
361+
#[derive(Debug)]
378362
pub(crate) enum MagicAlgoRuleArg {
379363
InMem {
380364
name: MagicSymbol,
@@ -415,7 +399,7 @@ impl MagicAlgoRuleArg {
415399
}
416400
}
417401

418-
#[derive(Debug, Clone)]
402+
#[derive(Debug)]
419403
pub(crate) struct InputProgram {
420404
pub(crate) prog: BTreeMap<Symbol, InputInlineRulesOrAlgo>,
421405
pub(crate) out_opts: QueryOutOptions,
@@ -568,9 +552,12 @@ impl InputProgram {
568552

569553
Err(NoEntryError.into())
570554
}
571-
pub(crate) fn to_normalized_program(&self, tx: &SessionTx<'_>) -> Result<NormalFormProgram> {
555+
pub(crate) fn into_normalized_program(
556+
self,
557+
tx: &SessionTx<'_>,
558+
) -> Result<(NormalFormProgram, QueryOutOptions)> {
572559
let mut prog: BTreeMap<Symbol, _> = Default::default();
573-
for (k, rules_or_algo) in &self.prog {
560+
for (k, rules_or_algo) in self.prog {
574561
match rules_or_algo {
575562
InputInlineRulesOrAlgo::Rules { rules } => {
576563
let mut collected_rules = vec![];
@@ -581,7 +568,7 @@ impl InputProgram {
581568
Symbol::new(&format!("***{}", counter) as &str, span)
582569
};
583570
let normalized_body = InputAtom::Conjunction {
584-
inner: rule.body.clone(),
571+
inner: rule.body,
585572
span: rule.span,
586573
}
587574
.disjunctive_normal_form(tx)?;
@@ -631,23 +618,18 @@ impl InputProgram {
631618
);
632619
}
633620
InputInlineRulesOrAlgo::Algo { algo: algo_apply } => {
634-
prog.insert(
635-
k.clone(),
636-
NormalFormAlgoOrRules::Algo {
637-
algo: algo_apply.clone(),
638-
},
639-
);
621+
prog.insert(k.clone(), NormalFormAlgoOrRules::Algo { algo: algo_apply });
640622
}
641623
}
642624
}
643-
Ok(NormalFormProgram { prog })
625+
Ok((NormalFormProgram { prog }, self.out_opts))
644626
}
645627
}
646628

647-
#[derive(Debug, Clone)]
629+
#[derive(Debug)]
648630
pub(crate) struct StratifiedNormalFormProgram(pub(crate) Vec<NormalFormProgram>);
649631

650-
#[derive(Debug, Clone)]
632+
#[derive(Debug)]
651633
pub(crate) enum NormalFormAlgoOrRules {
652634
Rules { rules: Vec<NormalFormInlineRule> },
653635
Algo { algo: AlgoApply },
@@ -662,15 +644,15 @@ impl NormalFormAlgoOrRules {
662644
}
663645
}
664646

665-
#[derive(Debug, Clone, Default)]
647+
#[derive(Debug, Default)]
666648
pub(crate) struct NormalFormProgram {
667649
pub(crate) prog: BTreeMap<Symbol, NormalFormAlgoOrRules>,
668650
}
669651

670-
#[derive(Debug, Clone)]
652+
#[derive(Debug)]
671653
pub(crate) struct StratifiedMagicProgram(pub(crate) Vec<MagicProgram>);
672654

673-
#[derive(Debug, Clone)]
655+
#[derive(Debug)]
674656
pub(crate) enum MagicRulesOrAlgo {
675657
Rules { rules: Vec<MagicInlineRule> },
676658
Algo { algo: MagicAlgoApply },
@@ -697,7 +679,7 @@ impl MagicRulesOrAlgo {
697679
}
698680
}
699681

700-
#[derive(Debug, Clone)]
682+
#[derive(Debug)]
701683
pub(crate) struct MagicProgram {
702684
pub(crate) prog: BTreeMap<MagicSymbol, MagicRulesOrAlgo>,
703685
}
@@ -815,22 +797,22 @@ impl MagicSymbol {
815797
}
816798
}
817799

818-
#[derive(Debug, Clone)]
800+
#[derive(Debug)]
819801
pub(crate) struct InputInlineRule {
820802
pub(crate) head: Vec<Symbol>,
821803
pub(crate) aggr: Vec<Option<(Aggregation, Vec<DataValue>)>>,
822804
pub(crate) body: Vec<InputAtom>,
823805
pub(crate) span: SourceSpan,
824806
}
825807

826-
#[derive(Debug, Clone)]
808+
#[derive(Debug)]
827809
pub(crate) struct NormalFormInlineRule {
828810
pub(crate) head: Vec<Symbol>,
829811
pub(crate) aggr: Vec<Option<(Aggregation, Vec<DataValue>)>>,
830812
pub(crate) body: Vec<NormalFormAtom>,
831813
}
832814

833-
#[derive(Debug, Clone)]
815+
#[derive(Debug)]
834816
pub(crate) struct MagicInlineRule {
835817
pub(crate) head: Vec<Symbol>,
836818
pub(crate) aggr: Vec<Option<(Aggregation, Vec<DataValue>)>>,
@@ -852,7 +834,6 @@ impl MagicInlineRule {
852834
}
853835
}
854836

855-
#[derive(Clone)]
856837
pub(crate) enum InputAtom {
857838
Rule {
858839
inner: InputRuleApplyAtom,

cozo-core/src/query/compile.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,37 +98,32 @@ struct ArityMismatch(String, usize, usize, #[label] SourceSpan);
9898
impl<'a> SessionTx<'a> {
9999
pub(crate) fn stratified_magic_compile(
100100
&mut self,
101-
prog: &StratifiedMagicProgram,
101+
prog: StratifiedMagicProgram,
102102
) -> Result<Vec<CompiledProgram>> {
103-
// let mut stores: BTreeMap<MagicSymbol, InMemRelation> = Default::default();
104-
let mut store_arities: BTreeMap<&MagicSymbol, usize> = Default::default();
103+
let mut store_arities: BTreeMap<MagicSymbol, usize> = Default::default();
105104

106105
for stratum in prog.0.iter() {
107106
for (name, ruleset) in &stratum.prog {
108-
// stores.insert(
109-
// name.clone(),
110-
// self.new_rule_store(ruleset.arity()?),
111-
// );
112-
store_arities.insert(name, ruleset.arity()?);
107+
store_arities.insert(name.clone(), ruleset.arity()?);
113108
}
114109
}
115110

116111
let compiled: Vec<_> = prog
117112
.0
118-
.iter()
113+
.into_iter()
119114
.rev()
120115
.map(|cur_prog| -> Result<CompiledProgram> {
121116
cur_prog
122117
.prog
123-
.iter()
118+
.into_iter()
124119
.map(|(k, body)| -> Result<(MagicSymbol, CompiledRuleSet)> {
125120
match body {
126121
MagicRulesOrAlgo::Rules { rules: body } => {
127122
let mut collected = Vec::with_capacity(body.len());
128123
for rule in body.iter() {
129124
let header = &rule.head;
130125
let mut relation =
131-
self.compile_magic_rule_body(rule, k, &store_arities, header)?;
126+
self.compile_magic_rule_body(rule, &k, &store_arities, header)?;
132127
relation.fill_binding_indices().with_context(|| {
133128
format!(
134129
"error encountered when filling binding indices for {:#?}",
@@ -141,11 +136,11 @@ impl<'a> SessionTx<'a> {
141136
contained_rules: rule.contained_rules(),
142137
})
143138
}
144-
Ok((k.clone(), CompiledRuleSet::Rules(collected)))
139+
Ok((k, CompiledRuleSet::Rules(collected)))
145140
}
146141

147142
MagicRulesOrAlgo::Algo { algo: algo_apply } => {
148-
Ok((k.clone(), CompiledRuleSet::Algo(algo_apply.clone())))
143+
Ok((k, CompiledRuleSet::Algo(algo_apply)))
149144
}
150145
}
151146
})
@@ -158,7 +153,7 @@ impl<'a> SessionTx<'a> {
158153
&mut self,
159154
rule: &MagicInlineRule,
160155
rule_name: &MagicSymbol,
161-
store_arities: &BTreeMap<&MagicSymbol, usize>,
156+
store_arities: &BTreeMap<MagicSymbol, usize>,
162157
ret_vars: &[Symbol],
163158
) -> Result<RelAlgebra> {
164159
let mut ret = RelAlgebra::unit(rule_name.symbol().span);

cozo-core/src/query/stratify.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ fn make_scc_reduced_graph<'a>(
215215

216216
impl NormalFormProgram {
217217
/// returns the stratified program and the store lifetimes of the intermediate relations
218-
pub(crate) fn stratify(
218+
pub(crate) fn into_stratified_program(
219219
self,
220220
) -> Result<(StratifiedNormalFormProgram, BTreeMap<MagicSymbol, usize>)> {
221221
// prerequisite: the program is already in disjunctive normal form
@@ -256,7 +256,8 @@ impl NormalFormProgram {
256256
.flat_map(|(stratum, indices)| indices.into_iter().map(move |idx| (idx, stratum)))
257257
.collect::<BTreeMap<_, _>>();
258258
// 7. translate the stratification into datalog program
259-
let mut ret: Vec<NormalFormProgram> = vec![Default::default(); n_strata];
259+
let mut ret: Vec<NormalFormProgram> =
260+
(0..n_strata).map(|_| Default::default()).collect_vec();
260261

261262
let mut store_lifetimes = BTreeMap::new();
262263
for (fr, tos) in &stratified_graph {

0 commit comments

Comments
 (0)