From: Mark Thom Date: Mon, 20 Apr 2020 17:37:32 +0000 (-0600) Subject: speed unification of pairs of partial strings X-Git-Tag: v0.8.123~115 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=443c4b6cd02844d8d50aeef68f8670c588b55f09;p=scryer-prolog.git speed unification of pairs of partial strings --- diff --git a/src/prolog/machine/machine_state_impl.rs b/src/prolog/machine/machine_state_impl.rs index 350ca33d..14e4679b 100644 --- a/src/prolog/machine/machine_state_impl.rs +++ b/src/prolog/machine/machine_state_impl.rs @@ -281,19 +281,18 @@ impl MachineState { (Addr::PStrLocation(h1, n1), Addr::PStrLocation(h2, n2)) => { if let &HeapCellValue::PartialString(ref pstr1, has_tail_1) = &self.heap[h1] { if let &HeapCellValue::PartialString(ref pstr2, has_tail_2) = &self.heap[h2] { - let pstr1_iter = pstr1.range_from(n1 ..); - let pstr2_iter = pstr2.range_from(n2 ..); - - let mut m_len = 0; - - for (c1, c2) in pstr1_iter.zip(pstr2_iter) { - if c1 != c2 { + let pstr1_s = pstr1.as_str_from(n1); + let pstr2_s = pstr2.as_str_from(n2); + + let m_len = + if pstr1_s.starts_with(pstr2_s) { + pstr2_s.len() + } else if pstr2_s.starts_with(pstr1_s) { + pstr1_s.len() + } else { self.fail = true; return; - } - - m_len += c1.len_utf8(); - } + }; if pstr1.at_end(n1 + m_len) { if has_tail_1 { @@ -499,19 +498,18 @@ impl MachineState { (Addr::PStrLocation(h1, n1), Addr::PStrLocation(h2, n2)) => { if let &HeapCellValue::PartialString(ref pstr1, has_tail_1) = &self.heap[h1] { if let &HeapCellValue::PartialString(ref pstr2, has_tail_2) = &self.heap[h2] { - let pstr1_iter = pstr1.range_from(n1 ..); - let pstr2_iter = pstr2.range_from(n2 ..); - - let mut m_len = 0; - - for (c1, c2) in pstr1_iter.zip(pstr2_iter) { - if c1 != c2 { + let pstr1_s = pstr1.as_str_from(n1); + let pstr2_s = pstr2.as_str_from(n2); + + let m_len = + if pstr1_s.starts_with(pstr2_s) { + pstr2_s.len() + } else if pstr2_s.starts_with(pstr1_s) { + pstr1_s.len() + } else { self.fail = true; return; - } - - m_len += c1.len_utf8(); - } + }; if pstr1.at_end(n1 + m_len) { if has_tail_1 {