Skip to content

Commit 7da03b9

Browse files
committed
runtime: compute goal first in gcSetTriggerRatio
This slightly rearranges gcSetTriggerRatio to compute the goal before computing the other controls. This will simplify implementing the heap limit, which needs to control the absolute goal and flow the rest of the control parameters from this. For #16843. Change-Id: I46b7c1f8b6e4edbee78930fb093b60bd1a03d75e Reviewed-on: https://go-review.googlesource.com/c/go/+/46750 Run-TryBot: Austin Clements <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Rick Hudson <[email protected]>
1 parent 7ac0a8b commit 7da03b9

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/runtime/mgc.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,14 @@ func pollFractionalWorkerExit() bool {
765765
//
766766
// mheap_.lock must be held or the world must be stopped.
767767
func gcSetTriggerRatio(triggerRatio float64) {
768+
// Compute the next GC goal, which is when the allocated heap
769+
// has grown by GOGC/100 over the heap marked by the last
770+
// cycle.
771+
goal := ^uint64(0)
772+
if gcpercent >= 0 {
773+
goal = memstats.heap_marked + memstats.heap_marked*uint64(gcpercent)/100
774+
}
775+
768776
// Set the trigger ratio, capped to reasonable bounds.
769777
if triggerRatio < 0 {
770778
// This can happen if the mutator is allocating very
@@ -807,22 +815,16 @@ func gcSetTriggerRatio(triggerRatio float64) {
807815
print("runtime: next_gc=", memstats.next_gc, " heap_marked=", memstats.heap_marked, " heap_live=", memstats.heap_live, " initialHeapLive=", work.initialHeapLive, "triggerRatio=", triggerRatio, " minTrigger=", minTrigger, "\n")
808816
throw("gc_trigger underflow")
809817
}
810-
}
811-
memstats.gc_trigger = trigger
812-
813-
// Compute the next GC goal, which is when the allocated heap
814-
// has grown by GOGC/100 over the heap marked by the last
815-
// cycle.
816-
goal := ^uint64(0)
817-
if gcpercent >= 0 {
818-
goal = memstats.heap_marked + memstats.heap_marked*uint64(gcpercent)/100
819-
if goal < trigger {
818+
if trigger > goal {
820819
// The trigger ratio is always less than GOGC/100, but
821820
// other bounds on the trigger may have raised it.
822821
// Push up the goal, too.
823822
goal = trigger
824823
}
825824
}
825+
826+
// Commit to the trigger and goal.
827+
memstats.gc_trigger = trigger
826828
memstats.next_gc = goal
827829
if trace.enabled {
828830
traceNextGC()

0 commit comments

Comments
 (0)