Skip to content

Commit

Permalink
monitor: optimize cpu costs
Browse files Browse the repository at this point in the history
unicode.IsSpace is expensive
  • Loading branch information
fanyang89 authored and 1023280072 committed Aug 23, 2023
1 parent f6715d2 commit c24047f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
5 changes: 1 addition & 4 deletions tcpmon/cmd_ss.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"strings"
"time"
"unicode"

"github.com/cockroachdb/errors"
"github.com/go-cmd/cmd"
Expand Down Expand Up @@ -249,9 +248,7 @@ func ParseSSOutput(t *TcpMetric, out []string) {

s := &SocketMetric{}
for _, line := range out {
fields := strings.FieldsFunc(line, func(c rune) bool {
return unicode.IsSpace(c)
})
fields := strings.FieldsFunc(line, SplitSpace)

var exist bool
if len(fields) == 0 {
Expand Down
12 changes: 5 additions & 7 deletions tcpmon/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,10 @@ func FileExists(s string) (bool, error) {
return true, nil
}

var SplitSpace = SplitChar(' ')

var SplitNewline = SplitChar('\n')
func SplitNewline(c rune) bool {
return c == '\n'
}

func SplitChar(s rune) func(rune) bool {
return func(c rune) bool {
return c == s
}
func SplitSpace(c rune) bool {
return c == '\n' || c == '\r' || c == '\t' || c == ' '
}

0 comments on commit c24047f

Please sign in to comment.