Skip to content

Commit 05884e4

Browse files
committed
lint: string concat in loops; token expires
* ref token-expires-in (idiomatic) * other changes are strictly `perfsprint` linter - notable: - cos.JoinWords - logerr (backtrace) Signed-off-by: Alex Aizman <[email protected]>
1 parent 9d0387f commit 05884e4

File tree

11 files changed

+82
-39
lines changed

11 files changed

+82
-39
lines changed

ais/htrun.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,10 +1022,11 @@ func isBrowser(userAgent string) bool {
10221022

10231023
func (h *htrun) logerr(tag string, v any, err error) {
10241024
const maxl = 48
1025-
var efmt, msg string
1025+
var efmt string
10261026
if nlog.Stopping() {
10271027
return
10281028
}
1029+
10291030
if v != nil {
10301031
efmt = fmt.Sprintf("message: {%+v", v)
10311032
if len(efmt) > maxl {
@@ -1035,18 +1036,29 @@ func (h *htrun) logerr(tag string, v any, err error) {
10351036
}
10361037
}
10371038
efmt = tag + " response error: %v, " + efmt + " at "
1038-
msg = fmt.Sprintf(efmt, err)
1039+
1040+
// build msg
1041+
var (
1042+
sb strings.Builder
1043+
)
1044+
sb.Grow(cos.KiB)
1045+
sb.WriteString(fmt.Sprintf(efmt, err))
1046+
10391047
for i := 1; i < 4; i++ {
10401048
_, file, line, ok := runtime.Caller(i)
10411049
if !ok {
10421050
break
10431051
}
10441052
if i > 1 {
1045-
msg += " <- "
1053+
sb.WriteString(" <- ")
10461054
}
10471055
f := filepath.Base(file)
1048-
msg += fmt.Sprintf("%s:%d", f, line)
1056+
sb.WriteString(f)
1057+
sb.WriteByte(':')
1058+
sb.WriteString(strconv.Itoa(line))
10491059
}
1060+
msg := sb.String()
1061+
10501062
if cos.IsErrBrokenPipe(err) { // client went away
10511063
nlog.Infoln("Warning: " + msg)
10521064
} else {

ais/test/xaction_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package integration_test
66

77
import (
8+
"strings"
89
"testing"
910
"time"
1011

@@ -28,6 +29,16 @@ func TestXactionNotFound(t *testing.T) {
2829
tools.CheckErrIsNotFound(t, err)
2930
}
3031

32+
func _string(vec nl.StatusVec) string {
33+
var sb strings.Builder
34+
for _, ns := range vec {
35+
sb.WriteString(ns.String())
36+
sb.WriteString(", ")
37+
}
38+
s := sb.String()
39+
return s[:max(0, len(s)-2)]
40+
}
41+
3142
func TestXactionAllStatus(t *testing.T) {
3243
tests := []struct {
3344
running bool
@@ -51,7 +62,7 @@ func TestXactionAllStatus(t *testing.T) {
5162
continue
5263
}
5364
if kind != apc.ActList {
54-
tlog.Logln(vec.String())
65+
tlog.Logln(_string(vec))
5566
}
5667
for _, ns := range vec {
5768
tassert.Errorf(t, ns.Kind == kind, "kind %q vs %q", ns.Kind, kind)

bench/micro/hrw/helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ func similarFileName(bucketName string, objNum int) string {
6161
// Duplicated on purpose to avoid dependency on any AIStore code.
6262
func randNodeID(randGen *rand.Rand) string {
6363
randIP := ""
64-
for range 3 {
65-
randIP += strconv.Itoa(randGen.IntN(255)) + "."
66-
}
64+
randIP += strconv.Itoa(randGen.IntN(255)) + "."
65+
randIP += strconv.Itoa(randGen.IntN(255)) + "."
66+
randIP += strconv.Itoa(randGen.IntN(255)) + "."
6767
randIP += strconv.Itoa(randGen.IntN(255))
6868
cksum := onexxh.Checksum32S(cos.UnsafeB(randIP), xxHashSeed)
6969
nodeID := strconv.Itoa(int(cksum & 0xfffff))

cmd/authn/tok/token.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,13 @@ func (c *AISClaims) CheckPermissions(clusterID string, bck *cmn.Bck, perms apc.A
319319
//
320320

321321
func expiresIn(tm time.Time) string {
322-
now := time.Now()
323-
if now.After(tm) {
322+
dur := time.Until(tm)
323+
if dur <= 0 {
324324
return ErrTokenExpired.Error()
325325
}
326326
// round up
327-
d := tm.Sub(now) / time.Second
328-
d *= time.Second
329-
return "token expires in " + d.String()
327+
dur = dur.Round(time.Second)
328+
return "token expires in " + dur.String()
330329
}
331330

332331
func (c *AISClaims) aclForCluster(clusterID string) (perms apc.AccessAttrs, ok bool) {

cmd/cli/teb/mountpath.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,20 @@ func _capStatus(tcdf fs.Tcdf) (s string) {
188188
}
189189

190190
func _fmtMpathDisks(cdfs map[string]*fs.CDF, idx int) (s string) {
191-
var next bool
191+
var (
192+
prefix = "\n" + strings.Repeat("\t", idx) + " "
193+
sb strings.Builder
194+
next bool
195+
)
196+
sb.Grow(len(cdfs) * 64) // estimate per CDF entry
192197
for _, cdf := range cdfs {
193198
if next {
194-
s += "\n" + strings.Repeat("\t", idx) + " "
199+
sb.WriteString(prefix)
195200
}
196-
s += cdf.FS.String()
201+
sb.WriteString(cdf.FS.String())
197202
next = true
198203
}
199-
return
204+
return sb.String()
200205
}
201206

202207
func _inclStatus(st StstMap) bool {

cmd/cli/teb/performance.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,14 @@ func _metricToPrintedColName(mname string, cols []*header, metrics, n2n cos.StrK
343343
if parts[l] == "total" { // latency; see related: `stats.LatencyToCounter`
344344
l--
345345
}
346+
var sb strings.Builder
347+
sb.Grow(32)
348+
sb.WriteString(printedName)
346349
for j := 1; j < l; j++ {
347-
printedName += "-" + strings.ToUpper(parts[j])
350+
sb.WriteByte('-')
351+
sb.WriteString(strings.ToUpper(parts[j]))
348352
}
353+
printedName = sb.String()
349354

350355
// suffix
351356
switch kind {

cmn/cos/log_module.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package cos
77
import (
88
"fmt"
99
"strconv"
10+
"strings"
1011

1112
"github.com/NVIDIA/aistore/cmn/debug"
1213
)
@@ -80,21 +81,25 @@ func (l LogLevel) Validate() (err error) {
8081

8182
func (l LogLevel) String() (s string) {
8283
var (
83-
ms string
8484
n int
8585
level, modules = l.Parse()
8686
)
8787
s = strconv.Itoa(level)
8888
if modules == 0 {
8989
return
9090
}
91+
92+
var sb strings.Builder
93+
sb.Grow(len(Mods) * 16)
9194
for i, sm := range Mods {
9295
if modules&(1<<i) != 0 {
93-
ms += "," + sm
96+
sb.WriteByte(',')
97+
sb.WriteString(sm)
9498
n++
9599
}
96100
}
101+
97102
debug.Assert(n > 0, fmt.Sprintf(ferl, string(l), level, modules))
98-
s += " (module" + Plural(n) + ": " + ms[1:] + ")"
103+
s += " (module" + Plural(n) + ": " + sb.String()[1:] + ")"
99104
return
100105
}

cmn/cos/quantity.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ func ParseQuantity(quantity string) (ParsedQuantity, error) {
4141
idx int
4242
number string
4343
parsedQ ParsedQuantity
44+
sb strings.Builder
4445
)
4546
quantity = strings.ReplaceAll(quantity, " ", "")
47+
sb.Grow(len(quantity)) // at most all digits
4648
for ; idx < len(quantity) && unicode.IsDigit(rune(quantity[idx])); idx++ {
47-
number += string(quantity[idx])
49+
sb.WriteByte(quantity[idx])
4850
}
51+
number = sb.String()
4952

5053
value, err := strconv.Atoi(number)
5154
if err != nil {

cmn/cos/template.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,16 @@ func ParseAtTemplate(template string) (pt ParsedTemplate, _ error) {
378378
err error
379379
tr = TemplateRange{}
380380
left = strings.IndexByte(template, '@')
381+
sb strings.Builder
381382
number string
382383
)
383384
if left == -1 {
384385
break
385386
}
386387
for left++; len(template) > left && unicode.IsDigit(rune(template[left])); left++ {
387-
number += string(template[left])
388+
sb.WriteByte(template[left])
388389
}
390+
number = sb.String()
389391

390392
tr.Start = 0
391393
if tr.End, err = strconv.ParseInt(number, 10, 64); err != nil {

cmn/cos/url.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,25 @@ func ReparseQuery(r *http.Request) {
7272
r.URL.RawQuery = q.Encode()
7373
}
7474

75-
// JoinWords uses forward slash to join any number of words into a single path.
76-
// Returned path is prefixed with a slash.
77-
func JoinWords(w string, words ...string) (path string) {
78-
path = w
79-
if path[0] != '/' {
80-
path = "/" + path
75+
func JoinWords(w0 string, words ...string) (path string) {
76+
debug.Assert(w0 != "")
77+
var l = len(w0)
78+
if w0[0] != '/' {
79+
l++
8180
}
82-
for _, s := range words {
83-
path += "/" + s
81+
for _, w := range words {
82+
l += 1 + len(w)
8483
}
85-
return
84+
b := make([]byte, 0, l)
85+
if w0[0] != '/' {
86+
b = append(b, '/')
87+
}
88+
b = append(b, w0...)
89+
for _, w := range words {
90+
b = append(b, '/')
91+
b = append(b, w...)
92+
}
93+
return UnsafeS(b)
8694
}
8795

8896
// JoinPath joins two path elements that may (or may not) be prefixed/suffixed with a slash.

0 commit comments

Comments
 (0)