Merge pull request #2303 from kolyshkin/minmax

Use min/max to simplify the code
This commit is contained in:
openshift-merge-bot[bot] 2025-03-31 16:23:43 +00:00 committed by GitHub
commit 3fc5c23095
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 6 additions and 24 deletions

View File

@ -496,10 +496,7 @@ type slowEmptyTarReader struct {
// Read is a slow reader of an empty tar (like the output of "tar c --files-from /dev/null") // Read is a slow reader of an empty tar (like the output of "tar c --files-from /dev/null")
func (s *slowEmptyTarReader) Read(p []byte) (int, error) { func (s *slowEmptyTarReader) Read(p []byte) (int, error) {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
count := s.chunkSize count := min(len(p), s.chunkSize)
if len(p) < s.chunkSize {
count = len(p)
}
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
p[i] = 0 p[i] = 0
} }

View File

@ -142,10 +142,7 @@ func (rc *rollingChecksumReader) Read(b []byte) (bool, int, error) {
rc.IsLastChunkZeros = false rc.IsLastChunkZeros = false
if rc.pendingHole > 0 { if rc.pendingHole > 0 {
toCopy := int64(len(b)) toCopy := min(rc.pendingHole, int64(len(b)))
if rc.pendingHole < toCopy {
toCopy = rc.pendingHole
}
rc.pendingHole -= toCopy rc.pendingHole -= toCopy
for i := int64(0); i < toCopy; i++ { for i := int64(0); i < toCopy; i++ {
b[i] = 0 b[i] = 0

View File

@ -698,18 +698,12 @@ func (c *chunkedDiffer) prepareCompressedStreamToFile(partCompression compressed
// hashHole writes SIZE zeros to the specified hasher // hashHole writes SIZE zeros to the specified hasher
func hashHole(h hash.Hash, size int64, copyBuffer []byte) error { func hashHole(h hash.Hash, size int64, copyBuffer []byte) error {
count := int64(len(copyBuffer)) count := min(size, int64(len(copyBuffer)))
if size < count {
count = size
}
for i := int64(0); i < count; i++ { for i := int64(0); i < count; i++ {
copyBuffer[i] = 0 copyBuffer[i] = 0
} }
for size > 0 { for size > 0 {
count = int64(len(copyBuffer)) count = min(size, int64(len(copyBuffer)))
if size < count {
count = size
}
if _, err := h.Write(copyBuffer[:count]); err != nil { if _, err := h.Write(copyBuffer[:count]); err != nil {
return err return err
} }

View File

@ -93,10 +93,7 @@ loop0:
} }
// add new byte slice to the buffers slice and continue writing // add new byte slice to the buffers slice and continue writing
nextCap := b.Cap() * 2 nextCap := min(b.Cap()*2, maxCap)
if nextCap > maxCap {
nextCap = maxCap
}
bp.buf = append(bp.buf, getBuffer(nextCap)) bp.buf = append(bp.buf, getBuffer(nextCap))
} }
bp.wait.Broadcast() bp.wait.Broadcast()

View File

@ -276,10 +276,7 @@ func (s *store) getAutoUserNS(options *types.AutoUserNsOptions, image *Image, rl
// bigger than s.autoNsMaxSize. // bigger than s.autoNsMaxSize.
// This is a best effort heuristic. // This is a best effort heuristic.
if requestedSize == 0 { if requestedSize == 0 {
size = initialSize size = max(s.autoNsMinSize, initialSize)
if s.autoNsMinSize > size {
size = s.autoNsMinSize
}
if image != nil { if image != nil {
sizeFromImage, err := s.getMaxSizeFromImage(image, rlstore, lstores, options.PasswdFile, options.GroupFile) sizeFromImage, err := s.getMaxSizeFromImage(image, rlstore, lstores, options.PasswdFile, options.GroupFile)
if err != nil { if err != nil {