]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: faster iteration over slices
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 22 Jan 2022 23:23:59 +0000 (15:23 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 22 Jan 2022 23:27:53 +0000 (15:27 -0800)
basis/grouping/grouping.factor
core/sequences/sequences.factor

index 750c8eea1eae15341b474a3878619e31ed49b6ce..49890180e03d166d1227714629ca84c7cb94f05e 100644 (file)
@@ -74,7 +74,7 @@ PRIVATE>
         2 = [
             [ first2-unsafe ] dip call
         ] [
-            [ [ first-unsafe 1 ] [ setup-each ] bi ] dip
+            [ [ first-unsafe 1 ] [ setup-each [ + ] 2dip ] bi ] dip
             '[ @ _ keep swap ] (all-integers?) nip
         ] if
     ] if ; inline
index 610630053e197f44e1c49830a89caab8b3893945..b1524145d4af27eedc2633a0935b9f25df5d784c 100644 (file)
@@ -397,15 +397,36 @@ PRIVATE>
 
 <PRIVATE
 
-: setup-each ( seq -- n quot )
+! slow-each versions don't unslice
+
+: setup-slow-each ( seq -- n quot )
     [ length check-length ] keep [ nth-unsafe ] curry ; inline
 
-: (each) ( seq quot -- n quot' )
+: (slow-each) ( seq quot -- n quot' )
+    [ setup-slow-each ] dip compose ; inline
+
+: (slow-each-index) ( seq quot -- n quot' )
+    [ setup-slow-each [ keep ] curry ] dip compose ; inline
+
+: setup-each ( seq -- i n quot )
+    dup slice? [
+        [ from>> ] [ to>> ] [ seq>> ] tri
+    ] [
+        [ length check-length 0 swap ] keep
+    ] if [ nth-unsafe ] curry ; inline
+
+: (each) ( seq quot -- i n quot' )
     [ setup-each ] dip compose ; inline
 
-: (each-index) ( seq quot -- n quot' )
+: (each-from) ( seq quot i -- i n quot' )
+    [ (each) ] dip [ + ] curry 2dip ; inline
+
+: (each-index) ( seq quot -- i n quot' )
     [ setup-each [ keep ] curry ] dip compose ; inline
 
+: (each-index-from) ( seq quot i -- i n quot' )
+    [ (each-index) ] dip [ + ] curry 2dip ; inline
+
 : (collect) ( quot into -- quot' )
     [ [ keep ] dip set-nth-unsafe ] 2curry ; inline
 
@@ -413,7 +434,7 @@ PRIVATE>
     (collect) each-integer ; inline
 
 : map-into ( seq quot into -- )
-    [ (each) ] dip collect ; inline
+    [ (slow-each) ] dip collect ; inline
 
 : 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
     [ nth-unsafe ] bi-curry@ bi ; inline
@@ -438,7 +459,7 @@ PRIVATE>
     over [ dupd nth-unsafe ] [ drop f ] if ; inline
 
 : (find) ( seq quot quot' -- i elt )
-    pick [ [ (each) ] dip call ] dip finish-find ; inline
+    pick [ [ (slow-each) ] dip call ] dip finish-find ; inline
 
 : (find-from) ( n seq quot quot' -- i elt )
     [ 2dup bounds-check? ] 2dip
@@ -447,7 +468,7 @@ PRIVATE>
     if ; inline
 
 : (find-index) ( seq quot quot' -- i elt )
-    pick [ [ (each-index) ] dip call ] dip finish-find ; inline
+    pick [ [ (slow-each-index) ] dip call ] dip finish-find ; inline
 
 : (find-index-from) ( n seq quot quot' -- i elt )
     [ 2dup bounds-check? ] 2dip
@@ -464,10 +485,10 @@ PRIVATE>
 PRIVATE>
 
 : each ( ... seq quot: ( ... x -- ... ) -- ... )
-    (each) each-integer ; inline
+    (each) (each-integer) ; inline
 
 : each-from ( ... seq quot: ( ... x -- ... ) i -- ... )
-    -rot (each) (each-integer) ; inline
+    (each-from) (each-integer) ; inline
 
 : reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
     swapd each ; inline
@@ -476,7 +497,7 @@ PRIVATE>
     overd [ [ collect ] keep ] new-like ; inline
 
 : map-as ( ... seq quot: ( ... elt -- ... newelt ) exemplar -- ... newseq )
-    [ (each) ] dip map-integers ; inline
+    [ (slow-each) ] dip map-integers ; inline
 
 : map ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
     over map-as ; inline
@@ -557,7 +578,7 @@ PRIVATE>
     [ find-integer ] (find-index) ; inline
 
 : all? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
-    (each) all-integers? ; inline
+    (each) (all-integers?) ; inline
 
 : push-if ( ..a elt quot: ( ..a elt -- ..b ? ) accum -- ..b )
     [ keep ] dip rot [ push ] [ 2drop ] if ; inline
@@ -618,7 +639,7 @@ PRIVATE>
     [ dup ] swap [ keep ] curry produce nip ; inline
 
 : each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
-    (each-index) each-integer ; inline
+    (each-index) (each-integer) ; inline
 
 : map-index-as ( ... seq quot: ( ... elt index -- ... newelt ) exemplar -- ... newseq )
     [ dup length <iota> ] 2dip 2map-as ; inline