8000 High cpu cost when compare the characters? · Issue #35448 · golang/go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

High cpu cost when compare the characters? #35448

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

Closed
toranger opened this issue Nov 8, 2019 · 3 comments
Closed

High cpu cost when compare the characters? #35448

toranger opened this issue Nov 8, 2019 · 3 comments

Comments

@toranger
Copy link
toranger commented Nov 8, 2019

go version go1.10 linux/amd64.
ps: online version asked, so not changed to latest release.

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tmd/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/tmd/gostation"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build651618122=/tmp/go-build -gno-record-gcc-switches"

What did you do?

compile my project. use the pprof to get profile info.
found out the characters compare waste almost 26.58s.
I used the string compare has the same problem that's why I separate it to compare head and tail
characters, but it seems does not work either.

(pprof) top 10
Showing nodes accounting for 30.58s, 89.78% of 34.06s total
Dropped 240 nodes (cum <= 0.17s)
Showing top 10 nodes out of 73
flat flat% sum% cum cum%
28.12s 82.56% 82.56% 28.14s 82.62% cos-config/common.findEnv
0.68s 2.00% 84.56% 0.68s 2.00% runtime.futex
0.30s 0.88% 85.44% 0.37s 1.09% syscall.Syscall
0.26s 0.76% 86.20% 0.30s 0.88% runtime.step
0.24s 0.7% 86.91% 0.24s 0.7% runtime.memeqbody
0.23s 0.68% 87.58% 0.23s 0.68% runtime._ExternalCode
0.21s 0.62% 88.20% 0.66s 1.94% runtime.gentraceback
0.19s 0.56% 88.76% 0.40s 1.17% runtime.mallocgc
0.19s 0.56% 89.31% 0.49s 1.44% runtime.pcvalue
0.16s 0.47% 89.78% 0.18s 0.53% runtime.findfunc
(pprof) list cos-config/common.findEnv
Total: 34.06s
ROUTINE ======================== cos-config/common.findEnv in /home/tmd/gostation/src/cos-config/common/config.go
28.12s 28.14s (flat, cum) 82.62% of Total
600ms 600ms 359: for i := 0; i < leng; i++ {
900ms 900ms 360: tName := module.Env[i].EnvName
26.58s 26.58s 361: if tName[0] == head && tName[len(tName)-1] == tail {
40ms 60ms 362: if tName == envName {

pprof disasm output file:
disasm.txt

core code:

func findEnv(module *ConfigModule, envName string) (*ConfigEnvironment, bool) {
	if nil == module {
		return nil, false
	}
	head := envName[0]
	tail := envName[len(envName)-1]
	for _, en := range module.Env {
		tName := en.EnvName
		if tName[0] == head && tName[len(tName)-1] == tail {
			if tName == envName {
				return &en, true
			}
		}
	}
	return nil, false
}

What did you expect to see?

low cpu cost

What did you see instead?

high cpu cost

@bcmills
Copy link
Contributor
bcmills commented Nov 8, 2019

go version go1.10

Note that the current release is Go 1.13.4, and only the two most recent major versions are supported (today, Go 1.13 and 1.12).

@bcmills
Copy link
Contributor
bcmills commented Nov 8, 2019

Even so, the code you have provided does not look like something amenable to compiler optimization. In particular, comparing the first and last characters is likely to cause needless cache misses and unaligned reads.

Your program is looking for two strings that are equal: if you want the compiler to use its fastest equality check, you should skip the outer check and use tName == envName directly. If the == operation on strings is unexpectedly slow, that is something we can consider optimizing further.

@bcmills
Copy link
Contributor
bcmills commented Nov 8, 2019

This issue seems to be a question about how to use Go, rather than a feature request or defect report about the Go language and/or toolchain.

We have decided that our experiment to allow questions on the issue tracker has not had the outcome we desired, so I am closing this issue. I'm sorry that we can't answer your question here.

There are many other methods to get help if you're still looking for answers:

Thanks

@bcmills bcmills closed this as completed Nov 8, 2019
@golang golang locked and limited conversation to collaborators Nov 7, 2020
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

3 participants
0