Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions libs/gorules/rule_benchmark_loop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gorules

import "github.com/quasilyte/go-ruleguard/dsl"

// UseBenchmarkLoop detects classic benchmark loops over b.N and suggests
// b.Loop() (Go 1.24+; the inlining regression was fixed in 1.26 so there's
// no longer a reason to keep b.N-based loops).
func UseBenchmarkLoop(m dsl.Matcher) {
m.Match(
`for $_ := range $b.N`,
`for range $b.N`,
).
Where(m["b"].Type.Is("*testing.B")).
Report(`Use 'for $b.Loop()' instead of looping over $b.N (Go 1.24+, performance-correct in 1.26+)`)
}
4 changes: 1 addition & 3 deletions libs/structs/structdiff/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ func bench(b *testing.B, job1, job2 string) {

total := 0

b.ResetTimer()
for range b.N {
for b.Loop() {
changes, err := GetStructDiff(&x, &y, nil)
if err != nil {
b.Fatalf("error: %s", err)
}
total += len(changes)
}
b.StopTimer()

b.Logf("Total: %d / %d", total, b.N)
}
Expand Down
4 changes: 2 additions & 2 deletions libs/structs/structtag/jsontag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ var benchTags = []JSONTag{
}

func BenchmarkJSONTagName(b *testing.B) {
for range b.N {
for b.Loop() {
for _, tag := range benchTags {
tag.Name()
}
}
}

func BenchmarkJSONTagOmitEmpty(b *testing.B) {
for range b.N {
for b.Loop() {
for _, tag := range benchTags {
tag.OmitEmpty()
}
Expand Down
4 changes: 1 addition & 3 deletions libs/structs/structwalk/walktype_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ func countFields(typ reflect.Type) (int, error) {
func benchmarkWalkType(b *testing.B, tt reflect.Type) {
total := 0

b.ResetTimer()
for range b.N {
for b.Loop() {
count, err := countFields(tt)
if err != nil {
b.Fatalf("WalkType failed: %v", err)
}
total += count
}

b.StopTimer()
// Root now correctly includes embedded struct fields, so it has many more fields than JobSettings
// (3,487 vs 533) because it contains JobSettings plus other resource types and config fields
b.Logf("Counted fields in %s: %d (%d/%d)", tt, total/b.N, total, b.N)
Expand Down
Loading