Skip to content

Commit

Permalink
🥯 all: relax b.Loop usage
Browse files Browse the repository at this point in the history
The documentation of b.Loop mentions that the compiler has a special case for it, and it "must be written in exactly" the form of "for b.Loop() { ... }".

Turns out it's actually not that restrictive. As of Go 1.24, the check is at https://github.com/golang/go/blob/master/src/cmd/compile/internal/inline/interleaved/interleaved.go. It has no restrictions on the init and post part of the for loop.

Given this knowledge, this commit moves scattered "var i int" and "i++" into the for statement.
  • Loading branch information
database64128 committed Feb 13, 2025
1 parent cb8ba33 commit 6d3ed14
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
4 changes: 1 addition & 3 deletions domainset/matcher_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,10 @@ func TestDomainMapMatcher(t *testing.T) {

func benchmarkDomainMatcher(b *testing.B, count int, name string, m Matcher) {
b.Run(fmt.Sprintf("%d/%s/Hit", count, name), func(b *testing.B) {
var i int
for b.Loop() {
for i := 0; b.Loop(); i++ {
if !m.Match(testDomains[i%count]) {
b.Fatal("unexpected miss")
}
i++
}
})
b.Run(fmt.Sprintf("%d/%s/Miss", count, name), func(b *testing.B) {
Expand Down
4 changes: 1 addition & 3 deletions domainset/matcher_suffix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,10 @@ func TestSuffixTrieMatcher(t *testing.T) {

func benchmarkSuffixMatcher(b *testing.B, count int, name string, m Matcher) {
b.Run(fmt.Sprintf("%d/%s/Hit", count, name), func(b *testing.B) {
var i int
for b.Loop() {
for i := 0; b.Loop(); i++ {
if !m.Match(testSuffixes[i%count]) {
b.Fatal("unexpected miss")
}
i++
}
})
b.Run(fmt.Sprintf("%d/%s/Miss/Short", count, name), func(b *testing.B) {
Expand Down

0 comments on commit 6d3ed14

Please sign in to comment.