-
Notifications
You must be signed in to change notification settings - Fork 2
fix(lint): avoid string concat in loop #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📝 WalkthroughWalkthroughThis PR replaces direct string concatenation and fmt.Sprintf usage with strings.Builder-based accumulation in three internal functions: Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Areas requiring attention:
Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
🔇 Additional comments (3)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
internal/handshake/domain.go (1)
286-342: Good refactoring with micro-optimization opportunities.The switch to
strings.Buildercorrectly addresses the linting issue. Consider these performance improvements:
Line 315: Use
sb.WriteByte(b)instead ofsb.WriteString(string([]byte{b}))to avoid the byte-to-string conversion overhead.Line 317: Use
sb.Len() > 0instead oflen(sb.String()) > 0to avoid creating a temporary string allocation just for the length check.Line 318: Use
sb.WriteByte('.')instead ofsb.WriteString(".")for better performance with single characters.Apply this diff to optimize the builder usage:
- sb.WriteString(string([]byte{b})) + sb.WriteByte(b) } - if len(sb.String()) > 0 { - sb.WriteString(".") + if sb.Len() > 0 { + sb.WriteByte('.') }internal/dns/dns.go (2)
554-576: Refactoring looks good; consider WriteByte for single characters.The
strings.Builderrefactoring correctly addresses the linting issue. For a minor performance improvement, consider usingWriteByteinstead ofWriteStringfor single-character literals on lines 556, 558, 568, 570, 572, and 574.Example optimization:
func formatMessageAnswerSection(section []dns.RR) string { var sb strings.Builder - sb.WriteString("[ ") + sb.WriteString("[ ") // or sb.WriteByte('['); sb.WriteByte(' ') for idx, rr := range section { - sb.WriteString("< ") + sb.WriteString("< ") // or sb.WriteByte('<'); sb.WriteByte(' ') sb.WriteString(strings.ReplaceAll( strings.TrimPrefix( rr.String(), ";", ), "\t", " ", ), ) - sb.WriteString(" >") + sb.WriteString(" >") // or sb.WriteByte(' '); sb.WriteByte('>') if idx != len(section)-1 { - sb.WriteString(`,`) + sb.WriteByte(',') } - sb.WriteString(` `) + sb.WriteByte(' ') } - sb.WriteString("]") + sb.WriteByte(']') return sb.String() }
578-600: Refactoring looks good; consider WriteByte for single characters.Similar to
formatMessageAnswerSection, this function correctly usesstrings.Builder. Consider the same micro-optimization of usingWriteBytefor single-character literals on lines 580, 582, 592, 594, 596, and 598.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
internal/dns/dns.go(1 hunks)internal/handshake/domain.go(4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Analyze (go)
🔇 Additional comments (1)
internal/handshake/domain.go (1)
16-16: LGTM: Import addition is correct.The
stringsimport is required for thestrings.Builderusage indomainRecordNameDecode.
Signed-off-by: Chris Gianelloni <[email protected]>
6a935cd to
ef91b3e
Compare
| ret := "[ " | ||
| var sb strings.Builder | ||
| sb.WriteByte('[') | ||
| sb.WriteByte(' ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not sb.WriteString("[ ") to save a call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more efficient than a WriteString
Summary by CodeRabbit