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: Optimization of array indexing #7545

Closed
gopherbot opened this issue Mar 14, 2014 · 2 comments
Closed

cmd/compile: Optimization of array indexing #7545

gopherbot opened this issue Mar 14, 2014 · 2 comments

Comments

@gopherbot
Copy link

by nicolas.riesco:

Before filing a bug, please check whether it has been fixed since the
latest release. Search the issue tracker and check that you're running the
latest version of Go:

Run "go version" and compare against
http://golang.org/doc/devel/release.html  If a newer version of Go exists,
install it and retry what you did to reproduce the problem.

Thanks.

What does 'go version' print?
go version devel +08dcdcdb757b Thu Mar 13 14:04:29 2014 -0700 linux/amd64

What steps reproduce the problem?
I'm attaching a file with benchmarks to illustrate how the optimization can be achieved.
In few words, I'm proposing that loops as this one:

    for j := range s {
        s[j].X, s[j].Y, s[j].Z = 1, 2, 3
    }

could be optimised by the compiler and implemented as:

    for j := range s {
        p := &s[j]
        p.X, p.Y, p.Z = 1, 2, 3
    }

What happened?

The unoptimised benchmarks:

    BenchmarkLoop       10000        182743 ns/op
    BenchmarkLoopLen       10000        170704 ns/op
    BenchmarkLoopRange       10000        170395 ns/op

run slower than the optimised ones:

    BenchmarkLoopPointer       10000        136950 ns/op
    BenchmarkLoopPointerRange       10000        111240 ns/op      

What should have happened instead?



Please provide any additional information below.

This optimization is more important that removing bound checks. I've run the benchmarks
with and without bound checks and the effect of this optimization is still important:

$ go test -bench=.  loops_test.go
testing: warning: no tests to run
PASS
BenchmarkLoop       10000        182743 ns/op
BenchmarkLoopPointer       10000        136950 ns/op
BenchmarkLoopLen       10000        170704 ns/op
BenchmarkLoopRange       10000        170395 ns/op
BenchmarkLoopPointerRange       10000        111240 ns/op
ok      command-line-arguments    7.802s

$ go test -bench=. -gcflags=-B loops_test.go
testing: warning: no tests to run
PASS
BenchmarkLoop       10000        151952 ns/op
BenchmarkLoopPointer       10000        123730 ns/op
BenchmarkLoopLen       10000        134355 ns/op
BenchmarkLoopRange       10000        134049 ns/op
BenchmarkLoopPointerRange       10000        110534 ns/op
ok      command-line-arguments    6.620s

Attachments:

  1. loops_test.go (4393 bytes)
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-none.

@bradfitz bradfitz removed the new label Dec 18, 2014
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title cmd/gc: Optimization of array indexing cmd/compile: Optimization of array indexing Jun 8, 2015
@ALTree
Copy link
Member

ALTree commented Jan 19, 2017

Fixed by the SSA backend, lifting the pointer is no longer faster.

BenchmarkLoop-4               	   10000	    111573 ns/op
BenchmarkLoopPointer-4        	   10000	    110875 ns/op

BenchmarkLoopRange-4          	   20000	     84981 ns/op
BenchmarkLoopPointerRange-4   	   20000	     84309 ns/op

@ALTree ALTree closed this as completed Jan 19, 2017
@golang golang locked and limited conversation to collaborators Jan 19, 2018
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

5 participants