From 8657a568bb822c56d28063cab394094270cf71f9 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sat, 22 Jan 2022 15:23:59 -0800 Subject: [PATCH] sequences: faster iteration over slices --- basis/grouping/grouping.factor | 2 +- core/sequences/sequences.factor | 43 ++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/basis/grouping/grouping.factor b/basis/grouping/grouping.factor index 750c8eea1e..49890180e0 100644 --- a/basis/grouping/grouping.factor +++ b/basis/grouping/grouping.factor @@ -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 diff --git a/core/sequences/sequences.factor b/core/sequences/sequences.factor index 610630053e..b1524145d4 100644 --- a/core/sequences/sequences.factor +++ b/core/sequences/sequences.factor @@ -397,15 +397,36 @@ PRIVATE> > ] [ 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 ] 2dip 2map-as ; inline -- 2.34.1