Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort: the Sort function doesn't exit early for short inputs #9793

Closed
cannona opened this issue Feb 6, 2015 · 2 comments
Closed

sort: the Sort function doesn't exit early for short inputs #9793

cannona opened this issue Feb 6, 2015 · 2 comments

Comments

@cannona
Copy link
Contributor

cannona commented Feb 6, 2015

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.

@bradfitz
Copy link
Contributor

bradfitz commented Feb 6, 2015

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.

@bradfitz bradfitz closed this as completed Feb 6, 2015
@cannona
Copy link
Contributor Author

cannona commented Feb 6, 2015

You're right. I had an error in my testing code.

Sorry for the confusion.

Aaron

On 2/6/15, Brad Fitzpatrick notifications@github.com wrote:

Closed #9793.


Reply to this email directly or view it on GitHub:
#9793 (comment)

@mikioh mikioh changed the title Sort: the Sort function doesn't exit early for short inputs sort: the Sort function doesn't exit early for short inputs Feb 7, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants