Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sort/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ func MultithreadedSortComparators(comparators Comparators) Comparators {
copy(toBeSorted, comparators)

var wg sync.WaitGroup
chunks := chunk(toBeSorted, int64(runtime.NumCPU()))

numCPU := int64(runtime.NumCPU())
if numCPU%2 == 1 { // single core machine
numCPU++
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this works. Can you explain?

Either way, it would be nice to wrap our NumCPU() calls in some utility function that would do this for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're just checking to see if there are an odd number of CPUs. In most cases a user will have 1, 2 and then some multiple of 2 thereafter. If someone intentionally disabled a CPU to get an odd number (someone who has more than one core) this will also even that out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and AMD at least went through a phase where they sold triple-core processors. I actually have one at home in my desktop; fourth core unlocked and overclocked of course 😄


chunks := chunk(toBeSorted, numCPU)
wg.Add(len(chunks))
for i := 0; i < len(chunks); i++ {
go func(i int) {
Expand Down