Skip to content

Commit

Permalink
Replace usages of bytes.Buffer with strings.Builder (#14539)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Robenolt <[email protected]>
  • Loading branch information
mattrobenolt authored Nov 17, 2023
1 parent 29a9bf4 commit 81777e5
Show file tree
Hide file tree
Showing 31 changed files with 143 additions and 164 deletions.
2 changes: 1 addition & 1 deletion go/bytes2/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (buf *Buffer) String() string {
// is _not_ allocated, so modifying this buffer after calling StringUnsafe will lead
// to undefined behavior.
func (buf *Buffer) StringUnsafe() string {
return *(*string)(unsafe.Pointer(&buf.bytes))
return unsafe.String(unsafe.SliceData(buf.bytes), len(buf.bytes))
}

// Reset is equivalent to bytes.Buffer.Reset.
Expand Down
6 changes: 3 additions & 3 deletions go/mysql/collations/integration/collations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func getSQLQueries(t *testing.T, testfile string) []string {
defer tf.Close()

var chunks []string
var curchunk bytes.Buffer
var curchunk strings.Builder

addchunk := func() {
if curchunk.Len() > 0 {
Expand Down Expand Up @@ -219,8 +219,8 @@ func TestCollationsOnMysqld(t *testing.T) {
}

func TestRemoteKanaSensitivity(t *testing.T) {
var Kana1 = []byte("の東京ノ")
var Kana2 = []byte("ノ東京の")
Kana1 := []byte("の東京ノ")
Kana2 := []byte("ノ東京の")

testRemoteComparison(t, nil, []testcmp{
{"utf8mb4_0900_as_cs", Kana1, Kana2},
Expand Down
7 changes: 3 additions & 4 deletions go/mysql/sqlerror/sql_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package sqlerror

import (
"bytes"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -53,17 +52,17 @@ func NewSQLError(number ErrorCode, sqlState string, format string, args ...any)

// Error implements the error interface
func (se *SQLError) Error() string {
buf := &bytes.Buffer{}
var buf strings.Builder
buf.WriteString(se.Message)

// Add MySQL errno and SQLSTATE in a format that we can later parse.
// There's no avoiding string parsing because all errors
// are converted to strings anyway at RPC boundaries.
// See NewSQLErrorFromError.
fmt.Fprintf(buf, " (errno %v) (sqlstate %v)", se.Num, se.State)
fmt.Fprintf(&buf, " (errno %v) (sqlstate %v)", se.Num, se.State)

if se.Query != "" {
fmt.Fprintf(buf, " during query: %s", sqlparser.TruncateForLog(se.Query))
fmt.Fprintf(&buf, " during query: %s", sqlparser.TruncateForLog(se.Query))
}

return buf.String()
Expand Down
4 changes: 2 additions & 2 deletions go/sqltypes/bind_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package sqltypes

import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"

"google.golang.org/protobuf/proto"

Expand Down Expand Up @@ -418,7 +418,7 @@ func FormatBindVariables(bindVariables map[string]*querypb.BindVariable, full, a
}

if asJSON {
var buf bytes.Buffer
var buf strings.Builder
buf.WriteString("{")
first := true
for k, v := range out {
Expand Down
7 changes: 3 additions & 4 deletions go/sqltypes/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package sqltypes

import (
"bytes"
crand "crypto/rand"
"encoding/base64"
"encoding/hex"
Expand Down Expand Up @@ -155,13 +154,13 @@ func TestTuple(vals ...Value) Value {
// PrintResults prints []*Results into a string.
// This function should only be used for testing.
func PrintResults(results []*Result) string {
b := new(bytes.Buffer)
var b strings.Builder
for i, r := range results {
if i == 0 {
fmt.Fprintf(b, "%v", r)
fmt.Fprintf(&b, "%v", r)
continue
}
fmt.Fprintf(b, ", %v", r)
fmt.Fprintf(&b, ", %v", r)
}
return b.String()
}
Expand Down
8 changes: 4 additions & 4 deletions go/sqltypes/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,13 @@ func TestEncode(t *testing.T) {
outASCII: "'YQ=='",
}}
for _, tcase := range testcases {
buf := &bytes.Buffer{}
tcase.in.EncodeSQL(buf)
var buf strings.Builder
tcase.in.EncodeSQL(&buf)
if tcase.outSQL != buf.String() {
t.Errorf("%v.EncodeSQL = %q, want %q", tcase.in, buf.String(), tcase.outSQL)
}
buf = &bytes.Buffer{}
tcase.in.EncodeASCII(buf)
buf.Reset()
tcase.in.EncodeASCII(&buf)
if tcase.outASCII != buf.String() {
t.Errorf("%v.EncodeASCII = %q, want %q", tcase.in, buf.String(), tcase.outASCII)
}
Expand Down
3 changes: 1 addition & 2 deletions go/stats/opentsdb/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package opentsdb

import (
"bytes"
"encoding/json"
"expvar"
"strings"
Expand Down Expand Up @@ -65,7 +64,7 @@ func (dc *collector) addFloat(metric string, val float64, tags map[string]string
// Also make everything lowercase, since opentsdb is case sensitive and lowercase
// simplifies the convention.
sanitize := func(text string) string {
var b bytes.Buffer
var b strings.Builder
for _, r := range text {
if unicode.IsDigit(r) || unicode.IsLetter(r) || r == '-' || r == '_' || r == '/' || r == '.' {
b.WriteRune(r)
Expand Down
25 changes: 10 additions & 15 deletions go/tools/release-notes/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -79,9 +78,7 @@ type (
}
)

var (
releaseNotesPath = `changelog/`
)
var releaseNotesPath = `changelog/`

const (
releaseNotesPathGitHub = `https://github.com/vitessio/vitess/blob/main/`
Expand Down Expand Up @@ -141,7 +138,7 @@ func (rn *releaseNote) generate(rnFile, changelogFile *os.File) error {
// Generate the release notes
rn.PathToChangeLogFileOnGH = releaseNotesPathGitHub + path.Join(rn.SubDirPath, "changelog.md")
if rnFile == nil {
rnFile, err = os.OpenFile(path.Join(rn.SubDirPath, "release_notes.md"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
rnFile, err = os.OpenFile(path.Join(rn.SubDirPath, "release_notes.md"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
return err
}
Expand All @@ -155,7 +152,7 @@ func (rn *releaseNote) generate(rnFile, changelogFile *os.File) error {

// Generate the changelog
if changelogFile == nil {
changelogFile, err = os.OpenFile(path.Join(rn.SubDirPath, "changelog.md"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
changelogFile, err = os.OpenFile(path.Join(rn.SubDirPath, "changelog.md"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
return err
}
Expand Down Expand Up @@ -304,23 +301,23 @@ func getStringForPullRequestInfos(prPerType prsByType) (string, error) {
data := createSortedPrTypeSlice(prPerType)

t := template.Must(template.New("markdownTemplatePR").Parse(markdownTemplatePR))
buff := bytes.Buffer{}
if err := t.ExecuteTemplate(&buff, "markdownTemplatePR", data); err != nil {
var buf strings.Builder
if err := t.ExecuteTemplate(&buf, "markdownTemplatePR", data); err != nil {
return "", err
}
return buff.String(), nil
return buf.String(), nil
}

func getStringForKnownIssues(issues []knownIssue) (string, error) {
if len(issues) == 0 {
return "", nil
}
t := template.Must(template.New("markdownTemplateKnownIssues").Parse(markdownTemplateKnownIssues))
buff := bytes.Buffer{}
if err := t.ExecuteTemplate(&buff, "markdownTemplateKnownIssues", issues); err != nil {
var buf strings.Builder
if err := t.ExecuteTemplate(&buf, "markdownTemplateKnownIssues", issues); err != nil {
return "", err
}
return buff.String(), nil
return buf.String(), nil
}

func groupAndStringifyPullRequest(pris []pullRequestInformation) (string, error) {
Expand All @@ -336,9 +333,7 @@ func groupAndStringifyPullRequest(pris []pullRequestInformation) (string, error)
}

func main() {
var (
versionName, summaryFile string
)
var versionName, summaryFile string
pflag.StringVarP(&versionName, "version", "v", "", "name of the version (has to be the following format: v11.0.0)")
pflag.StringVarP(&summaryFile, "summary", "s", "", "readme file on which there is a summary of the release")
pflag.Parse()
Expand Down
8 changes: 4 additions & 4 deletions go/vt/binlog/binlog_streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ func (bls *Streamer) parseEvents(ctx context.Context, events <-chan mysql.Binlog
var statements []FullBinlogStatement
var format mysql.BinlogFormat
var gtid replication.GTID
var pos = bls.startPos
var autocommit = true
pos := bls.startPos
autocommit := true
var err error

// Remember the RBR state.
Expand Down Expand Up @@ -723,7 +723,7 @@ func (bls *Streamer) appendDeletes(statements []FullBinlogStatement, tce *tableC
}

// writeValuesAsSQL is a helper method to print the values as SQL in the
// provided bytes.Buffer. It also returns the value for the keyspaceIDColumn,
// provided strings.Builder. It also returns the value for the keyspaceIDColumn,
// and the array of values for the PK, if necessary.
func writeValuesAsSQL(sql *sqlparser.TrackedBuffer, tce *tableCacheEntry, rs *mysql.Rows, rowIndex int, getPK bool) (sqltypes.Value, []sqltypes.Value, error) {
valueIndex := 0
Expand Down Expand Up @@ -794,7 +794,7 @@ func writeValuesAsSQL(sql *sqlparser.TrackedBuffer, tce *tableCacheEntry, rs *my
}

// writeIdentifiersAsSQL is a helper method to print the identifies as SQL in the
// provided bytes.Buffer. It also returns the value for the keyspaceIDColumn,
// provided strings.Builder. It also returns the value for the keyspaceIDColumn,
// and the array of values for the PK, if necessary.
func writeIdentifiersAsSQL(sql *sqlparser.TrackedBuffer, tce *tableCacheEntry, rs *mysql.Rows, rowIndex int, getPK bool) (sqltypes.Value, []sqltypes.Value, error) {
valueIndex := 0
Expand Down
7 changes: 3 additions & 4 deletions go/vt/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package hook

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -147,7 +146,7 @@ func (hook *Hook) ExecuteContext(ctx context.Context) (result *HookResult) {
}

// Run it.
var stdout, stderr bytes.Buffer
var stdout, stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr

Expand Down Expand Up @@ -234,7 +233,7 @@ func (hook *Hook) ExecuteAsWritePipe(out io.Writer) (io.WriteCloser, WaitFunc, i
return nil, nil, HOOK_GENERIC_ERROR, fmt.Errorf("failed to configure stdin: %v", err)
}
cmd.Stdout = out
var stderr bytes.Buffer
var stderr strings.Builder
cmd.Stderr = &stderr

// Start the process.
Expand Down Expand Up @@ -273,7 +272,7 @@ func (hook *Hook) ExecuteAsReadPipe(in io.Reader) (io.Reader, WaitFunc, int, err
return nil, nil, HOOK_GENERIC_ERROR, fmt.Errorf("failed to configure stdout: %v", err)
}
cmd.Stdin = in
var stderr bytes.Buffer
var stderr strings.Builder
cmd.Stderr = &stderr

// Start the process.
Expand Down
9 changes: 4 additions & 5 deletions go/vt/key/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package key

import (
"bytes"
"encoding/hex"
"math/rand"
"sort"
Expand Down Expand Up @@ -48,7 +47,7 @@ type Destination interface {

// DestinationsString returns a printed version of the destination array.
func DestinationsString(destinations []Destination) string {
var buffer bytes.Buffer
var buffer strings.Builder
buffer.WriteString("Destinations:")
for i, d := range destinations {
if i > 0 {
Expand Down Expand Up @@ -177,7 +176,7 @@ func (d DestinationExactKeyRanges) Resolve(allShards []*topodatapb.ShardReferenc

// String is part of the Destination interface.
func (d DestinationExactKeyRanges) String() string {
var buffer bytes.Buffer
var buffer strings.Builder
buffer.WriteString("DestinationExactKeyRanges(")
for i, kr := range d {
if i > 0 {
Expand Down Expand Up @@ -246,7 +245,7 @@ func (d DestinationKeyRanges) Resolve(allShards []*topodatapb.ShardReference, ad

// String is part of the Destination interface.
func (d DestinationKeyRanges) String() string {
var buffer bytes.Buffer
var buffer strings.Builder
buffer.WriteString("DestinationKeyRanges(")
for i, kr := range d {
if i > 0 {
Expand Down Expand Up @@ -318,7 +317,7 @@ func (d DestinationKeyspaceIDs) Resolve(allShards []*topodatapb.ShardReference,

// String is part of the Destination interface.
func (d DestinationKeyspaceIDs) String() string {
var buffer bytes.Buffer
var buffer strings.Builder
buffer.WriteString("DestinationKeyspaceIDs(")
for i, ksid := range d {
if i > 0 {
Expand Down
17 changes: 8 additions & 9 deletions go/vt/logutil/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package logutil

import (
"bytes"
"fmt"
"io"
"runtime"
Expand Down Expand Up @@ -57,7 +56,7 @@ type Logger interface {

// EventToBuffer formats an individual Event into a buffer, without the
// final '\n'
func EventToBuffer(event *logutilpb.Event, buf *bytes.Buffer) {
func EventToBuffer(event *logutilpb.Event, buf *strings.Builder) {
// Avoid Fprintf, for speed. The format is so simple that we
// can do it quickly by hand. It's worth about 3X. Fprintf is hard.

Expand Down Expand Up @@ -98,8 +97,8 @@ func EventToBuffer(event *logutilpb.Event, buf *bytes.Buffer) {

// EventString returns the line in one string
func EventString(event *logutilpb.Event) string {
buf := new(bytes.Buffer)
EventToBuffer(event, buf)
var buf strings.Builder
EventToBuffer(event, &buf)
return buf.String()
}

Expand Down Expand Up @@ -251,11 +250,11 @@ func NewMemoryLogger() *MemoryLogger {

// String returns all the lines in one String, separated by '\n'
func (ml *MemoryLogger) String() string {
buf := new(bytes.Buffer)
var buf strings.Builder
ml.mu.Lock()
defer ml.mu.Unlock()
for _, event := range ml.Events {
EventToBuffer(event, buf)
EventToBuffer(event, &buf)
buf.WriteByte('\n')
}
return buf.String()
Expand Down Expand Up @@ -355,15 +354,15 @@ func (tl *TeeLogger) Printf(format string, v ...any) {
const digits = "0123456789"

// twoDigits adds a zero-prefixed two-digit integer to buf
func twoDigits(buf *bytes.Buffer, value int) {
func twoDigits(buf *strings.Builder, value int) {
buf.WriteByte(digits[value/10])
buf.WriteByte(digits[value%10])
}

// nDigits adds an n-digit integer d to buf
// padding with pad on the left.
// It assumes d >= 0.
func nDigits(buf *bytes.Buffer, n, d int, pad byte) {
func nDigits(buf *strings.Builder, n, d int, pad byte) {
tmp := make([]byte, n)
j := n - 1
for ; j >= 0 && d > 0; j-- {
Expand All @@ -377,7 +376,7 @@ func nDigits(buf *bytes.Buffer, n, d int, pad byte) {
}

// someDigits adds a zero-prefixed variable-width integer to buf
func someDigits(buf *bytes.Buffer, d int64) {
func someDigits(buf *strings.Builder, d int64) {
// Print into the top, then copy down.
tmp := make([]byte, 10)
j := 10
Expand Down
Loading

0 comments on commit 81777e5

Please sign in to comment.