-
Notifications
You must be signed in to change notification settings - Fork 6
/
sorty_test.go
544 lines (479 loc) · 13.9 KB
/
sorty_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/* Copyright (c) 2019, Serhat Şevki Dinçer.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package sorty
import (
"reflect"
"sort"
"testing"
"time"
"unsafe"
"github.com/jfcg/rng"
"github.com/jfcg/sixb"
)
const (
// will have many equal & distinct elements in buffers
bufFull = 10_000_000
bufHalf = bufFull / 2
maxMaxGor = 3
)
var (
// A & B buffers will hold all slices to sort
aaBuf = make([]uint32, bufFull)
bbBuf = make([]uint32, bufFull)
// source buffer
srcBuf = make([]uint32, bufFull)
tsPtr *testing.T
)
// fill source buffer with random bytes
func fillSrc() {
rng.Fill(sixb.U4toB(srcBuf))
}
// copy, prepare, sort, test
func copyPrepSortTest(buf []uint32, prepare func([]uint32) any,
srf func(any)) (time.Duration, any) {
// copy from srcBuf into buf
if p := &buf[0]; p == &aaBuf[0] || p == &bbBuf[0] {
copy(buf, srcBuf)
} else {
copy(buf, srcBuf[bufHalf:]) // initialize from second half
}
// prepare input if given
// its output type determines the sort type
var ar any
if prepare != nil {
ar = prepare(buf)
} else {
ar = buf
}
// measure duration of sorting
now := time.Now()
srf(ar)
dur := time.Since(now)
isSorted := IsSortedSlice // by value
if !isValueSort(srf) {
isSorted = IsSortedLen // by length
}
// check if result is sorted
if isSorted(ar) != 0 {
_, kind := extractSK(ar)
tsPtr.Fatal("not sorted, kind:", kind)
}
return dur, ar
}
func isValueSort(srf func(any)) bool {
sPtr := reflect.ValueOf(srf).Pointer()
return sPtr == stdSortPtr || sPtr == sortSlcPtr || sPtr == sortLswPtr
}
var (
stdSortPtr = reflect.ValueOf(stdSort).Pointer() // standard sort.Slice
sortSlcPtr = reflect.ValueOf(SortSlice).Pointer() // sorty
sortLswPtr = reflect.ValueOf(sortLsw).Pointer() // sorty
)
func stdSort(ar any) {
slc, kind := extractSK(ar)
switch kind {
case reflect.Float32:
buf := *(*[]float32)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool {
x, y := buf[i], buf[k]
return x < y || NaNoption == NaNlarge && x == x && y != y ||
NaNoption == NaNsmall && x != x && y == y
})
case reflect.Float64:
buf := *(*[]float64)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool {
x, y := buf[i], buf[k]
return x < y || NaNoption == NaNlarge && x == x && y != y ||
NaNoption == NaNsmall && x != x && y == y
})
case reflect.Int32:
buf := *(*[]int32)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool { return buf[i] < buf[k] })
case reflect.Int64:
buf := *(*[]int64)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool { return buf[i] < buf[k] })
case reflect.Uint32:
buf := *(*[]uint32)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool { return buf[i] < buf[k] })
case reflect.Uint64:
buf := *(*[]uint64)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool { return buf[i] < buf[k] })
case reflect.String:
buf := *(*[]string)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool { return buf[i] < buf[k] })
case sliceBias + reflect.Uint8:
buf := *(*[][]byte)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool { return sixb.BtoS(buf[i]) < sixb.BtoS(buf[k]) })
default:
tsPtr.Fatal("unrecognized kind:", kind)
}
}
//go:nosplit
func stdSortLen(ar any) {
slc, kind := extractSK(ar)
switch {
case kind == reflect.String:
buf := *(*[]string)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool { return len(buf[i]) < len(buf[k]) })
case kind >= sliceBias:
buf := *(*[][]byte)(unsafe.Pointer(&slc))
sort.Slice(buf, func(i, k int) bool { return len(buf[i]) < len(buf[k]) })
default:
tsPtr.Fatal("unrecognized kind:", kind)
}
}
func basicCheck(ar, ap any) (slc1, slc2 sixb.Slice, kind reflect.Kind) {
slc1, kind = extractSK(ar)
slc2, kind2 := extractSK(ap)
if kind != kind2 {
tsPtr.Fatal("different kinds:", kind, kind2)
}
if slc1.Len != slc2.Len {
tsPtr.Fatal("length mismatch:", kind, slc1.Len, slc2.Len)
}
if slc1.Data == slc2.Data {
tsPtr.Fatal("same slice data:", kind, slc1.Data)
}
return
}
func compare(ar, ap any) { // by value
slc1, slc2, kind := basicCheck(ar, ap)
var buf1, buf2 []uint64
switch kind {
case reflect.String:
buf1 := *(*[]string)(unsafe.Pointer(&slc1))
buf2 := *(*[]string)(unsafe.Pointer(&slc2))
for i := len(buf1) - 1; i >= 0; i-- {
if buf1[i] != buf2[i] {
tsPtr.Fatal("values mismatch:", kind, i, buf1[i], buf2[i])
}
}
return
case sliceBias + reflect.Uint8:
buf1 := *(*[][]byte)(unsafe.Pointer(&slc1))
buf2 := *(*[][]byte)(unsafe.Pointer(&slc2))
for i := len(buf1) - 1; i >= 0; i-- {
if a, b := sixb.BtoS(buf1[i]), sixb.BtoS(buf2[i]); a != b {
tsPtr.Fatal("values mismatch:", kind, i, a, b)
}
}
return
case reflect.Float32:
buf1 := *(*[]float32)(unsafe.Pointer(&slc1))
buf2 := *(*[]float32)(unsafe.Pointer(&slc2))
for i := len(buf1) - 1; i >= 0; i-- {
a, b := buf1[i], buf2[i]
if a != b && (a == a || b == b) { // consider NaNs equal
tsPtr.Fatal("values mismatch:", kind, i, a, b)
}
}
return
case reflect.Float64:
buf1 := *(*[]float64)(unsafe.Pointer(&slc1))
buf2 := *(*[]float64)(unsafe.Pointer(&slc2))
for i := len(buf1) - 1; i >= 0; i-- {
a, b := buf1[i], buf2[i]
if a != b && (a == a || b == b) { // consider NaNs equal
tsPtr.Fatal("values mismatch:", kind, i, a, b)
}
}
return
case reflect.Int32, reflect.Uint32:
b1 := *(*[]uint32)(unsafe.Pointer(&slc1))
b2 := *(*[]uint32)(unsafe.Pointer(&slc2))
buf1 = sixb.U4toU8(b1)
buf2 = sixb.U4toU8(b2)
case reflect.Int64, reflect.Uint64:
buf1 = *(*[]uint64)(unsafe.Pointer(&slc1))
buf2 = *(*[]uint64)(unsafe.Pointer(&slc2))
default:
tsPtr.Fatal("unrecognized kind:", kind)
}
for i := len(buf1) - 1; i >= 0; i-- {
if buf1[i] != buf2[i] {
tsPtr.Fatal("values mismatch:", kind, i, buf1[i], buf2[i])
}
}
}
func compareLen(ar, ap any) { // by length
slc1, slc2, kind := basicCheck(ar, ap)
switch {
case kind == reflect.String:
buf1 := *(*[]string)(unsafe.Pointer(&slc1))
buf2 := *(*[]string)(unsafe.Pointer(&slc2))
for i := len(buf1) - 1; i >= 0; i-- {
if a, b := len(buf1[i]), len(buf2[i]); a != b {
tsPtr.Fatal("len values mismatch:", kind, i, a, b)
}
}
case kind >= sliceBias:
buf1 := *(*[][]byte)(unsafe.Pointer(&slc1))
buf2 := *(*[][]byte)(unsafe.Pointer(&slc2))
for i := len(buf1) - 1; i >= 0; i-- {
if a, b := len(buf1[i]), len(buf2[i]); a != b {
tsPtr.Fatal("len values mismatch:", kind, i, a, b)
}
}
default:
tsPtr.Fatal("unrecognized kind:", kind)
}
}
// median of four durations
func medur(a, b, c, d time.Duration) time.Duration {
if d < b {
d, b = b, d
}
if c < a {
c, a = a, c
}
if d < c {
c = d
}
if b < a {
b = a
}
return time.Duration(sixb.MeanI8(int64(b), int64(c)))
}
// Calculate median duration of four distinct calls with random inputs to srf().
// Optionally compare each result with standard sort.Slice.
// prepare()'s output type determines the sort type.
func medianCpstCompare(testName string, prepare func([]uint32) any,
srf func(any), compStd bool) float64 {
var std func(any)
var cmp func(any, any)
if compStd {
if isValueSort(srf) {
std = stdSort
cmp = compare // by value
} else {
std = stdSortLen
cmp = compareLen // by length
}
}
dur := [4]time.Duration{}
var ar any
for i := 0; i < len(dur); i++ {
fillSrc()
dur[i], ar = copyPrepSortTest(aaBuf, prepare, srf)
if compStd {
_, ap := copyPrepSortTest(bbBuf, prepare, std)
cmp(ar, ap)
}
}
dur[0] = medur(dur[0], dur[1], dur[2], dur[3])
return printSec(testName, dur[0])
}
var stNames = [4]string{"sorty-1", "sorty-2", "sorty-3", "sorty-4"}
// return sum of sortU4() durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurU4(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(stNames[MaxGor-1], nil, SortSlice, compStd)
}
return
}
func U4toF4(buf []uint32) any {
return *(*[]float32)(unsafe.Pointer(&buf))
}
// return sum of sortF4() durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurF4(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(stNames[MaxGor-1], U4toF4, SortSlice, compStd)
}
return
}
// implant strings into buf
func implantS(buf []uint32) any {
// string size is 4*t bytes
t := sixb.StrSize >> 2
// buf will hold n strings (headers followed by overlapping 12-bytes bodies)
n := (len(buf) - 2) / (t + 1)
t *= n // total string headers space
ss := sixb.U4toStrs(buf[:t:t])
for k := len(buf) - 2; n > 0; {
n--
k--
ss[n].Data = unsafe.Pointer(&buf[k])
ss[n].Len = 12
}
return *(*[]string)(unsafe.Pointer(&ss))
}
// return sum of sortS() durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurS(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(stNames[MaxGor-1], implantS, SortSlice, compStd)
}
return
}
// implant []byte's into buf
func implantB(buf []uint32) any {
// []byte size is 4*t bytes
t := sixb.SliceSize >> 2
// buf will hold n []byte's (headers followed by overlapping 12-bytes bodies)
n := (len(buf) - 2) / (t + 1)
t *= n // total []byte headers space
bs := sixb.U4toSlcs(buf[:t:t])
for k := len(buf) - 2; n > 0; {
n--
k--
bs[n].Data = unsafe.Pointer(&buf[k])
bs[n].Len = 12
bs[n].Cap = 12
}
return *(*[][]byte)(unsafe.Pointer(&bs))
}
// return sum of sortB() durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurB(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(stNames[MaxGor-1], implantB, SortSlice, compStd)
}
return
}
// implant strings into buf for SortLen
func implantLenS(buf []uint32) any {
// string size is 4*t bytes
t := sixb.StrSize >> 2
// buf will hold n string headers
n := len(buf) / t
t *= n // total string headers space
ss := sixb.U4toStrs(buf[:t:t])
for L := 4*uint(len(buf)) + 1; n > 0; {
n--
// string bodies start at &buf[0] with random lengths up to 4*len(buf) bytes
ss[n].Data = unsafe.Pointer(&buf[0])
l := uint(ss[n].Len) // random number from srcBuf
ss[n].Len = int(l % L)
}
return *(*[]string)(unsafe.Pointer(&ss))
}
// return sum of sortLenS() durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurLenS(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(stNames[MaxGor-1], implantLenS, SortLen, compStd)
}
return
}
// implant []byte's into buf
func implantLenB(buf []uint32) any {
// []byte size is 4*t bytes
t := sixb.SliceSize >> 2
// buf will hold n []byte headers
n := len(buf) / t
t *= n // total []byte headers space
bs := sixb.U4toSlcs(buf[:t:t])
for L := 4*uint(len(buf)) + 1; n > 0; {
n--
// []byte bodies start at &buf[0] with random lengths up to 4*len(buf) bytes
bs[n].Data = unsafe.Pointer(&buf[0])
l := uint(bs[n].Len) % L // random number from srcBuf
bs[n].Len = int(l)
bs[n].Cap = int(l)
}
return *(*[][]byte)(unsafe.Pointer(&bs))
}
// return sum of sortLenB() durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurLenB(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(stNames[MaxGor-1], implantLenB, SortLen, compStd)
}
return
}
//go:nosplit
func sortLsw(ar any) {
slc, kind := extractSK(ar)
switch kind {
case reflect.Uint32:
buf := *(*[]uint32)(unsafe.Pointer(&slc))
lsw := func(i, k, r, s int) bool {
if buf[i] < buf[k] {
if r != s {
buf[r], buf[s] = buf[s], buf[r]
}
return true
}
return false
}
Sort(len(buf), lsw)
case reflect.Float32:
buf := *(*[]float32)(unsafe.Pointer(&slc))
lsw := func(i, k, r, s int) bool {
x, y := buf[i], buf[k]
if x < y || NaNoption == NaNlarge && x == x && y != y ||
NaNoption == NaNsmall && x != x && y == y {
if r != s {
buf[r], buf[s] = buf[s], buf[r]
}
return true
}
return false
}
Sort(len(buf), lsw)
case reflect.String:
buf := *(*[]string)(unsafe.Pointer(&slc))
lsw := func(i, k, r, s int) bool {
if buf[i] < buf[k] {
if r != s {
buf[r], buf[s] = buf[s], buf[r]
}
return true
}
return false
}
Sort(len(buf), lsw)
case sliceBias + reflect.Uint8:
buf := *(*[][]byte)(unsafe.Pointer(&slc))
lsw := func(i, k, r, s int) bool {
if sixb.BtoS(buf[i]) < sixb.BtoS(buf[k]) {
if r != s {
buf[r], buf[s] = buf[s], buf[r]
}
return true
}
return false
}
Sort(len(buf), lsw)
default:
tsPtr.Fatal("unrecognized kind:", kind)
}
}
var sLswNames = [4]string{"sortyLsw-1", "sortyLsw-2", "sortyLsw-3", "sortyLsw-4"}
// return sum of Sort([]uint32) durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurLswU4(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(sLswNames[MaxGor-1], nil, sortLsw, compStd)
}
return
}
// return sum of Sort([]float32) durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurLswF4(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(sLswNames[MaxGor-1], U4toF4, sortLsw, compStd)
}
return
}
// return sum of Sort([]string) durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurLswS(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(sLswNames[MaxGor-1], implantS, sortLsw, compStd)
}
return
}
// return sum of Sort([][]byte) durations for 1..maxMaxGor goroutines
// optionally compare with standard sort.Slice
func sumDurLswB(compStd bool) (sum float64) {
for MaxGor = 1; MaxGor <= maxMaxGor; MaxGor++ {
sum += medianCpstCompare(sLswNames[MaxGor-1], implantB, sortLsw, compStd)
}
return
}