Skip to content

Commit 29ab4ac

Browse files
authored
refactor: use strings.Builder for string accumulation (#513)
Replace repeated string concatenation with `strings.Builder` to reduce allocations and improve performance. Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
1 parent 2a705e0 commit 29ab4ac

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

builder/vmware/common/driver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func NewDriver(dconfig *DriverConfig, config *SSHConfig, vmName string) (Driver,
352352
return nil, fmt.Errorf("error finding a driver for %s", runtime.GOOS)
353353
}
354354

355-
errs := ""
355+
var errs strings.Builder
356356
for _, driver := range drivers {
357357
err := driver.Verify()
358358

@@ -362,10 +362,10 @@ func NewDriver(dconfig *DriverConfig, config *SSHConfig, vmName string) (Driver,
362362
}
363363

364364
log.Printf("[INFO] Skipping %T because it failed with the following error %s", driver, err)
365-
errs += "* " + err.Error() + "\n"
365+
errs.WriteString("* " + err.Error() + "\n")
366366
}
367367

368-
return nil, fmt.Errorf("driver initialization failed. fix at least one driver to continue:\n%s", errs)
368+
return nil, fmt.Errorf("driver initialization failed. fix at least one driver to continue:\n%s", errs.String())
369369
}
370370

371371
// runAndLog executes the given command, logs its execution, and returns its stdout, stderr, and any encountered error.

builder/vmware/common/driver_parser_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ func uncommentFromString(s string) string {
3636
inCh := consumeString(s)
3737
out := uncomment(inCh)
3838

39-
result := ""
39+
var result strings.Builder
4040
for item := range out {
41-
result += string(item)
41+
result.WriteString(string(item))
4242
}
43-
return result
43+
return result.String()
4444
}
4545

4646
func TestParserUncomment(t *testing.T) {
@@ -492,11 +492,11 @@ func TestParserReadNetworkMap(t *testing.T) {
492492
}
493493

494494
func collectIntoString(in chan byte) string {
495-
result := ""
495+
var result strings.Builder
496496
for item := range in {
497-
result += string(item)
497+
result.WriteString(string(item))
498498
}
499-
return result
499+
return result.String()
500500
}
501501

502502
func TestParserConsumeUntilSentinel(t *testing.T) {

0 commit comments

Comments
 (0)