Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
sort: the Sort function doesn't exit early for short inputs #9793
In func Sort(data Interface), we already call data.Len(). However we don't check to see if it returned a value < 2. We should, and if so, return immediately. As it is now, without this simple check, we end up allocating memory, regardless of whether or not we can do anything with the item passed.
Adding this check should have an insignificant impact on other calls to this function, since we're just doing a simple test on a variable we already have.
It doesn't look like it ends up allocating any memory or doing any work, at least at tip.
Sort calls quickSort which checks if it has over 7 elements and does quick sort if so (but not for short inputs), then checks if it has over 1 element and does insertion sort (but not for short inputs).
So looks like this is already done.
I'm close this bug, but feel free to point out that I'm wrong if I'm misreading it.