From 13df2fe3e8de2344df18c5707498a280458ca8c5 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 4 Aug 2005 16:58:07 +0000 Subject: [PATCH] marginally faster nappend --- TODO.FACTOR.txt | 3 --- library/collections/sequences-epilogue.factor | 13 +++++++------ library/generic/tuple.factor | 4 ++-- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/TODO.FACTOR.txt b/TODO.FACTOR.txt index 188e4f1734..df05155ba2 100644 --- a/TODO.FACTOR.txt +++ b/TODO.FACTOR.txt @@ -65,7 +65,6 @@ - intrinsic char-slot set-char-slot integer-slot set-integer-slot - [ [ dup call ] dup call ] infer hangs - declarations -- optimize away arithmetic dispatch - the invalid recursion form case needs to be fixed, for inlines too - #jump-f #jump-f-label - re-introduce #target-label => #target optimization @@ -75,8 +74,6 @@ + sequences - array sort -- nappend: instead of using push, enlarge the sequence with set-length - then add set the elements with set-nth - specialized arrays + kernel: diff --git a/library/collections/sequences-epilogue.factor b/library/collections/sequences-epilogue.factor index 8f591ad928..8bd2d7fada 100644 --- a/library/collections/sequences-epilogue.factor +++ b/library/collections/sequences-epilogue.factor @@ -112,9 +112,13 @@ M: object >list ( seq -- list ) dup length 0 rot (>list) ; : remove ( obj list -- list ) [ = not ] subset-with ; : remq ( obj list -- list ) [ eq? not ] subset-with ; -: nappend ( s1 s2 -- ) - #! Destructively append s2 to s1. - [ over push ] each drop ; +: copy-into ( start to from -- ) + dup length [ >r pick r> + pick set-nth ] 2each 2drop ; + +: nappend ( to from -- ) + >r dup length swap r> + over length over length + pick set-length + copy-into ; : append ( s1 s2 -- s1+s2 ) #! Outputs a new sequence of the same type as s1. @@ -223,9 +227,6 @@ M: object reverse ( seq -- seq ) [ ] keep like ; dup pick index dup -1 = [ drop ] [ nip pick nth ] ifte ] map 2nip ; -: copy-into ( to from -- ) - dup length [ pick set-nth ] 2each drop ; - IN: kernel : depth ( -- n ) diff --git a/library/generic/tuple.factor b/library/generic/tuple.factor index b18751f9cf..4e9622471f 100644 --- a/library/generic/tuple.factor +++ b/library/generic/tuple.factor @@ -148,9 +148,9 @@ M: mirror set-nth ( n mirror -- elt ) M: mirror length ( mirror -- len ) mirror-tuple array-capacity ; -: literal-tuple ( list -- tuple ) +: literal-tuple ( seq -- tuple ) dup first "tuple-size" word-prop - [ swap copy-into ] keep ; + [ 0 swap rot copy-into ] keep ; : clone-tuple ( tuple -- tuple ) #! Make a shallow copy of a tuple, without cloning its