]> Repositorios git - scryer-prolog.git/commitdiff
deduplicate index ptr inlining for 0-arity atoms (#1538)
authorMark Thom <[email protected]>
Sat, 23 Jul 2022 19:16:02 +0000 (13:16 -0600)
committerMark Thom <[email protected]>
Thu, 27 Oct 2022 05:36:07 +0000 (23:36 -0600)
src/heap_iter.rs
src/machine/loader.rs

index f3b36334c9ff70125be42448f246e5b4eda09864..9760be9ea75dc406fbb5e2a341cd374cf0de99f8 100644 (file)
@@ -351,12 +351,33 @@ impl<Iter: FocusedHeapIter> Iterator for PostOrderIterator<Iter> {
 }
 
 impl<Iter: FocusedHeapIter> FocusedHeapIter for PostOrderIterator<Iter> {
-    #[inline]
+    #[inline(always)]
     fn focus(&self) -> usize {
         self.focus
     }
 }
 
+impl<Iter: FocusedHeapIter> PostOrderIterator<Iter> {
+    /* return true if the term at heap offset idx_loc is a
+     * direct/inlined subterm of a structure at the focus of
+     * self.stack.last(). this function is used to determine, e.g.,
+     * ownership of inlined code indices.
+     */
+    #[inline]
+    pub(crate) fn direct_subterm_of_str(&self, idx_loc: usize) -> bool {
+        if let Some((_child_count, item, focus)) = self.parent_stack.last() {
+            read_heap_cell!(item,
+                (HeapCellValueTag::Atom, (_name, arity)) => {
+                    return focus + arity >= idx_loc && *focus < idx_loc;
+                }
+                _ => {}
+            );
+        }
+
+        false
+    }
+}
+
 pub(crate) type LeftistPostOrderHeapIter<'a> = PostOrderIterator<StackfulPreOrderHeapIter<'a>>;
 
 impl<'a> LeftistPostOrderHeapIter<'a> {
index 0cc93f506b225f521320c7df067b6664ea949460..7c9b380ce1fc00b4c07c8616ca8d6748946d136f 100644 (file)
@@ -517,11 +517,17 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
                         let value = iter.heap[h + arity + 1];
 
                         if let Some(idx) = get_structure_index(value) {
-                            term_stack.push(
-                                Term::Literal(Cell::default(), Literal::CodeIndex(idx))
-                            );
+                            // in the second condition, arity == 0,
+                            // meaning idx cannot pertain to this atom
+                            // if it is the direct subterm of a larger
+                            // structure.
+                            if arity > 0 || !iter.direct_subterm_of_str(h) {
+                                term_stack.push(
+                                    Term::Literal(Cell::default(), Literal::CodeIndex(idx))
+                                );
 
-                            arity += 1;
+                                arity += 1;
+                            }
                         }
                     }