]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: simplify sweep allocation counting
authorAustin Clements <austin@google.com>
Fri, 23 Dec 2016 01:00:18 +0000 (18:00 -0700)
committerAustin Clements <austin@google.com>
Fri, 3 Mar 2017 17:02:16 +0000 (17:02 +0000)
Currently sweep counts the number of allocated objects, computes the
number of free objects from that, then re-computes the number of
allocated objects from that. Simplify and clean this up by skipping
these intermediate steps.

Change-Id: I3ed98e371eb54bbcab7c8530466c4ab5fde35f0a
Reviewed-on: https://go-review.googlesource.com/34935
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Marvin Stenger <marvin.stenger94@gmail.com>
Reviewed-by: Rick Hudson <rlh@golang.org>
src/runtime/mbitmap.go
src/runtime/mgcsweep.go

index 57225ab32221e50185e6281bd1934a99b775c821..7bead96904bf64c1ee6bf87f5057c7ed34e8b58f 100644 (file)
@@ -823,10 +823,10 @@ var oneBitCount = [256]uint8{
        4, 5, 5, 6, 5, 6, 6, 7,
        5, 6, 6, 7, 6, 7, 7, 8}
 
-// countFree runs through the mark bits in a span and counts the number of free objects
-// in the span.
+// countAlloc returns the number of objects allocated in span s by
+// scanning the allocation bitmap.
 // TODO:(rlh) Use popcount intrinsic.
-func (s *mspan) countFree() int {
+func (s *mspan) countAlloc() int {
        count := 0
        maxIndex := s.nelems / 8
        for i := uintptr(0); i < maxIndex; i++ {
@@ -839,7 +839,7 @@ func (s *mspan) countFree() int {
                bits := mrkBits & mask
                count += int(oneBitCount[bits])
        }
-       return int(s.nelems) - count
+       return count
 }
 
 // heapBitsSetType records that the new allocation [x, x+size)
index fb5c488ffc37ed87e22ac91c569928842dd3940c..63c7fb782f406687c7e35775958e422fab025448 100644 (file)
@@ -186,7 +186,6 @@ func (s *mspan) sweep(preserve bool) bool {
        cl := s.sizeclass
        size := s.elemsize
        res := false
-       nfree := 0
 
        c := _g_.m.mcache
        freeToHeap := false
@@ -276,15 +275,14 @@ func (s *mspan) sweep(preserve bool) bool {
        }
 
        // Count the number of free objects in this span.
-       nfree = s.countFree()
-       if cl == 0 && nfree != 0 {
+       nalloc := uint16(s.countAlloc())
+       if cl == 0 && nalloc == 0 {
                s.needzero = 1
                freeToHeap = true
        }
-       nalloc := uint16(s.nelems) - uint16(nfree)
        nfreed := s.allocCount - nalloc
        if nalloc > s.allocCount {
-               print("runtime: nelems=", s.nelems, " nfree=", nfree, " nalloc=", nalloc, " previous allocCount=", s.allocCount, " nfreed=", nfreed, "\n")
+               print("runtime: nelems=", s.nelems, " nalloc=", nalloc, " previous allocCount=", s.allocCount, " nfreed=", nfreed, "\n")
                throw("sweep increased allocation count")
        }