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

mountinfo_openbsd: Rewrite int8SliceToString with unsafe #117

Closed
wants to merge 1 commit into from

Conversation

Red54
Copy link
Contributor

@Red54 Red54 commented May 19, 2022

Signed-off-by: 谢致邦 (XIE Zhibang) <Yeking@Red54.com>
@kolyshkin
Copy link
Collaborator

I have submitted https://go-review.googlesource.com/c/sys/+/407195 and hope it will be merged soon. Let's wait a couple of days.

@kolyshkin
Copy link
Collaborator

The alternative is using something like BytePtrToString((*byte)(unsafe.Pointer(&is)))

@kolyshkin kolyshkin marked this pull request as draft May 19, 2022 18:53
@Red54
Copy link
Contributor Author

Red54 commented May 20, 2022

package main

import (
        "testing"
        "unsafe"

        "golang.org/x/sys/unix"
)

var is = [90]int8{'a', 'b', 'c'}
var es = "abc"

func int8SliceToString(is []int8) string {
        for i := range is {
                if is[i] == 0 {
                        is = is[:i]
                        break
                }
        }
        return *(*string)(unsafe.Pointer(&is))
}

func TestingInt8SliceToString(t *testing.T) {
        s := int8SliceToString(is[:])
        if s != es {
                t.Fail()
        }
}

func BenchmarkInt8SliceToString(b *testing.B) {
        for i := 0; i < b.N; i++ {
                int8SliceToString(is[:])
        }
}

func TestingBytePtrToString(t *testing.T) {
        s := unix.BytePtrToString((*byte)(unsafe.Pointer(&is)))
        if s != es {
                t.Fail()
        }
}

func BenchmarkBytePtrToString(b *testing.B) {
        for i := 0; i < b.N; i++ {
                unix.BytePtrToString((*byte)(unsafe.Pointer(&is)))
        }
}
$ go test -bench=. -benchmem  
goos: openbsd
goarch: amd64
pkg: bench
cpu: Intel Core Processor (Skylake, IBRS)
BenchmarkInt8SliceToString-2    458439867                2.585 ns/op           0 B/op          0 allocs/op
BenchmarkBytePtrToString-2      194815534                6.158 ns/op           0 B/op          0 allocs/op
PASS
ok      bench   3.317s
$

BytePtrToString((*byte)(unsafe.Pointer(&is))) is more than twice as slow.

@Red54
Copy link
Contributor Author

Red54 commented May 20, 2022

I have submitted https://go-review.googlesource.com/c/go/+/407436 and if it is merged there is no need to split bsd into freebsdlike and openbsd.

@kolyshkin
Copy link
Collaborator

Guess we should go with what I outlined in #114 (comment) instead

@kolyshkin
Copy link
Collaborator

Closing in favor of #118

@kolyshkin kolyshkin closed this Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants