Perform expensive calculations during initialization rather than on each request, and avoid unnecessary operations in frequently executed code paths. This significantly improves performance in high-traffic applications.
Perform expensive calculations during initialization rather than on each request, and avoid unnecessary operations in frequently executed code paths. This significantly improves performance in high-traffic applications.
Key practices:
Example:
// Instead of this:
func (c *Context) ClientIP() string {
// Parse trusted proxies on every request
for _, trustedProxy := range e.TrustedProxies {
// Parse CIDR each time
}
return ip
}
// Do this:
type Engine struct {
// Precomputed during initialization
trustedCIDR []*net.IPNet
// ...
}
func (e *Engine) Run() {
// Precompute once during startup
e.trustedCIDR = parseTrustedProxies(e.TrustedProxies)
// ...
}
func (c *Context) ClientIP() string {
// Use precomputed values
return ip
}
Similarly, for operations like BSON marshaling, avoid unnecessary pointer indirection:
// Prefer this when possible:
bytes, err := bson.Marshal(r.Data)
// Over this:
bytes, err := bson.Marshal(&r.Data)
Enter the URL of a public GitHub repository