aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2025-01-17 10:28:16 +0100
committerDmitry Vyukov <dvyukov@google.com>2025-01-17 10:02:07 +0000
commit5d04aae8969f6c72318ce0a4cde4f027766b1a55 (patch)
tree8f1b632847c431407090f0fe1335522ff2195a37 /pkg
parentf9e07a6e597b68d3397864e6ee4550f9065c3518 (diff)
all: use min/max functions
They are shorter, more readable, and don't require temp vars.
Diffstat (limited to 'pkg')
-rw-r--r--pkg/ast/format.go4
-rw-r--r--pkg/bisect/bisect.go16
-rw-r--r--pkg/bisect/minimize/slice.go5
-rw-r--r--pkg/compiler/gen.go12
-rw-r--r--pkg/compiler/types.go9
-rw-r--r--pkg/cover/backend/dwarf.go18
-rw-r--r--pkg/cover/html.go19
-rw-r--r--pkg/instance/instance.go4
-rw-r--r--pkg/report/report.go8
-rw-r--r--pkg/stat/set.go8
-rw-r--r--pkg/vcs/linux.go4
11 files changed, 24 insertions, 83 deletions
diff --git a/pkg/ast/format.go b/pkg/ast/format.go
index b16a62531..3e143596a 100644
--- a/pkg/ast/format.go
+++ b/pkg/ast/format.go
@@ -138,9 +138,7 @@ func (n *Struct) serialize(w io.Writer) {
maxTabs := 0
for _, f := range n.Fields {
tabs := (len(f.Name.Name) + tabWidth) / tabWidth
- if maxTabs < tabs {
- maxTabs = tabs
- }
+ maxTabs = max(maxTabs, tabs)
}
for _, f := range n.Fields {
if f.NewBlock {
diff --git a/pkg/bisect/bisect.go b/pkg/bisect/bisect.go
index 73a1971c8..ba022e514 100644
--- a/pkg/bisect/bisect.go
+++ b/pkg/bisect/bisect.go
@@ -928,9 +928,7 @@ func (env *env) postTestResult(res *testResult) {
// Let's be conservative and only decrease our reproduction likelihood estimate.
// As the estimate of each test() can also be flaky, only partially update the result.
avg := (env.reproChance + res.badRatio) / 2.0
- if env.reproChance > avg {
- env.reproChance = avg
- }
+ env.reproChance = min(env.reproChance, avg)
}
}
@@ -1086,7 +1084,7 @@ func pickReleaseTags(all []string) []string {
}
var ret []string
// Take 2 latest sub releases.
- takeSubReleases := minInts(2, len(subReleases))
+ takeSubReleases := min(2, len(subReleases))
ret = append(ret, subReleases[:takeSubReleases]...)
// If there are a lot of sub releases, also take the middle one.
if len(subReleases) > 5 {
@@ -1107,13 +1105,3 @@ func pickReleaseTags(all []string) []string {
}
return ret
}
-
-func minInts(vals ...int) int {
- ret := vals[0]
- for i := 1; i < len(vals); i++ {
- if vals[i] < ret {
- ret = vals[i]
- }
- }
- return ret
-}
diff --git a/pkg/bisect/minimize/slice.go b/pkg/bisect/minimize/slice.go
index fee125542..d5fbc6c6a 100644
--- a/pkg/bisect/minimize/slice.go
+++ b/pkg/bisect/minimize/slice.go
@@ -257,10 +257,7 @@ func splitChunk[T any](chunk []T, parts int) [][]T {
}
var ret [][]T
for i := 0; i < len(chunk); i += chunkSize {
- end := i + chunkSize
- if end > len(chunk) {
- end = len(chunk)
- }
+ end := min(i+chunkSize, len(chunk))
ret = append(ret, chunk[i:end])
}
return ret
diff --git a/pkg/compiler/gen.go b/pkg/compiler/gen.go
index fb406b815..81e8f83bc 100644
--- a/pkg/compiler/gen.go
+++ b/pkg/compiler/gen.go
@@ -284,9 +284,7 @@ func (comp *compiler) layoutUnion(t *prog.UnionType) {
" which is less than field %v size %v",
structNode.Name.Name, sizeAttr, fld.Type.Name(), sz)
}
- if t.TypeSize < sz {
- t.TypeSize = sz
- }
+ t.TypeSize = max(t.TypeSize, sz)
}
if hasSize {
t.TypeSize = sizeAttr
@@ -313,9 +311,7 @@ func (comp *compiler) layoutStruct(t *prog.StructType) {
size = 0
}
size += f.Size()
- if t.TypeSize < size {
- t.TypeSize = size
- }
+ t.TypeSize = max(t.TypeSize, size)
}
sizeAttr, hasSize := attrs[attrSize]
if hasSize {
@@ -348,9 +344,7 @@ func (comp *compiler) layoutStructFields(t *prog.StructType, varlen, packed bool
fieldAlign := uint64(1)
if !packed {
fieldAlign = f.Alignment()
- if structAlign < fieldAlign {
- structAlign = fieldAlign
- }
+ structAlign = max(structAlign, fieldAlign)
}
fullBitOffset := byteOffset*8 + bitOffset
var fieldOffset uint64
diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go
index 906af199c..ae0864725 100644
--- a/pkg/compiler/types.go
+++ b/pkg/compiler/types.go
@@ -1007,9 +1007,7 @@ func init() {
case *prog.UnionType:
typ1.Fields = fields
for _, f := range fields {
- if a := f.Type.Alignment(); typ1.TypeAlign < a {
- typ1.TypeAlign = a
- }
+ typ1.TypeAlign = max(typ1.TypeAlign, f.Type.Alignment())
}
case *prog.StructType:
typ1.Fields = fields
@@ -1028,10 +1026,7 @@ func init() {
typ1.TypeAlign = 1
} else {
for _, f := range fields {
- a := f.Type.Alignment()
- if typ1.TypeAlign < a {
- typ1.TypeAlign = a
- }
+ typ1.TypeAlign = max(typ1.TypeAlign, f.Type.Alignment())
}
}
}
diff --git a/pkg/cover/backend/dwarf.go b/pkg/cover/backend/dwarf.go
index 375a82dcc..eeb924a4b 100644
--- a/pkg/cover/backend/dwarf.go
+++ b/pkg/cover/backend/dwarf.go
@@ -405,21 +405,14 @@ func readTextRanges(debugInfo *dwarf.Data, module *vminfo.KernelModule, pcFix pc
func symbolizeModule(target *targets.Target, interner *symbolizer.Interner, objDir, srcDir, buildDir string,
splitBuildDelimiters []string, mod *vminfo.KernelModule, pcs []uint64) ([]Frame, error) {
- procs := runtime.GOMAXPROCS(0) / 2
- if need := len(pcs) / 1000; procs > need {
- procs = need
- }
+ procs := min(runtime.GOMAXPROCS(0)/2, len(pcs)/1000)
const (
minProcs = 1
maxProcs = 4
)
// addr2line on a beefy vmlinux takes up to 1.6GB of RAM, so don't create too many of them.
- if procs > maxProcs {
- procs = maxProcs
- }
- if procs < minProcs {
- procs = minProcs
- }
+ procs = min(procs, maxProcs)
+ procs = max(procs, minProcs)
type symbolizerResult struct {
frames []symbolizer.Frame
err error
@@ -449,10 +442,7 @@ func symbolizeModule(target *targets.Target, interner *symbolizer.Interner, objD
}()
}
for i := 0; i < len(pcs); {
- end := i + 100
- if end > len(pcs) {
- end = len(pcs)
- }
+ end := min(i+100, len(pcs))
pcchan <- pcs[i:end]
i = end
}
diff --git a/pkg/cover/html.go b/pkg/cover/html.go
index ba38cd884..2c8a1281e 100644
--- a/pkg/cover/html.go
+++ b/pkg/cover/html.go
@@ -160,10 +160,7 @@ func fileLineContents(file *file, lines [][]byte) lineCoverExport {
start := 0
cover := append(lineCover[i+1], lineCoverChunk{End: backend.LineEnd})
for _, cov := range cover {
- end := cov.End - 1
- if end > len(ln) {
- end = len(ln)
- }
+ end := min(cov.End-1, len(ln))
if end == start {
continue
}
@@ -661,10 +658,7 @@ func fileContents(file *file, lines [][]byte, haveProgs bool) string {
start := 0
cover := append(lineCover[i+1], lineCoverChunk{End: backend.LineEnd})
for _, cov := range cover {
- end := cov.End - 1
- if end > len(ln) {
- end = len(ln)
- }
+ end := min(cov.End-1, len(ln))
if end == start {
continue
}
@@ -708,9 +702,7 @@ func perLineCoverage(covered, uncovered []backend.Range) map[int][]lineCoverChun
func mergeRange(lines map[int][]lineCoverChunk, r backend.Range, covered bool) {
// Don't panic on broken debug info, it is frequently broken.
- if r.EndLine < r.StartLine {
- r.EndLine = r.StartLine
- }
+ r.EndLine = max(r.EndLine, r.StartLine)
if r.EndLine == r.StartLine && r.EndCol <= r.StartCol {
r.EndCol = backend.LineEnd
}
@@ -750,10 +742,7 @@ func mergeLine(chunks []lineCoverChunk, start, end int, covered bool) []lineCove
if chunkStart < start {
res = append(res, lineCoverChunk{start, chunk.Covered, chunk.Uncovered})
}
- mid := end
- if mid > chunk.End {
- mid = chunk.End
- }
+ mid := min(end, chunk.End)
res = append(res, lineCoverChunk{mid, chunk.Covered || covered, chunk.Uncovered || !covered})
if chunk.End > end {
res = append(res, lineCoverChunk{chunk.End, chunk.Covered, chunk.Uncovered})
diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go
index f2f024216..411568e65 100644
--- a/pkg/instance/instance.go
+++ b/pkg/instance/instance.go
@@ -273,9 +273,7 @@ func (env *env) Test(numVMs int, reproSyz, reproOpts, reproC []byte) ([]EnvTestR
return nil, fmt.Errorf("failed to create VM pool: %w", err)
}
defer vmPool.Close()
- if n := vmPool.Count(); numVMs > n {
- numVMs = n
- }
+ numVMs = min(numVMs, vmPool.Count())
res := make(chan EnvTestResult, numVMs)
for i := 0; i < numVMs; i++ {
inst := &inst{
diff --git a/pkg/report/report.go b/pkg/report/report.go
index d14e22f51..0006cb92b 100644
--- a/pkg/report/report.go
+++ b/pkg/report/report.go
@@ -219,11 +219,9 @@ func (reporter *Reporter) ParseFrom(output []byte, minReportPos int) *Report {
if pos := bytes.IndexByte(rep.Output[rep.StartPos:], '\n'); pos != -1 {
rep.SkipPos = rep.StartPos + pos
}
- if rep.EndPos < rep.SkipPos {
- // This generally should not happen.
- // But openbsd does some hacks with /r/n which may lead to off-by-one EndPos.
- rep.EndPos = rep.SkipPos
- }
+ // This generally should not happen.
+ // But openbsd does some hacks with /r/n which may lead to off-by-one EndPos.
+ rep.EndPos = max(rep.EndPos, rep.SkipPos)
return rep
}
diff --git a/pkg/stat/set.go b/pkg/stat/set.go
index 4982362d9..f6e327d92 100644
--- a/pkg/stat/set.go
+++ b/pkg/stat/set.go
@@ -233,9 +233,7 @@ func (s *set) New(name, desc string, opts ...any) *Val {
lines: make(map[string]*line),
}
}
- if s.graphs[v.graph].level < v.level {
- s.graphs[v.graph].level = v.level
- }
+ s.graphs[v.graph].level = max(s.graphs[v.graph].level, v.level)
s.graphs[v.graph].stacked = stacked
}
return v
@@ -345,9 +343,7 @@ func (s *set) tick() {
*pv += float64(val-v.prev) / float64(s.historyScale)
v.prev = val
} else {
- if *pv < float64(val) {
- *pv = float64(val)
- }
+ *pv = max(*pv, float64(val))
}
}
}
diff --git a/pkg/vcs/linux.go b/pkg/vcs/linux.go
index d348dd71e..c0b96aaa4 100644
--- a/pkg/vcs/linux.go
+++ b/pkg/vcs/linux.go
@@ -109,9 +109,7 @@ func gitReleaseTagToInt(tag string, includeRC bool) uint64 {
if v1 < 0 {
return 0
}
- if v3 < 0 {
- v3 = 0
- }
+ v3 = max(v3, 0)
if rc >= 0 {
if !includeRC {
return 0