Skip to content

Commit

Permalink
v3: Optimize IsFromLocal() performance (#3140)
Browse files Browse the repository at this point in the history
Optimize IsFromLocal()
  • Loading branch information
gaby authored Sep 23, 2024
1 parent fbc24e8 commit f8c514c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
14 changes: 1 addition & 13 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1841,21 +1841,9 @@ func (c *DefaultCtx) IsProxyTrusted() bool {
return false
}

var localHosts = [...]string{"127.0.0.1", "::1"}

// IsLocalHost will return true if address is a localhost address.
func (*DefaultCtx) isLocalHost(address string) bool {
for _, h := range localHosts {
if address == h {
return true
}
}
return false
}

// IsFromLocal will return true if request came from local.
func (c *DefaultCtx) IsFromLocal() bool {
return c.isLocalHost(c.fasthttp.RemoteIP().String())
return c.fasthttp.RemoteIP().IsLoopback()
}

// Bind You can bind body, cookie, headers etc. into the map, map slice, struct easily by using Binding method.
Expand Down
2 changes: 0 additions & 2 deletions ctx_interface_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6352,3 +6352,61 @@ func Benchmark_Ctx_IsProxyTrusted(b *testing.B) {
})
})
}

func Benchmark_Ctx_IsFromLocalhost(b *testing.B) {
// Scenario without localhost check
b.Run("Non_Localhost", func(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().SetRequestURI("http://google.com:8080/test")
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
c.IsFromLocal()
}
app.ReleaseCtx(c)
})

// Scenario without localhost check in parallel
b.Run("Non_Localhost_Parallel", func(b *testing.B) {
app := New()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().SetRequestURI("http://google.com:8080/test")
for pb.Next() {
c.IsFromLocal()
}
app.ReleaseCtx(c)
})
})

// Scenario with localhost check
b.Run("Localhost", func(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().SetRequestURI("http://localhost:8080/test")
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
c.IsFromLocal()
}
app.ReleaseCtx(c)
})

// Scenario with localhost check in parallel
b.Run("Localhost_Parallel", func(b *testing.B) {
app := New()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().SetRequestURI("http://localhost:8080/test")
for pb.Next() {
c.IsFromLocal()
}
app.ReleaseCtx(c)
})
})
}

1 comment on commit f8c514c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: f8c514c Previous: 6e74114 Ratio
Benchmark_Ctx_Send 7.171 ns/op 0 B/op 0 allocs/op 4.342 ns/op 0 B/op 0 allocs/op 1.65
Benchmark_Ctx_Send - ns/op 7.171 ns/op 4.342 ns/op 1.65
Benchmark_Utils_GetOffer/1_parameter 202.4 ns/op 0 B/op 0 allocs/op 131.1 ns/op 0 B/op 0 allocs/op 1.54
Benchmark_Utils_GetOffer/1_parameter - ns/op 202.4 ns/op 131.1 ns/op 1.54
Benchmark_Middleware_BasicAuth - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_Middleware_BasicAuth_Upper - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth_Upper - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_CORS_NewHandler - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandler - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflight - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflight - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard 1036 ns/op 104 B/op 5 allocs/op 689.8 ns/op 0 B/op 0 allocs/op 1.50
Benchmark_CORS_NewHandlerPreflightWildcard - ns/op 1036 ns/op 689.8 ns/op 1.50
Benchmark_CORS_NewHandlerPreflightWildcard - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_Middleware_CSRF_GenerateToken - B/op 526 B/op 334 B/op 1.57
Benchmark_Middleware_CSRF_GenerateToken - allocs/op 10 allocs/op 6 allocs/op 1.67

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.