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

cmd/compile: access slice is slow #29805

Closed
egmkang opened this issue Jan 18, 2019 · 4 comments
Closed

cmd/compile: access slice is slow #29805

egmkang opened this issue Jan 18, 2019 · 4 comments

Comments

@egmkang
Copy link

@egmkang egmkang commented Jan 18, 2019

type VectorInt struct {
	data     []int
	size     int
	capacity int
}

func (this *VectorInt) Set(i int, v int) {
	if i >= this.size {
		panic("out of bound!")
	}
	this.data[i] = v
}

func (this *VectorInt) At(i int) int {
	if i >= this.size {
		panic("out of bound!")
	}
	return this.data[i]
}

func (this *VectorInt) Push(v int) {
	if this.size < this.capacity {
		this.data[this.size] = v
		this.size++
		return
	}
	this.data = append(this.data, v)
	this.size++
	this.capacity = len(this.data)
}

func (this *VectorInt) Length() int {
	return this.size
}

func (this *VectorInt) Clear() {
	this.size = 0
}

func NewVectorInt(capacity int) *VectorInt {
	return &VectorInt{data: make([]int, capacity), size: 0, capacity: capacity}
}

//!!!BUG!!!
func BenchmarkVectorInt_Push(b *testing.B) {
	b.N = 10000 * 10000
	vec := NewVectorInt(b.N + 1)
	for index := 0; index < b.N; index++ {
		vec.Push(index)
	}
}

func push(a []int, v int, index *int) {
	if *index < len(a) {
		a[*index] = v
		*index = *index + 1
	}
}

//1-2ns
func BenchmarkSliceAppend(b *testing.B) {
	b.N = 10000 * 10000
	s := make([]int, b.N+1)
	i := 0
	for index := 0; index < b.N; index++ {
		push(s, index, &i)
	}
}

here is my codes.

On Windows 7 64bit, go version go1.11.4 windows/amd64. the VectorInt.Push is very slow, it costs about 12ns.

but on Win10 about 2ns, Linux about 2ns

@ianlancetaylor ianlancetaylor changed the title access slice is slow cmd/compile: access slice is slow Jan 18, 2019
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jan 18, 2019

You seem to be implementing a slice on top of a slice. Every bounds check is going to be done twice. Your benchmark using push is just a normal slice. Your benchmark using vec.Push does multiple bounds checks. Of course vec.Push is going to be slower.

@bradfitz

This comment has been minimized.

Copy link
Contributor

@bradfitz bradfitz commented Jan 18, 2019

I don't see a bug here, so I'm going to close this.

If you want to discuss writing & profiling Go code, let's bring this to the mailing list or other forum. See https://golang.org/wiki/Questions.

@bradfitz bradfitz closed this Jan 18, 2019
@egmkang

This comment has been minimized.

Copy link
Author

@egmkang egmkang commented Jan 19, 2019

so, why only win7 is very slow, the some code, same hardware.

is this not a bug?

wtf

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jan 19, 2019

There are many possible reasons. Windows 7 is almost 10 years old and spending time to investigate whether there is a problem on Windows 7 is not going to be a priority for the Go team. Even if there is a problem there is a good chance that it has something to do with details of the OS and that there is nothing we can do to fix it. As suggested above, I recommend that you investigate this in a forum. If you find a specific problem, we can reconsider.

@golang golang locked and limited conversation to collaborators Jan 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.