Skip to content

Commit b5f87b5

Browse files
qiulaidongfengrandall77
authored andcommitted
runtime: use max/min func
Change-Id: I3f0b7209621b39cee69566a5cc95e4343b4f1f20 GitHub-Last-Rev: af9dbbe GitHub-Pull-Request: #63321 Reviewed-on: https://go-review.googlesource.com/c/go/+/531916 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Mauri de Souza Meneguzzo <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 9ab5121 commit b5f87b5

File tree

10 files changed

+39
-68
lines changed

10 files changed

+39
-68
lines changed

src/runtime/export_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ func (b *PallocBits) PopcntRange(i, n uint) uint { return (*pageBits)(b).popcntR
806806
// SummarizeSlow is a slow but more obviously correct implementation
807807
// of (*pallocBits).summarize. Used for testing.
808808
func SummarizeSlow(b *PallocBits) PallocSum {
809-
var start, max, end uint
809+
var start, most, end uint
810810

811811
const N = uint(len(b)) * 64
812812
for start < N && (*pageBits)(b).get(start) == 0 {
@@ -822,11 +822,9 @@ func SummarizeSlow(b *PallocBits) PallocSum {
822822
} else {
823823
run = 0
824824
}
825-
if run > max {
826-
max = run
827-
}
825+
most = max(most, run)
828826
}
829-
return PackPallocSum(start, max, end)
827+
return PackPallocSum(start, most, end)
830828
}
831829

832830
// Expose non-trivial helpers for testing.

src/runtime/memmove_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,7 @@ func BenchmarkMemclrRange(b *testing.B) {
488488
maxLen := 0
489489

490490
for _, clrLen := range t.data {
491-
if clrLen > maxLen {
492-
maxLen = clrLen
493-
}
491+
maxLen = max(maxLen, clrLen)
494492
if clrLen < minLen || minLen == 0 {
495493
minLen = clrLen
496494
}

src/runtime/mfixalloc.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ func (f *fixalloc) init(size uintptr, first func(arg, p unsafe.Pointer), arg uns
5757
if size > _FixAllocChunk {
5858
throw("runtime: fixalloc size too large")
5959
}
60-
if min := unsafe.Sizeof(mlink{}); size < min {
61-
size = min
62-
}
60+
size = max(size, unsafe.Sizeof(mlink{}))
6361

6462
f.size = size
6563
f.first = first

src/runtime/mgcmark.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,9 +1326,7 @@ func scanobject(b uintptr, gcw *gcWork) {
13261326
// must be a large object, s.base() is the beginning
13271327
// of the object.
13281328
n = s.base() + s.elemsize - b
1329-
if n > maxObletBytes {
1330-
n = maxObletBytes
1331-
}
1329+
n = min(n, maxObletBytes)
13321330
}
13331331

13341332
hbits := heapBitsForAddr(b, n)

src/runtime/mgcpacer.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,7 @@ func (c *gcControllerState) trigger() (uint64, uint64) {
11371137
if goal > defaultHeapMinimum && goal-defaultHeapMinimum > maxTrigger {
11381138
maxTrigger = goal - defaultHeapMinimum
11391139
}
1140-
if maxTrigger < minTrigger {
1141-
maxTrigger = minTrigger
1142-
}
1140+
maxTrigger = max(maxTrigger, minTrigger)
11431141

11441142
// Compute the trigger from our bounds and the runway stored by commit.
11451143
var trigger uint64
@@ -1149,12 +1147,8 @@ func (c *gcControllerState) trigger() (uint64, uint64) {
11491147
} else {
11501148
trigger = goal - runway
11511149
}
1152-
if trigger < minTrigger {
1153-
trigger = minTrigger
1154-
}
1155-
if trigger > maxTrigger {
1156-
trigger = maxTrigger
1157-
}
1150+
trigger = max(trigger, minTrigger)
1151+
trigger = min(trigger, maxTrigger)
11581152
if trigger > goal {
11591153
print("trigger=", trigger, " heapGoal=", goal, "\n")
11601154
print("minTrigger=", minTrigger, " maxTrigger=", maxTrigger, "\n")

src/runtime/mgcscavenge.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -893,12 +893,12 @@ func fillAligned(x uint64, m uint) uint64 {
893893
// will round up). That is, even if max is small, the returned size is not guaranteed
894894
// to be equal to max. max is allowed to be less than min, in which case it is as if
895895
// max == min.
896-
func (m *pallocData) findScavengeCandidate(searchIdx uint, min, max uintptr) (uint, uint) {
897-
if min&(min-1) != 0 || min == 0 {
898-
print("runtime: min = ", min, "\n")
896+
func (m *pallocData) findScavengeCandidate(searchIdx uint, minimum, max uintptr) (uint, uint) {
897+
if minimum&(minimum-1) != 0 || minimum == 0 {
898+
print("runtime: min = ", minimum, "\n")
899899
throw("min must be a non-zero power of 2")
900-
} else if min > maxPagesPerPhysPage {
901-
print("runtime: min = ", min, "\n")
900+
} else if minimum > maxPagesPerPhysPage {
901+
print("runtime: min = ", minimum, "\n")
902902
throw("min too large")
903903
}
904904
// max may not be min-aligned, so we might accidentally truncate to
@@ -907,16 +907,16 @@ func (m *pallocData) findScavengeCandidate(searchIdx uint, min, max uintptr) (ui
907907
// a power of 2). This also prevents max from ever being less than
908908
// min, unless it's zero, so handle that explicitly.
909909
if max == 0 {
910-
max = min
910+
max = minimum
911911
} else {
912-
max = alignUp(max, min)
912+
max = alignUp(max, minimum)
913913
}
914914

915915
i := int(searchIdx / 64)
916916
// Start by quickly skipping over blocks of non-free or scavenged pages.
917917
for ; i >= 0; i-- {
918918
// 1s are scavenged OR non-free => 0s are unscavenged AND free
919-
x := fillAligned(m.scavenged[i]|m.pallocBits[i], uint(min))
919+
x := fillAligned(m.scavenged[i]|m.pallocBits[i], uint(minimum))
920920
if x != ^uint64(0) {
921921
break
922922
}
@@ -929,7 +929,7 @@ func (m *pallocData) findScavengeCandidate(searchIdx uint, min, max uintptr) (ui
929929
// extend further. Loop until we find the extent of it.
930930

931931
// 1s are scavenged OR non-free => 0s are unscavenged AND free
932-
x := fillAligned(m.scavenged[i]|m.pallocBits[i], uint(min))
932+
x := fillAligned(m.scavenged[i]|m.pallocBits[i], uint(minimum))
933933
z1 := uint(sys.LeadingZeros64(^x))
934934
run, end := uint(0), uint(i)*64+(64-z1)
935935
if x<<z1 != 0 {
@@ -942,7 +942,7 @@ func (m *pallocData) findScavengeCandidate(searchIdx uint, min, max uintptr) (ui
942942
// word so it may extend into further words.
943943
run = 64 - z1
944944
for j := i - 1; j >= 0; j-- {
945-
x := fillAligned(m.scavenged[j]|m.pallocBits[j], uint(min))
945+
x := fillAligned(m.scavenged[j]|m.pallocBits[j], uint(minimum))
946946
run += uint(sys.LeadingZeros64(x))
947947
if x != 0 {
948948
// The run stopped in this word.
@@ -953,10 +953,7 @@ func (m *pallocData) findScavengeCandidate(searchIdx uint, min, max uintptr) (ui
953953

954954
// Split the run we found if it's larger than max but hold on to
955955
// our original length, since we may need it later.
956-
size := run
957-
if size > uint(max) {
958-
size = uint(max)
959-
}
956+
size := min(run, uint(max))
960957
start := end - size
961958

962959
// Each huge page is guaranteed to fit in a single palloc chunk.

src/runtime/mksizeclasses.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,12 @@ func printComment(w io.Writer, classes []class) {
278278
}
279279

280280
func maxObjsPerSpan(classes []class) int {
281-
max := 0
281+
most := 0
282282
for _, c := range classes[1:] {
283283
n := c.npages * pageSize / c.size
284-
if n > max {
285-
max = n
286-
}
284+
most = max(most, n)
287285
}
288-
return max
286+
return most
289287
}
290288

291289
func printClasses(w io.Writer, classes []class) {

src/runtime/mpagealloc.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,8 +1038,8 @@ func mergeSummaries(sums []pallocSum, logMaxPagesPerSum uint) pallocSum {
10381038
// Merge the summaries in sums into one.
10391039
//
10401040
// We do this by keeping a running summary representing the merged
1041-
// summaries of sums[:i] in start, max, and end.
1042-
start, max, end := sums[0].unpack()
1041+
// summaries of sums[:i] in start, most, and end.
1042+
start, most, end := sums[0].unpack()
10431043
for i := 1; i < len(sums); i++ {
10441044
// Merge in sums[i].
10451045
si, mi, ei := sums[i].unpack()
@@ -1055,12 +1055,7 @@ func mergeSummaries(sums []pallocSum, logMaxPagesPerSum uint) pallocSum {
10551055
// across the boundary between the running sum and sums[i]
10561056
// and at the max sums[i], taking the greatest of those two
10571057
// and the max of the running sum.
1058-
if end+si > max {
1059-
max = end + si
1060-
}
1061-
if mi > max {
1062-
max = mi
1063-
}
1058+
most = max(most, end+si, mi)
10641059

10651060
// Merge in end by checking if this new summary is totally
10661061
// free. If it is, then we want to extend the running sum's
@@ -1073,5 +1068,5 @@ func mergeSummaries(sums []pallocSum, logMaxPagesPerSum uint) pallocSum {
10731068
end = ei
10741069
}
10751070
}
1076-
return packPallocSum(start, max, end)
1071+
return packPallocSum(start, most, end)
10771072
}

src/runtime/mpallocbits.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ type pallocBits pageBits
134134

135135
// summarize returns a packed summary of the bitmap in pallocBits.
136136
func (b *pallocBits) summarize() pallocSum {
137-
var start, max, cur uint
137+
var start, most, cur uint
138138
const notSetYet = ^uint(0) // sentinel for start value
139139
start = notSetYet
140140
for i := 0; i < len(b); i++ {
@@ -151,9 +151,7 @@ func (b *pallocBits) summarize() pallocSum {
151151
if start == notSetYet {
152152
start = cur
153153
}
154-
if cur > max {
155-
max = cur
156-
}
154+
most = max(most, cur)
157155
// Final region that might span to next uint64
158156
cur = l
159157
}
@@ -162,12 +160,11 @@ func (b *pallocBits) summarize() pallocSum {
162160
const n = uint(64 * len(b))
163161
return packPallocSum(n, n, n)
164162
}
165-
if cur > max {
166-
max = cur
167-
}
168-
if max >= 64-2 {
163+
most = max(most, cur)
164+
165+
if most >= 64-2 {
169166
// There is no way an internal run of zeros could beat max.
170-
return packPallocSum(start, max, cur)
167+
return packPallocSum(start, most, cur)
171168
}
172169
// Now look inside each uint64 for runs of zeros.
173170
// All uint64s must be nonzero, or we would have aborted above.
@@ -188,7 +185,7 @@ outer:
188185

189186
// Strategy: shrink all runs of zeros by max. If any runs of zero
190187
// remain, then we've identified a larger maximum zero run.
191-
p := max // number of zeros we still need to shrink by.
188+
p := most // number of zeros we still need to shrink by.
192189
k := uint(1) // current minimum length of runs of ones in x.
193190
for {
194191
// Shrink all runs of zeros by p places (except the top zeros).
@@ -217,14 +214,14 @@ outer:
217214
x >>= j & 63 // remove trailing ones
218215
j = uint(sys.TrailingZeros64(x)) // count contiguous trailing zeros
219216
x >>= j & 63 // remove zeros
220-
max += j // we have a new maximum!
217+
most += j // we have a new maximum!
221218
if x&(x+1) == 0 { // no more zeros (except at the top).
222219
continue outer
223220
}
224221
p = j // remove j more zeros from each zero run.
225222
}
226223
}
227-
return packPallocSum(start, max, cur)
224+
return packPallocSum(start, most, cur)
228225
}
229226

230227
// find searches for npages contiguous free pages in pallocBits and returns

src/runtime/symtab.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,16 +1043,14 @@ func funcMaxSPDelta(f funcInfo) int32 {
10431043
p := datap.pctab[f.pcsp:]
10441044
pc := f.entry()
10451045
val := int32(-1)
1046-
max := int32(0)
1046+
most := int32(0)
10471047
for {
10481048
var ok bool
10491049
p, ok = step(p, &pc, &val, pc == f.entry())
10501050
if !ok {
1051-
return max
1052-
}
1053-
if val > max {
1054-
max = val
1051+
return most
10551052
}
1053+
most = max(most, val)
10561054
}
10571055
}
10581056

0 commit comments

Comments
 (0)