From fad363e64acf8be7c0888a7f3bf399b598591fbb Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 6 Aug 2023 01:26:01 -0600 Subject: [PATCH] shed CodeIndex for control predicates in disjuncts.rs (#1791) --- src/machine/disjuncts.rs | 46 ++++++++++++++++++++++++++-------------- src/parser/ast.rs | 27 ----------------------- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/machine/disjuncts.rs b/src/machine/disjuncts.rs index 6c6d3a7b..a17240f1 100644 --- a/src/machine/disjuncts.rs +++ b/src/machine/disjuncts.rs @@ -530,7 +530,36 @@ impl VariableClassifier { } }; + let mut add_chunk = |classifier: &mut Self, name: Atom, terms: Vec| { + if update_chunk_data(classifier, name, terms.len()) { + build_stack.add_chunk(); + } + + for (arg_c, term) in terms.iter().enumerate() { + classifier.probe_body_term(arg_c + 1, terms.len(), term); + } + + build_stack.push_chunk_term( + clause_to_query_term( + loader, + name, + terms, + classifier.call_policy, + ), + ); + }; + match term { + Term::Clause(_, name @ (atom!("->") | atom!(";") | atom!(",")), mut terms) if terms.len() == 3 => { + if let Some(last_arg) = terms.last() { + if let Term::Literal(_, Literal::CodeIndex(_)) = last_arg { + terms.pop(); + state_stack.push(TraversalState::Term(Term::Clause(Cell::default(), name, terms))); + } else { + add_chunk(self, name, terms); + } + } + } Term::Clause(_, atom!(","), mut terms) if terms.len() == 2 => { let tail = terms.pop().unwrap(); let head = terms.pop().unwrap(); @@ -700,22 +729,7 @@ impl VariableClassifier { self.call_policy = CallPolicy::Counted; } Term::Clause(_, name, terms) => { - if update_chunk_data(self, name, terms.len()) { - build_stack.add_chunk(); - } - - for (arg_c, term) in terms.iter().enumerate() { - self.probe_body_term(arg_c + 1, terms.len(), term); - } - - build_stack.push_chunk_term( - clause_to_query_term( - loader, - name, - terms, - self.call_policy, - ), - ); + add_chunk(self, name, terms); } var @ Term::Var(..) => { if update_chunk_data(self, atom!("call"), 1) { diff --git a/src/parser/ast.rs b/src/parser/ast.rs index fc728782..05ca5a37 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -830,30 +830,3 @@ pub fn unfold_by_str(mut term: Term, s: Atom) -> Vec { terms.push(term); terms } - -fn unfold_by_str_ref_once(term: &Term, s: Atom) -> Option<(&Term, &Term)> { - if let Term::Clause(_, ref name, ref subterms) = term { - if name == &s && subterms.len() == 2 { - let fst = &subterms[0]; - let snd = &subterms[1]; - - return Some((fst, snd)); - } - } - - None -} - -pub fn unfold_by_str_ref(mut term: &Term, s: Atom) -> Vec<&Term> { - let mut terms = vec![]; - - while let Some((fst, snd)) = unfold_by_str_ref_once(&term, s) { - terms.push(fst); - term = snd; - } - - terms.push(term); - terms -} - - -- 2.54.0