Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve UnsafeBytes with Go 1.20+ #64

Merged
merged 4 commits into from
Feb 9, 2024
Merged

Improve UnsafeBytes with Go 1.20+ #64

merged 4 commits into from
Feb 9, 2024

Conversation

Fenny
Copy link
Member

@Fenny Fenny commented Feb 9, 2024

func UnsafeBytes(s string) []byte {
    if s == "" {
        return nil
    }
    return (*[MaxStringLen]byte)(unsafe.Pointer(
        (*reflect.StringHeader)(unsafe.Pointer(&s)).Data),
    )[:len(s):len(s)]
}
//Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.8708 ns/op          0 B/op          0 allocs/op
//Benchmark_UnsafeBytes/unsafe-24                 1000000000               1.043 ns/op           0 B/op          0 allocs/op
//Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.8187 ns/op          0 B/op          0 allocs/op
//Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.7915 ns/op          0 B/op          0 allocs/op

func UnsafeBytes(s string) []byte {
    return unsafe.Slice(unsafe.StringData(s), len(s))
}
//Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.3992 ns/op          0 B/op          0 allocs/op
//Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.3953 ns/op          0 B/op          0 allocs/op
//Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.3975 ns/op          0 B/op          0 allocs/op
//Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.3997 ns/op          0 B/op          0 allocs/op

** Before
```
Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.8708 ns/op          0 B/op          0 allocs/op
Benchmark_UnsafeBytes/unsafe-24                 1000000000               1.043 ns/op           0 B/op          0 allocs/op
Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.8187 ns/op          0 B/op          0 allocs/op
Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.7915 ns/op          0 B/op          0 allocs/op
```

**After
```
Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.3992 ns/op          0 B/op          0 allocs/op
Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.3953 ns/op          0 B/op          0 allocs/op
Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.3975 ns/op          0 B/op          0 allocs/op
Benchmark_UnsafeBytes/unsafe-24                 1000000000               0.3997 ns/op          0 B/op          0 allocs/op
```
@Fenny Fenny merged commit 230c181 into gofiber:master Feb 9, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant