gemini-code-assist[bot] commented on code in PR #39288:
URL: https://github.com/apache/beam/pull/39288#discussion_r3561767634
##########
sdks/go/container/tools/buffered_logging.go:
##########
@@ -63,8 +63,15 @@ func (b *BufferedLogger) Write(p []byte) (int, error) {
if b.logs == nil {
b.logs = make([]string, 0, initialLogSize)
}
- b.logs = append(b.logs, b.builder.String())
- b.builder.Reset()
+ s := b.builder.String()
+ if lastNL := strings.LastIndex(s, "\n"); lastNL != -1 {
+ lines := strings.Split(s[:lastNL], "\n")
+ for _, line := range lines {
+ b.logs = append(b.logs, strings.TrimSuffix(line, "\r"))
+ }
+ b.builder.Reset()
+ b.builder.WriteString(s[lastNL+1:])
+ }
Review Comment:

Using `strings.LastIndex` followed by `strings.Split` allocates a slice of
strings and performs multiple scans over the string. Since logging can be a
high-throughput path, we can optimize this by scanning the string once using
`strings.IndexByte` in a loop. This avoids the slice allocation and is more
efficient.
```go
s := b.builder.String()
start := 0
for {
nl := strings.IndexByte(s[start:], '\n')
if nl == -1 {
break
}
line := s[start : start+nl]
b.logs = append(b.logs, strings.TrimSuffix(line, "\r"))
start += nl + 1
}
if start > 0 {
b.builder.Reset()
if start < len(s) {
b.builder.WriteString(s[start:])
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]