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

runtime: improve performance of grow() in mheap.go #25091

Closed
jerrinsg opened this issue Apr 25, 2018 · 3 comments
Closed

runtime: improve performance of grow() in mheap.go #25091

jerrinsg opened this issue Apr 25, 2018 · 3 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Performance
Milestone

Comments

@jerrinsg
Copy link
Contributor

Please answer these questions before submitting your issue. Thanks!

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

go version devel +c5f0104 Wed Apr 25 21:34:15 2018 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jerrin/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jerrin/gocode"
GORACE=""
GOROOT="/work/go"
GOTMPDIR=""
GOTOOLDIR="/work/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-build042307601=/tmp/go-build -gno-record-gcc-switches"

What did you do?

When grow() in mheap.go is called, it sets the span entry for every page in the newly created span. This seems unnecessary as the span is anyway going to be put into the free list/treap, and only the first and last span entry need be set for a free span. The following diff is a suggestion to improve this piece of code. The newly added code is similar to how allocSpanLocked() in mheap.go trims the extra pages and puts it back to the free list/treap.

diff --git a/src/runtime/mheap.go b/src/runtime/mheap.go
index b11853c..3b050cf 100644
--- a/src/runtime/mheap.go
+++ b/src/runtime/mheap.go
@@ -923,14 +923,12 @@ func (h *mheap) grow(npage uintptr) bool {
                return false
        }
 
-       // Create a fake "in use" span and free it, so that the
-       // right coalescing happens.
+       // Create a fake span and free it, so that the right coalescing happens.
        s := (*mspan)(h.spanalloc.alloc())
        s.init(uintptr(v), size/pageSize)
-       h.setSpans(s.base(), s.npages, s)
-       atomic.Store(&s.sweepgen, h.sweepgen)
-       s.state = _MSpanInUse
-       h.pagesInUse += uint64(s.npages)
+       h.setSpan(s.base(), s)
+       h.setSpan(s.base()+s.npages*pageSize-1, s)
+       s.state = _MSpanManual
        h.freeSpanLocked(s, false, true, 0)
        return true
 }
@ianlancetaylor
Copy link
Contributor

Please do not send patches in issues, please send them via Gerrit or GitHub as described at https://golang.org/doc/contribute.html. That will ensure proper licensing and attribution. Thanks.

@gopherbot
Copy link

Change https://golang.org/cl/109417 mentions this issue: runtime: improve performance of grow() in mheap.go

@FiloSottile FiloSottile added Performance NeedsFix The path to resolution is known, but the work has not been done. labels Apr 26, 2018
@FiloSottile FiloSottile added this to the Go1.11 milestone Apr 26, 2018
@bradfitz bradfitz modified the milestones: Go1.11, Unplanned May 18, 2018
@ALTree ALTree added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed NeedsFix The path to resolution is known, but the work has not been done. labels Jul 22, 2020
@ALTree
Copy link
Member

ALTree commented Jul 22, 2020

It appears that the proposed change was rejected by the runtime owner on the ground that it introduced hairiness in a codepath that is apparently not very hot (see discussion on the CL linked above).

Since this issue was opened (more than 2 years ago) mostly as a reference for the proposed change, I believe we can close.

@ALTree ALTree closed this as completed Jul 22, 2020
@golang golang locked and limited conversation to collaborators Jul 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Performance
Projects
None yet
Development

No branches or pull requests

6 participants