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

Open
jerrinsg opened this Issue Apr 25, 2018 · 2 comments

Comments

Projects
None yet
5 participants
@jerrinsg
Contributor

jerrinsg commented Apr 25, 2018

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

This comment has been minimized.

Contributor

ianlancetaylor commented Apr 25, 2018

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

This comment has been minimized.

gopherbot commented Apr 25, 2018

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

@FiloSottile FiloSottile added this to the Go1.11 milestone Apr 26, 2018

@bradfitz bradfitz modified the milestones: Go1.11, Unplanned May 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment