From f5ded007c6575f2c65932f397ce3140d77effc14 Mon Sep 17 00:00:00 2001
From: Alex Lyon <arcterus@mail.com>
Date: Sun, 13 May 2018 16:16:48 -0700
Subject: [PATCH] stdlib: recurse into the smaller partition when sorting

---
 src/stdlib/src/sort.rs | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/stdlib/src/sort.rs b/src/stdlib/src/sort.rs
index 79d1e9489..7602285f6 100644
--- a/src/stdlib/src/sort.rs
+++ b/src/stdlib/src/sort.rs
@@ -46,10 +46,17 @@ fn introsort_helper(
                 break;
             } else {
                 let (left, right) = partition(base, nel, width, comp);
-                introsort_helper(base, left, width, maxdepth, comp);
-                base = unsafe { base.add((right + 1) * width) };
-                nel -= right + 1;
+                let right_base = unsafe { base.add((right + 1) * width) };
+                let right_nel = nel - (right + 1);
                 maxdepth -= 1;
+                if left < nel - right {
+                    introsort_helper(base, left, width, maxdepth, comp);
+                    base = right_base;
+                    nel = right_nel;
+                } else {
+                    introsort_helper(right_base, right_nel, width, maxdepth, comp);
+                    nel = left;
+                }
             }
         }
     }
-- 
GitLab