diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-24 12:19:34 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-24 12:19:34 +0300 |
| commit | cb5824fc780c36db51361ab17e2849c1e1b30ccf (patch) | |
| tree | c11e1e72af5edfd68446066f6fb4606b8b54c38b | |
| parent | 0ba51c672d300de1f030f94718c60617a0b60698 (diff) | |
rpn: use strings.Builder in Show() for loop concatenation
Replace += string concatenation in Show() with strings.Builder
for O(n) performance instead of O(n^2). ListConstants() already
used strings.Builder.
| -rw-r--r-- | internal/rpn/operations_stack.go | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/internal/rpn/operations_stack.go b/internal/rpn/operations_stack.go index 85cd057..c028ab7 100644 --- a/internal/rpn/operations_stack.go +++ b/internal/rpn/operations_stack.go @@ -3,7 +3,10 @@ package rpn -import "fmt" +import ( + "fmt" + "strings" +) // stack manipulation operators @@ -62,18 +65,19 @@ func (o *Operations) Show(stack *Stack) (string, error) { } vals := stack.Values() - var result string + var sb strings.Builder for i, val := range vals { if i > 0 { - result += " " + sb.WriteByte(' ') } // Append metric suffix for non-Cool metrics m := val.Metric() if m != nil && m.Category != Universal { - result += val.String() + m.Name + sb.WriteString(val.String()) + sb.WriteString(m.Name) } else { - result += val.String() + sb.WriteString(val.String()) } } - return result, nil + return sb.String(), nil } |
