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: simple integer computation loop is relatively slow #59366

Closed
keakon opened this issue Apr 1, 2023 · 3 comments
Closed

cmd/compile: simple integer computation loop is relatively slow #59366

keakon opened this issue Apr 1, 2023 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@keakon
Copy link

keakon commented Apr 1, 2023

What version of Go are you using (go version)?

$ go version
go version go1.20.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/keakon/Library/Caches/go-build"
GOENV="/Users/keakon/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/keakon/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/keakon/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/Cellar/go/1.20.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.20.2/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="cc"
CXX="c++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/jw/d0g3224x6tx5dwqrvkl8pp440000gn/T/go-build1704497747=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Build and run them:

$ go run main.go

package main

import (
	"fmt"
	"time"
)

func main() {
	start := time.Now()
	r := uint64(0)
	for i := uint64(0); i < 10000000000; i++ {
		m := i * i
		if r > m {
			r -= m
		} else {
			r += m
		}
	}
	end := time.Now()
	fmt.Println(end.Sub(start), r)
}

$ clang -O3 main.c -o main && ./main

#include <stdio.h>
#include <time.h>

int main() {
	struct timespec start, end;
	unsigned long long r = 0;
	clock_gettime(CLOCK_MONOTONIC, &start);
	for (unsigned long long i = 0; i < 10000000000; i++) {
		unsigned long long m = i * i;
		if (r > m) {
			r -= m;
		} else {
			r += m;
		}
	}
	clock_gettime(CLOCK_MONOTONIC, &end);
	unsigned long long elapsed_time = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000;
	printf("%llu %llu\n", elapsed_time, r);
	return 0;
}

$ zig build-exe -O ReleaseFast main.zig && ./main

const std = @import("std");

pub fn main() void {
    const start = std.time.milliTimestamp();
    var i: u64 = 0;
    var r: u64 = 0;
    while (i < 10000000000) : (i += 1) {
        var m = i * i;
        if (r > m) {
            r -= m;
        } else {
            r += m;
        }
    }
    const end = std.time.milliTimestamp();
    std.debug.print("{d} {d}\n", .{ end - start, r });
}

What did you expect to see?

Go should run as fast as C and Zig.

What did you see instead?

Go spent over 9 seconds, C (clang 14) and Zig (0.10.1) spent about 4 seconds.

@bcmills bcmills added Performance compiler/runtime Issues related to the Go compiler and/or runtime. labels Apr 3, 2023
@bcmills bcmills added this to the Backlog milestone Apr 3, 2023
@mknyszek mknyszek changed the title Go runs 56% slower than C and Zig in this case cmd/compile: simple integer computation loop is relatively slow Apr 3, 2023
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 3, 2023
@mknyszek
Copy link
Contributor

mknyszek commented Apr 3, 2023

Seems like identifying differences in assembly output should quickly show why.

CC @golang/compiler

@renthraysk
Copy link

Looks like the difference comes from the ability to loop unroll.
See issue #51302

@mknyszek
Copy link
Contributor

mknyszek commented Apr 4, 2023

@renthraysk Thanks for looking into it. Closing as a duplicate of #51302.

@mknyszek mknyszek closed this as not planned Won't fix, can't repro, duplicate, stale Apr 4, 2023
@golang golang locked and limited conversation to collaborators Apr 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

5 participants