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

cgo: getting unsafe.Pointer of a slice slower in go 1.6 vs 1.5.3 #15172

Closed
piotrnar opened this issue Apr 7, 2016 · 3 comments
Closed

cgo: getting unsafe.Pointer of a slice slower in go 1.6 vs 1.5.3 #15172

piotrnar opened this issue Apr 7, 2016 · 3 comments

Comments

@piotrnar
Copy link

piotrnar commented Apr 7, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6 windows/amd64
  2. What operating system and processor architecture are you using (go env)?
    I tried on Windows 7 and all kinds of Linux OS.
    I only tried on 64-bit hosts.
  3. What did you do?
    Just run this program with go 1.6 and see how slow it is, comparing to running it with 1.5.3
package main

/*
void alloc_ptr(void *c) {
    return;
}

*/
import "C"

import (
    "unsafe"
    "time"
)

type data_ptr_t unsafe.Pointer

func gcc_AllocPtr(v []byte) {
    C.alloc_ptr(unsafe.Pointer(&v[0]))
}

func main() {
    source_buf := make([]byte, 1024)
    start := time.Now()
    for i:=0; i<1e6; i++ {
        gcc_AllocPtr(source_buf)
    }
    println("done in", time.Now().Sub(start).String())
}

  1. What did you expect to see?

done in 228.0131ms

  1. What did you see instead?

done in 6.0343452s

@piotrnar
Copy link
Author

piotrnar commented Apr 7, 2016

I also want to add that there is an easy workaround:

package main

/*
void alloc_ptr(void *c) {
    return;
}

*/
import "C"

import (
    "unsafe"
    "time"
)

type data_ptr_t unsafe.Pointer

func gcc_AllocPtr(v []byte) {
    ptr := unsafe.Pointer(&v[0])
    C.alloc_ptr(ptr)
}

func main() {
    source_buf := make([]byte, 1024)
    start := time.Now()
    for i:=0; i<1e6; i++ {
        gcc_AllocPtr(source_buf)
    }
    println("done in", time.Now().Sub(start).String())
}

@bradfitz bradfitz changed the title Getting unsafe.Pointer of a slice deadly slow in go 1.6 vs 1.5.3 cgo: getting unsafe.Pointer of a slice slower in go 1.6 vs 1.5.3 Apr 7, 2016
@bradfitz bradfitz added this to the Unplanned milestone Apr 7, 2016
@bradfitz
Copy link
Contributor

bradfitz commented Apr 7, 2016

/cc @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

Dup of #14387 .

@golang golang locked and limited conversation to collaborators Apr 13, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants