Skip to content

Commit 321fd44

Browse files
committed
add
1 parent ae7644e commit 321fd44

File tree

3 files changed

+101
-21
lines changed

3 files changed

+101
-21
lines changed

unsafeConvert.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@ import (
88
"unsafe"
99
)
1010

11-
func Bytes(v string) []byte {
11+
func ByteSlice(v string) []byte {
1212
return unsafe.Slice(unsafe.StringData(v), len(v))
1313
}
1414

15-
func BytesReflect(v string) []byte {
15+
func BytePointer(v string) []byte {
1616
return *(*[]byte)(unsafe.Pointer(&v))
1717
}
1818

19-
func String(v []byte) string {
19+
func ByteCopy(s string) []byte {
20+
buf := make([]byte, len(s))
21+
copy(buf, s)
22+
return buf
23+
}
24+
25+
func StringSlice(v []byte) string {
2026
return unsafe.String(unsafe.SliceData(v), len(v))
2127
}
2228

23-
func StringReflect(v []byte) string {
29+
func StringPointer(v []byte) string {
2430
return *(*string)(unsafe.Pointer(&v))
2531
}
2632

@@ -43,7 +49,7 @@ func StringReflect(v []byte) string {
4349
// system crashes. So before converting, please test in a virtual
4450
// environment whether the type you want to convert can be converted,
4551
// in order to avoid loss.
46-
func STBReflect[T ~string | ~[]byte](v T) string {
52+
func STBPointer[T ~string | ~[]byte](v T) string {
4753
return *(*string)(unsafe.Pointer(&v))
4854
}
4955

unsafeConvert_Benchmark_test.go

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,47 +233,110 @@ The official mailing list for discussion of the Go language is Go Nuts.
233233
234234
Report bugs using the Go issue tracker.`
235235

236-
func Benchmark_StringToBytes_Lite(b *testing.B) {
236+
func Benchmark_ByteToString_G_Lite(b *testing.B) {
237237
b.ResetTimer()
238238
v := "Break a leg"
239239
for i := 0; i < b.N; i++ {
240240
_ = []byte(v)
241241
}
242242
}
243243

244-
func Benchmark_StringToBytes_usC_Lite(b *testing.B) {
244+
func Benchmark_ByteToString_Copy_U_Lite(b *testing.B) {
245245
b.ResetTimer()
246246
v := "Break a leg"
247247
for i := 0; i < b.N; i++ {
248-
_ = unsafeConvert.Bytes(v)
248+
_ = unsafeConvert.ByteCopy(v)
249249
}
250250
}
251251

252-
func Benchmark_StringToBytes_usC_Reflect_Lite(b *testing.B) {
252+
func Benchmark_ByteToString_Slice_U_Lite(b *testing.B) {
253253
b.ResetTimer()
254254
v := "Break a leg"
255255
for i := 0; i < b.N; i++ {
256-
_ = unsafeConvert.BytesReflect(v)
256+
_ = unsafeConvert.ByteSlice(v)
257257
}
258258
}
259259

260-
func Benchmark_StringToBytes_Big(b *testing.B) {
260+
func Benchmark_ByteToString_Pointer_U_Lite(b *testing.B) {
261+
b.ResetTimer()
262+
v := "Break a leg"
263+
for i := 0; i < b.N; i++ {
264+
_ = unsafeConvert.BytePointer(v)
265+
}
266+
}
267+
268+
func Benchmark_ByteToString_G_Big(b *testing.B) {
269+
b.ResetTimer()
270+
for i := 0; i < b.N; i++ {
271+
_ = []byte(biG)
272+
}
273+
}
274+
275+
func Benchmark_ByteToString_Copy_U_Big(b *testing.B) {
276+
b.ResetTimer()
277+
for i := 0; i < b.N; i++ {
278+
_ = unsafeConvert.ByteCopy(biG)
279+
}
280+
}
281+
282+
func Benchmark_ByteToString_Slice_U_Big(b *testing.B) {
283+
b.ResetTimer()
284+
for i := 0; i < b.N; i++ {
285+
_ = unsafeConvert.ByteSlice(biG)
286+
}
287+
}
288+
289+
func Benchmark_BytesToString_U_Pointer_Big(b *testing.B) {
290+
b.ResetTimer()
291+
for i := 0; i < b.N; i++ {
292+
_ = unsafeConvert.BytePointer(biG)
293+
}
294+
}
295+
296+
//
297+
298+
func Benchmark_StringToByte_G_Lite(b *testing.B) {
299+
b.ResetTimer()
300+
v := []byte("Break a leg")
301+
for i := 0; i < b.N; i++ {
302+
_ = string(v)
303+
}
304+
}
305+
306+
func Benchmark_StringToByte_Slice_U_Lite(b *testing.B) {
307+
b.ResetTimer()
308+
v := []byte("Break a leg")
309+
for i := 0; i < b.N; i++ {
310+
_ = unsafeConvert.StringSlice(v)
311+
}
312+
}
313+
314+
315+
func Benchmark_StringToByte_Pointer_U_Lite(b *testing.B) {
316+
b.ResetTimer()
317+
v := []byte("Break a leg")
318+
for i := 0; i < b.N; i++ {
319+
_ = unsafeConvert.StringPointer(v)
320+
}
321+
}
322+
323+
func Benchmark_StringToByte_G_Big(b *testing.B) {
261324
b.ResetTimer()
262325
for i := 0; i < b.N; i++ {
263326
_ = []byte(biG)
264327
}
265328
}
266329

267-
func Benchmark_StringToBytes_usC_Big(b *testing.B) {
330+
func Benchmark_StringToByte_Slice_U_Big(b *testing.B) {
268331
b.ResetTimer()
269332
for i := 0; i < b.N; i++ {
270-
_ = unsafeConvert.Bytes(biG)
333+
_ = unsafeConvert.ByteSlice(biG)
271334
}
272335
}
273336

274-
func Benchmark_StringToBytes_usC_Reflect_Big(b *testing.B) {
337+
func Benchmark_StringToBytes_U_Pointer_Big(b *testing.B) {
275338
b.ResetTimer()
276339
for i := 0; i < b.N; i++ {
277-
_ = unsafeConvert.BytesReflect(biG)
340+
_ = unsafeConvert.BytePointer(biG)
278341
}
279342
}

unsafeConvert_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ type Bs string
4242

4343
func TestTsT(t *testing.T) {
4444
var r Bs = "src"
45-
t.Error(STBReflect(r))
45+
t.Error(STBPointer(r))
4646
t.Error("ok")
4747
}
4848

4949
func TestString(t *testing.T) {
5050
for _, want := range strings {
5151
arg := []byte(want)
52-
got := String(arg)
52+
got := StringSlice(arg)
5353
if got != want {
5454
t.Errorf("String(%q) = %q but want %q",
5555
arg, got, want)
@@ -71,7 +71,18 @@ func eq(a, b []byte) bool {
7171
func TestBytes(t *testing.T) {
7272
for _, arg := range strings {
7373
want := []byte(arg)
74-
got := Bytes(arg)
74+
got := ByteSlice(arg)
75+
if !eq(got, want) {
76+
t.Errorf("Bytes(%q) = %q but want %q",
77+
arg, got, want)
78+
}
79+
}
80+
}
81+
82+
func TestPoolBytes(t *testing.T) {
83+
for _, arg := range strings {
84+
want := []byte(arg)
85+
got := ByteCopy(arg)
7586
if !eq(got, want) {
7687
t.Errorf("Bytes(%q) = %q but want %q",
7788
arg, got, want)
@@ -80,7 +91,7 @@ func TestBytes(t *testing.T) {
8091
}
8192

8293
func TestNil(t *testing.T) {
83-
got := String(nil)
94+
got := StringSlice(nil)
8495
if got != "" {
8596
t.Errorf("String(nil) = %q but want %q", got, "")
8697
}
@@ -104,7 +115,7 @@ func BenchmarkString(b *testing.B) {
104115
name := fmt.Sprintf("String/%d", l)
105116
b.Run(name, func(b *testing.B) {
106117
for n := 0; n < b.N; n++ {
107-
String(arg)
118+
StringSlice(arg)
108119
}
109120
})
110121

@@ -124,7 +135,7 @@ func BenchmarkBytes(b *testing.B) {
124135
name := fmt.Sprintf("Bytes/%d", l)
125136
b.Run(name, func(b *testing.B) {
126137
for n := 0; n < b.N; n++ {
127-
Bytes(arg)
138+
ByteSlice(arg)
128139
}
129140
})
130141

0 commit comments

Comments
 (0)