| 1 |
package qsort |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"math/rand" |
| 6 |
"reflect" |
| 7 |
"sort" |
| 8 |
"strconv" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
var prng = rand.New(rand.NewSource(0)) |
| 13 |
|
| 14 |
// Note, "8", "16", "32" etc are all byte measurements, not bits. So a 32 byte |
| 15 |
// integer, for example, which you might see in e.g. a SHA256 hash. |
| 16 |
|
| 17 |
func TestSort8(t *testing.T) { |
| 18 |
testSort(t, 8) |
| 19 |
} |
| 20 |
|
| 21 |
func TestSort16(t *testing.T) { |
| 22 |
testSort(t, 16) |
| 23 |
} |
| 24 |
|
| 25 |
func TestSort24(t *testing.T) { |
| 26 |
testSort(t, 24) |
| 27 |
} |
| 28 |
|
| 29 |
func TestSort32(t *testing.T) { |
| 30 |
testSort(t, 32) |
| 31 |
} |
| 32 |
|
| 33 |
func testSort(t *testing.T, size int) { |
| 34 |
const ( |
| 35 |
iterations = 1000 |
| 36 |
minCount = 0 |
| 37 |
maxCount = 1000 |
| 38 |
) |
| 39 |
|
| 40 |
buf := make([]byte, maxCount*size) |
| 41 |
// A first test to validate that the swap function is called properly: |
| 42 |
prng.Read(buf) |
| 43 |
|
| 44 |
values := make([]byte, len(buf)) |
| 45 |
copy(values, buf) |
| 46 |
|
| 47 |
tmp := make([]byte, size) |
| 48 |
Sort(buf, size, func(i, j int) { |
| 49 |
vi := values[i*size : (i+1)*size] |
| 50 |
vj := values[j*size : (j+1)*size] |
| 51 |
copy(tmp, vi) |
| 52 |
copy(vi, vj) |
| 53 |
copy(vj, tmp) |
| 54 |
}) |
| 55 |
|
| 56 |
if !bytes.Equal(buf, values) { |
| 57 |
t.Fatal("values were not sorted correctly by the swap function") |
| 58 |
} |
| 59 |
|
| 60 |
for i := 0; i < iterations; i++ { |
| 61 |
count := randint(minCount, maxCount) |
| 62 |
slice := buf[:count*size] |
| 63 |
prng.Read(slice) |
| 64 |
|
| 65 |
// Test with/without duplicates. |
| 66 |
repeat := randint(0, count) |
| 67 |
for j := repeat; repeat > 0 && j < len(slice) && j+repeat < len(slice); j += repeat { |
| 68 |
copy(slice[j:j+repeat], slice[:repeat]) |
| 69 |
} |
| 70 |
|
| 71 |
expect := values[:len(slice)] |
| 72 |
copy(expect, slice) |
| 73 |
sort.Sort(newGeneric(expect, size, nil)) |
| 74 |
|
| 75 |
if !sort.IsSorted(newGeneric(expect, size, nil)) { |
| 76 |
t.Fatal("reference implementation did not produce a sorted output") |
| 77 |
} |
| 78 |
|
| 79 |
Sort(slice, size, nil) |
| 80 |
|
| 81 |
if !reflect.DeepEqual(expect, slice) { |
| 82 |
t.Fatal("buffer was not sorted correctly") |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestPivot8(t *testing.T) { |
| 88 |
lo := uint64(1) |
| 89 |
mid := uint64(2) |
| 90 |
hi := uint64(3) |
| 91 |
|
| 92 |
for i := 0; i < 1000; i++ { |
| 93 |
input := []uint64{lo, mid, hi} |
| 94 |
rand.Shuffle(3, func(i, j int) { |
| 95 |
input[i], input[j] = input[j], input[i] |
| 96 |
}) |
| 97 |
medianOfThree64(input, 3, nil) |
| 98 |
if input[0] != mid { |
| 99 |
t.Fatal("medianOfThree128 did not put pivot in first position") |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestPivot16(t *testing.T) { |
| 105 |
lo := uint128{lo: 1} |
| 106 |
mid := uint128{lo: 2} |
| 107 |
hi := uint128{lo: 3} |
| 108 |
|
| 109 |
for i := 0; i < 1000; i++ { |
| 110 |
input := []uint128{lo, mid, hi} |
| 111 |
rand.Shuffle(3, func(i, j int) { |
| 112 |
input[i], input[j] = input[j], input[i] |
| 113 |
}) |
| 114 |
medianOfThree128(input, 3, nil) |
| 115 |
if input[0] != mid { |
| 116 |
t.Fatal("medianOfThree128 did not put pivot in first position") |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func TestPivot24(t *testing.T) { |
| 122 |
lo := uint192{lo: 1} |
| 123 |
mid := uint192{lo: 2} |
| 124 |
hi := uint192{lo: 3} |
| 125 |
|
| 126 |
for i := 0; i < 1000; i++ { |
| 127 |
input := []uint192{lo, mid, hi} |
| 128 |
rand.Shuffle(3, func(i, j int) { |
| 129 |
input[i], input[j] = input[j], input[i] |
| 130 |
}) |
| 131 |
medianOfThree192(input, 3, nil) |
| 132 |
if input[0] != mid { |
| 133 |
t.Fatal("medianOfThree192 did not put pivot in first position") |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestPivot32(t *testing.T) { |
| 139 |
lo := uint256{d: 1} |
| 140 |
mid := uint256{d: 2} |
| 141 |
hi := uint256{d: 3} |
| 142 |
|
| 143 |
for i := 0; i < 1000; i++ { |
| 144 |
input := []uint256{lo, mid, hi} |
| 145 |
rand.Shuffle(3, func(i, j int) { |
| 146 |
input[i], input[j] = input[j], input[i] |
| 147 |
}) |
| 148 |
medianOfThree256(input, 3, nil) |
| 149 |
if input[0] != mid { |
| 150 |
t.Fatal("medianOfThree256 did not put pivot in first position") |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func randint(lo, hi int) int { |
| 156 |
if hi == lo { |
| 157 |
return lo |
| 158 |
} |
| 159 |
return prng.Intn(hi-lo) + lo |
| 160 |
} |
| 161 |
|
| 162 |
func BenchmarkSort8(b *testing.B) { |
| 163 |
for _, count := range []int{1e3, 1e4, 1e5, 1e6} { |
| 164 |
b.Run("random-"+strconv.Itoa(count), benchSort(count, 8, 0, random, nil)) |
| 165 |
if count > 1e4 { |
| 166 |
b.Run("partially-ordered(10)-"+strconv.Itoa(count), benchSort(count, 8, 10, random, nil)) |
| 167 |
b.Run("partially-ordered(100)-"+strconv.Itoa(count), benchSort(count, 8, 100, random, nil)) |
| 168 |
b.Run("partially-ordered(1000)-"+strconv.Itoa(count), benchSort(count, 8, 1000, random, nil)) |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func stdlibSort8(b *testing.B, size int) { |
| 174 |
// 8 bytes per int64 |
| 175 |
b.SetBytes(8 * int64(size)) |
| 176 |
data := make([]int64, size) |
| 177 |
unsorted := make([]int64, size) |
| 178 |
for j := 0; j < len(unsorted); j++ { |
| 179 |
unsorted[j] = int64(rand.Intn(size / 10)) |
| 180 |
} |
| 181 |
b.StopTimer() |
| 182 |
for i := 0; i < b.N; i++ { |
| 183 |
copy(data, unsorted) |
| 184 |
b.StartTimer() |
| 185 |
sort.Slice(data, func(i, j int) bool { return data[i] < data[j] }) |
| 186 |
b.StopTimer() |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func stdlibSort8PartiallySorted(b *testing.B, size int, partitions int) { |
| 191 |
// 8 bytes per int64 |
| 192 |
b.SetBytes(8 * int64(size)) |
| 193 |
data := make([]int64, size) |
| 194 |
// panic if not a whole number |
| 195 |
partitionSize := int(size / partitions) |
| 196 |
partitionOrder := rand.Perm(partitions) |
| 197 |
groupedPartitions := make([][]int64, partitions) |
| 198 |
|
| 199 |
for i := 0; i < len(groupedPartitions); i++ { |
| 200 |
partition := make([]int64, partitionSize) |
| 201 |
for j := 0; j < len(partition); j++ { |
| 202 |
partition[j] = int64(rand.Intn(size / 10)) |
| 203 |
} |
| 204 |
sort.Slice(partition, func(i, j int) bool { return partition[i] < partition[j] }) |
| 205 |
groupedPartitions[partitionOrder[i]] = partition |
| 206 |
} |
| 207 |
|
| 208 |
partiallyOrdered := make([]int64, size) |
| 209 |
for _, partition := range groupedPartitions { |
| 210 |
partiallyOrdered = append(partiallyOrdered, partition...) |
| 211 |
} |
| 212 |
|
| 213 |
b.StopTimer() |
| 214 |
for i := 0; i < b.N; i++ { |
| 215 |
copy(data, partiallyOrdered) |
| 216 |
b.StartTimer() |
| 217 |
sort.Slice(data, func(i, j int) bool { return data[i] < data[j] }) |
| 218 |
b.StopTimer() |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
func BenchmarkStdlibSort8(b *testing.B) { |
| 223 |
for _, size := range []int{1e5, 1e6} { |
| 224 |
b.Run("random-"+strconv.Itoa(size), func(b *testing.B) { |
| 225 |
stdlibSort8(b, size) |
| 226 |
}) |
| 227 |
b.Run("partially-sorted(10)-"+strconv.Itoa(size), func(b *testing.B) { |
| 228 |
stdlibSort8PartiallySorted(b, size, 10) |
| 229 |
}) |
| 230 |
b.Run("partially-sorted(100)-"+strconv.Itoa(size), func(b *testing.B) { |
| 231 |
stdlibSort8PartiallySorted(b, size, 100) |
| 232 |
}) |
| 233 |
b.Run("partially-sorted(1000)-"+strconv.Itoa(size), func(b *testing.B) { |
| 234 |
stdlibSort8PartiallySorted(b, size, 1000) |
| 235 |
}) |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
func BenchmarkSort8Indirect(b *testing.B) { |
| 240 |
swap := func(int, int) {} |
| 241 |
const count = 100000 |
| 242 |
b.Run("random", benchSort(count, 8, 0, random, swap)) |
| 243 |
b.Run("asc", benchSort(count, 8, 0, asc, swap)) |
| 244 |
b.Run("desc", benchSort(count, 8, 0, desc, swap)) |
| 245 |
} |
| 246 |
|
| 247 |
func BenchmarkSort16(b *testing.B) { |
| 248 |
for _, count := range []int{1e3, 1e4, 1e5, 1e6} { |
| 249 |
b.Run(strconv.Itoa(count), benchSort(count, 16, 0, random, nil)) |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
func BenchmarkSort16Indirect(b *testing.B) { |
| 254 |
swap := func(int, int) {} |
| 255 |
const count = 100000 |
| 256 |
b.Run("random", benchSort(count, 16, 0, random, swap)) |
| 257 |
b.Run("asc", benchSort(count, 16, 0, asc, swap)) |
| 258 |
b.Run("desc", benchSort(count, 16, 0, desc, swap)) |
| 259 |
} |
| 260 |
|
| 261 |
func BenchmarkSort24(b *testing.B) { |
| 262 |
for _, count := range []int{1e3, 1e4, 1e5, 1e6} { |
| 263 |
b.Run(strconv.Itoa(count), benchSort(count, 24, 0, random, nil)) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
func BenchmarkSort24Indirect(b *testing.B) { |
| 268 |
swap := func(int, int) {} |
| 269 |
const count = 100000 |
| 270 |
b.Run("random", benchSort(count, 24, 0, random, swap)) |
| 271 |
b.Run("asc", benchSort(count, 24, 0, asc, swap)) |
| 272 |
b.Run("desc", benchSort(count, 24, 0, desc, swap)) |
| 273 |
} |
| 274 |
|
| 275 |
func BenchmarkSort32(b *testing.B) { |
| 276 |
for _, count := range []int{1e3, 1e4, 1e5, 1e6} { |
| 277 |
b.Run(strconv.Itoa(count), benchSort(count, 0, 32, random, nil)) |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
func BenchmarkSort32Indirect(b *testing.B) { |
| 282 |
swap := func(int, int) {} |
| 283 |
const count = 100000 |
| 284 |
b.Run("random", benchSort(count, 32, 0, random, swap)) |
| 285 |
b.Run("asc", benchSort(count, 32, 0, asc, swap)) |
| 286 |
b.Run("desc", benchSort(count, 32, 0, desc, swap)) |
| 287 |
} |
| 288 |
|
| 289 |
type order int |
| 290 |
|
| 291 |
const ( |
| 292 |
random order = iota |
| 293 |
asc |
| 294 |
desc |
| 295 |
partiallyOrdered |
| 296 |
) |
| 297 |
|
| 298 |
func benchSort(count, size, partitions int, order order, indirect func(int, int)) func(*testing.B) { |
| 299 |
return func(b *testing.B) { |
| 300 |
b.StopTimer() |
| 301 |
buf := make([]byte, count*size) |
| 302 |
unsorted := make([]byte, count*size) |
| 303 |
prng.Read(unsorted) |
| 304 |
|
| 305 |
if order == asc || order == desc { |
| 306 |
sort.Sort(newGeneric(unsorted, size, nil)) |
| 307 |
} |
| 308 |
if order == desc { |
| 309 |
g := newGeneric(unsorted, size, nil) |
| 310 |
items := g.Len() |
| 311 |
for i := 0; i < items/2; i++ { |
| 312 |
g.Swap(i, items-1-i) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if order == partiallyOrdered { |
| 317 |
// panic if not a whole number |
| 318 |
partitionSize := int((count * size) / partitions) |
| 319 |
partitionOrder := rand.Perm(partitions) |
| 320 |
groupedPartitions := make([][]byte, partitions) |
| 321 |
|
| 322 |
for i := 0; i < len(groupedPartitions); i++ { |
| 323 |
partition := make([]byte, partitionSize) |
| 324 |
sort.Sort(newGeneric(partition, partitionSize, nil)) |
| 325 |
groupedPartitions[partitionOrder[i]] = partition |
| 326 |
} |
| 327 |
|
| 328 |
for _, partition := range groupedPartitions { |
| 329 |
unsorted = append(unsorted, partition...) |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
b.SetBytes(int64(len(buf))) |
| 334 |
|
| 335 |
for i := 0; i < b.N; i++ { |
| 336 |
copy(buf, unsorted) |
| 337 |
b.StartTimer() |
| 338 |
Sort(buf, size, indirect) |
| 339 |
b.StopTimer() |
| 340 |
} |
| 341 |
} |
| 342 |
} |