]> gitweb.factorcode.org Git - factor.git/commitdiff
sorting.quick: faster by using stack variables not mutable locals.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 10 Jun 2014 00:53:16 +0000 (17:53 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 10 Jun 2014 00:53:16 +0000 (17:53 -0700)
extra/sorting/quick/quick.factor

index df8d935ddfe28f3e1698ad974f824c0ac7764990..e585cb190f976ed30bf1b52f4baebc89f766f4bf 100644 (file)
@@ -10,24 +10,19 @@ IN: sorting.quick
 
 :: (quicksort) ( seq from to -- )
     from to < [
-        from to + 2/ :> p
-        from :> l!
-        to :> r!
-
-        p seq nth-unsafe :> p-nth
-
-        [ l r <= ] [
-            [ l seq nth-unsafe p-nth before? ] [ l 1 + l! ] while
-            [ r seq nth-unsafe p-nth after? ] [ r 1 - r! ] while
-            l r <= [
-                l r seq exchange-unsafe
-                l 1 + l!
-                r 1 - r!
+        from to + 2/ seq nth-unsafe :> pivot
+
+        from to [ 2dup <= ] [
+            [ over seq nth-unsafe pivot before? ] [ [ 1 + ] dip ] while
+            [ dup  seq nth-unsafe pivot after? ] [ 1 - ] while
+            2dup <= [
+                [ seq exchange-unsafe ]
+                [ [ 1 + ] [ 1 - ] bi* ] 2bi
             ] when
         ] while
 
-        seq from r (quicksort)
-        seq l to (quicksort)
+        [ seq from ] dip (quicksort)
+        [ seq ] dip to (quicksort)
 
     ] when ; inline recursive