/
bufcpy.go
172 lines (148 loc) · 4.53 KB
/
bufcpy.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
package bufcpy
// #include <string.h>
import "C"
import (
"bytes"
"unsafe"
)
// Copy functions
//
func NativeCopy(to, from []byte) {
copy(to, from)
}
func CgoMemcpy(to, from []byte) {
// seriously this can't be a good idea
C.memcpy(unsafe.Pointer(&(to[0])), unsafe.Pointer(&(from[0])), C.size_t(len(to)))
}
// recursive divide and conquer copy
// 2**(depth+1) == number of partitions
func RecursiveDacCopy(to, from []byte, depth int) {
done, h, depth := make(chan int, 2), len(to)/2, depth-1
if depth > 0 {
go func() { RecursiveDacCopy(to[:h], from[:h], depth); done <- 1 }()
go func() { RecursiveDacCopy(to[h:], from[h:], depth); done <- 1 }()
} else {
go func() { copy(to[:h], from[:h]); done <- 1 }()
go func() { copy(to[h:], from[h:]); done <- 1 }()
}
<-done
<-done
}
func RecursiveDacCgoMemcpy(to, from []byte, depth int) {
done, h, depth := make(chan int, 2), len(to)/2, depth-1
if depth > 0 {
go func() { RecursiveDacCgoMemcpy(to[:h], from[:h], depth); done <- 1 }()
go func() { RecursiveDacCgoMemcpy(to[h:], from[h:], depth); done <- 1 }()
} else {
go func() { CgoMemcpy(to[:h], from[:h]); done <- 1 }()
go func() { CgoMemcpy(to[h:], from[h:]); done <- 1 }()
}
<-done
<-done
}
func PartitionedCopy(to, from []byte, parts int) {
done, lento, chunk := make(chan int, parts), len(to), len(to)/parts
// using from-beginning slicing to ensure the center portion
// of the slice range aligns with our chunk size
offset := len(to) % chunk
if offset > 0 {
go func() { copy(to[:offset], from[:offset]); done <- 1 }()
}
for i, end := offset, lento; i < end; i += chunk {
go func(c int) { copy(to[c:c+chunk], from[c:c+chunk]); done <- 1 }(i)
}
for i := 0; i < parts; i++ {
<-done
}
}
func PartitionedCgoMemcpy(to, from []byte, parts int) {
done, lento, chunk := make(chan int, parts), len(to), len(to)/parts
// using from-beginning slicing to ensure the center portion
// of the slice range aligns with our chunk size
offset := len(to) % chunk
if offset > 0 {
go func() { CgoMemcpy(to[:offset], from[:offset]); done <- 1 }()
}
for i, end := offset, lento; i < end; i += chunk {
go func(c int) { CgoMemcpy(to[c:c+chunk], from[c:c+chunk]); done <- 1 }(i)
}
for i := 0; i < parts; i++ {
<-done
}
}
// Compare functions
//
func CgoMemcmp(to, from []byte) bool {
// seriously this can't be a good idea
return C.memcmp(unsafe.Pointer(&(to[0])), unsafe.Pointer(&(from[0])), C.size_t(len(to))) == 0
}
func PartitionedEqual(to, from []byte, parts int) bool {
if len(to) != len(from) {
return false
}
match, lento, chunk := make(chan bool, parts), len(to), len(to)/parts
// using from-beginning slicing to ensure the center portion
// of the slice range aligns with our chunk size
offset := len(to) % chunk
if offset > 0 {
go func() { match <- bytes.Equal(to[:offset], from[:offset]) }()
}
for i, end := offset, lento; i < end; i += chunk {
go func(c int) { match <- bytes.Equal(to[c:c+chunk], from[c:c+chunk]) }(i)
}
for i := 0; i < parts; i++ {
if !<-match {
return false
}
}
return true
}
func PartitionedCgoMemcmp(to, from []byte, parts int) bool {
if len(to) != len(from) {
return false
}
match, lento, chunk := make(chan bool, parts), len(to), len(to)/parts
// using from-beginning slicing to ensure the center portion
// of the slice range aligns with our chunk size
offset := len(to) % chunk
if offset > 0 {
go func() { match <- CgoMemcmp(to[:offset], from[:offset]) }()
}
for i, end := offset, lento; i < end; i += chunk {
go func(c int) { match <- CgoMemcmp(to[c:c+chunk], from[c:c+chunk]) }(i)
}
for i := 0; i < parts; i++ {
if !<-match {
return false
}
}
return true
}
func RecursiveDacEqual(to, from []byte, depth int) bool {
match, h, depth := make(chan bool, 2), len(to)/2, depth-1
if depth > 0 {
go func() { match <- RecursiveDacEqual(to[:h], from[:h], depth) }()
go func() { match <- RecursiveDacEqual(to[h:], from[h:], depth) }()
} else {
go func() { match <- bytes.Equal(to[:h], from[:h]) }()
go func() { match <- bytes.Equal(to[h:], from[h:]) }()
}
if !<-match || !<-match {
return false
}
return true
}
func RecursiveDacCgoMemcmp(to, from []byte, depth int) bool {
match, h, depth := make(chan bool, 2), len(to)/2, depth-1
if depth > 0 {
go func() { match <- RecursiveDacCgoMemcmp(to[:h], from[:h], depth) }()
go func() { match <- RecursiveDacCgoMemcmp(to[h:], from[h:], depth) }()
} else {
go func() { match <- CgoMemcmp(to[:h], from[:h]) }()
go func() { match <- CgoMemcmp(to[h:], from[h:]) }()
}
if !<-match || !<-match {
return false
}
return true
}