From 362fa4b9c4058915c41458e68fd0ac297f2625cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 21 Dec 2023 09:51:14 +0200 Subject: [PATCH] Avoid repeated regexp compilations --- cpu/cpu_solaris.go | 5 +++-- disk/disk_solaris.go | 5 +++-- host/host_linux.go | 13 ++++++++++--- net/net_solaris.go | 5 +++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cpu/cpu_solaris.go b/cpu/cpu_solaris.go index f828c843e..4231ad168 100644 --- a/cpu/cpu_solaris.go +++ b/cpu/cpu_solaris.go @@ -36,6 +36,8 @@ func Times(percpu bool) ([]TimesStat, error) { return TimesWithContext(context.Background(), percpu) } +var kstatSplit = regexp.MustCompile(`[:\s]+`) + func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/") if err != nil { @@ -47,9 +49,8 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { kern := make(map[float64]float64) iowt := make(map[float64]float64) // swap := make(map[float64]float64) - re := regexp.MustCompile(`[:\s]+`) for _, line := range strings.Split(string(kstatSysOut), "\n") { - fields := re.Split(line, -1) + fields := kstatSplit.Split(line, -1) if fields[0] != "cpu_stat" { continue } diff --git a/disk/disk_solaris.go b/disk/disk_solaris.go index 934d651ff..5d6ea8653 100644 --- a/disk/disk_solaris.go +++ b/disk/disk_solaris.go @@ -83,6 +83,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro return ret, err } +var kstatSplit = regexp.MustCompile(`[:\s]+`) + func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { var issolaris bool if runtime.GOOS == "illumos" { @@ -107,7 +109,6 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC writesarr := make(map[string]uint64) rtimearr := make(map[string]uint64) wtimearr := make(map[string]uint64) - re := regexp.MustCompile(`[:\s]+`) // in case the name is "/dev/sda1", then convert to "sda1" for i, name := range names { @@ -115,7 +116,7 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC } for _, line := range lines { - fields := re.Split(line, -1) + fields := kstatSplit.Split(line, -1) if len(fields) == 0 { continue } diff --git a/host/host_linux.go b/host/host_linux.go index b64432229..0618a6967 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -339,13 +339,15 @@ func getSlackwareVersion(contents []string) string { return c } +var redhatishReleaseMatch = regexp.MustCompile(`release (\w[\d.]*)`) + func getRedhatishVersion(contents []string) string { c := strings.ToLower(strings.Join(contents, "")) if strings.Contains(c, "rawhide") { return "rawhide" } - if matches := regexp.MustCompile(`release (\w[\d.]*)`).FindStringSubmatch(c); matches != nil { + if matches := redhatishReleaseMatch.FindStringSubmatch(c); matches != nil { return matches[1] } return "" @@ -362,12 +364,17 @@ func getRedhatishPlatform(contents []string) string { return f[0] } +var ( + suseVersionMatch = regexp.MustCompile(`VERSION = ([\d.]+)`) + susePatchLevelMatch = regexp.MustCompile(`PATCHLEVEL = (\d+)`) +) + func getSuseVersion(contents []string) string { version := "" for _, line := range contents { - if matches := regexp.MustCompile(`VERSION = ([\d.]+)`).FindStringSubmatch(line); matches != nil { + if matches := suseVersionMatch.FindStringSubmatch(line); matches != nil { version = matches[1] - } else if matches := regexp.MustCompile(`PATCHLEVEL = ([\d]+)`).FindStringSubmatch(line); matches != nil { + } else if matches = susePatchLevelMatch.FindStringSubmatch(line); matches != nil { version = version + "." + matches[1] } } diff --git a/net/net_solaris.go b/net/net_solaris.go index 7f1f5c86f..79d8ac30e 100644 --- a/net/net_solaris.go +++ b/net/net_solaris.go @@ -23,6 +23,8 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) { return IOCountersWithContext(context.Background(), pernic) } +var kstatSplit = regexp.MustCompile(`[:\s]+`) + func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { // collect all the net class's links with below statistics filterstr := "/^(?!vnic)/::phys:/^rbytes64$|^ipackets64$|^idrops64$|^ierrors$|^obytes64$|^opackets64$|^odrops64$|^oerrors$/" @@ -47,9 +49,8 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, odrops64arr := make(map[string]uint64) oerrorsarr := make(map[string]uint64) - re := regexp.MustCompile(`[:\s]+`) for _, line := range lines { - fields := re.Split(line, -1) + fields := kstatSplit.Split(line, -1) interfaceName := fields[0] instance := fields[1] switch fields[3] {