From 814c5058bbbd70e706b0305d823e83aa0112d5a4 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Wed, 18 Sep 2019 14:11:28 +0000 Subject: [PATCH] runtime: remove useless heap_objects accounting This change removes useless additional heap_objects accounting for large objects. heap_objects is computed from scratch at ReadMemStats time (which stops the world) by using nlargealloc and nlargefree, so mutating heap_objects turns out to be pointless. As a result, the "large" parameter on "mheap_.freeSpan" is no longer necessary and so this change cleans that up too. Change-Id: I7d6b486d9b57c018e3db46221d81b55fe4c1b021 Reviewed-on: https://go-review.googlesource.com/c/go/+/196637 Run-TryBot: Michael Knyszek TryBot-Result: Gobot Gobot Reviewed-by: Austin Clements --- src/runtime/mcentral.go | 2 +- src/runtime/mgcsweep.go | 2 +- src/runtime/mheap.go | 10 +--------- src/runtime/mstats.go | 5 ++++- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/runtime/mcentral.go b/src/runtime/mcentral.go index cd5901054a..2f97b7d094 100644 --- a/src/runtime/mcentral.go +++ b/src/runtime/mcentral.go @@ -243,7 +243,7 @@ func (c *mcentral) freeSpan(s *mspan, preserve bool, wasempty bool) bool { c.nonempty.remove(s) unlock(&c.lock) - mheap_.freeSpan(s, false) + mheap_.freeSpan(s) return true } diff --git a/src/runtime/mgcsweep.go b/src/runtime/mgcsweep.go index 580de7a715..b95c7f13a4 100644 --- a/src/runtime/mgcsweep.go +++ b/src/runtime/mgcsweep.go @@ -386,7 +386,7 @@ func (s *mspan) sweep(preserve bool) bool { s.limit = 0 // prevent mlookup from finding this span sysFault(unsafe.Pointer(s.base()), size) } else { - mheap_.freeSpan(s, true) + mheap_.freeSpan(s) } c.local_nlargefree++ c.local_largefree += size diff --git a/src/runtime/mheap.go b/src/runtime/mheap.go index c2a23267bc..f9039f78ca 100644 --- a/src/runtime/mheap.go +++ b/src/runtime/mheap.go @@ -926,7 +926,6 @@ func (h *mheap) alloc_m(npage uintptr, spanclass spanClass, large bool) *mspan { // update stats, sweep lists h.pagesInUse += uint64(npage) if large { - memstats.heap_objects++ mheap_.largealloc += uint64(s.elemsize) mheap_.nlargealloc++ atomic.Xadd64(&memstats.heap_live, int64(npage<<_PageShift)) @@ -1201,10 +1200,7 @@ func (h *mheap) grow(npage uintptr) bool { } // Free the span back into the heap. -// -// large must match the value of large passed to mheap.alloc. This is -// used for accounting. -func (h *mheap) freeSpan(s *mspan, large bool) { +func (h *mheap) freeSpan(s *mspan) { systemstack(func() { mp := getg().m lock(&h.lock) @@ -1218,10 +1214,6 @@ func (h *mheap) freeSpan(s *mspan, large bool) { bytes := s.npages << _PageShift msanfree(base, bytes) } - if large { - // Match accounting done in mheap.alloc. - memstats.heap_objects-- - } if gcBlackenEnabled != 0 { // heap_scan changed. gcController.revise() diff --git a/src/runtime/mstats.go b/src/runtime/mstats.go index 09dbb26735..a6866e3a15 100644 --- a/src/runtime/mstats.go +++ b/src/runtime/mstats.go @@ -40,7 +40,10 @@ type mstats struct { heap_idle uint64 // bytes in idle spans heap_inuse uint64 // bytes in mSpanInUse spans heap_released uint64 // bytes released to the os - heap_objects uint64 // total number of allocated objects + + // heap_objects is not used by the runtime directly and instead + // computed on the fly by updatememstats. + heap_objects uint64 // total number of allocated objects // Statistics about allocation of low-level fixed-size structures. // Protected by FixAlloc locks. -- 2.51.0