Skip to content

Commit

Permalink
Avoid double loops to zero out padding in events.slice (#41810)
Browse files Browse the repository at this point in the history
  • Loading branch information
espadolini authored May 21, 2024
1 parent 2372645 commit cbbf4bd
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 100 deletions.
7 changes: 4 additions & 3 deletions lib/events/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,10 @@ func (s *slice) reader() (io.ReadSeeker, error) {
// non last slices should be at least min upload bytes (as limited by S3 API spec)
if !s.isLast && wroteBytes < s.proto.cfg.MinUploadBytes {
paddingBytes = s.proto.cfg.MinUploadBytes - wroteBytes
if _, err := s.buffer.ReadFrom(utils.NewRepeatReader(byte(0), int(paddingBytes))); err != nil {
return nil, trace.Wrap(err)
}
s.buffer.Grow(int(paddingBytes))
padding := s.buffer.AvailableBuffer()[:paddingBytes]
clear(padding)
s.buffer.Write(padding)
}
data := s.buffer.Bytes()
// when the slice was created, the first bytes were reserved
Expand Down
57 changes: 0 additions & 57 deletions lib/utils/repeat.go

This file was deleted.

40 changes: 0 additions & 40 deletions lib/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package utils
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -562,45 +561,6 @@ func TestStringsSet(t *testing.T) {
require.NotNil(t, out)
}

// TestRepeatReader tests repeat reader
func TestRepeatReader(t *testing.T) {
t.Parallel()

type tc struct {
name string
repeat byte
count int
expected string
}
tcs := []tc{
{
name: "repeat once",
repeat: byte('a'),
count: 1,
expected: "a",
},
{
name: "repeat zero times",
repeat: byte('a'),
count: 0,
expected: "",
},
{
name: "repeat multiple times",
repeat: byte('a'),
count: 3,
expected: "aaa",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
data, err := io.ReadAll(NewRepeatReader(tc.repeat, tc.count))
require.NoError(t, err)
require.Equal(t, tc.expected, string(data))
})
}
}

func TestReadAtMost(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit cbbf4bd

Please sign in to comment.