<!--
title: Precompute over recalculate
domain: app-frameworks
topic: Performance Optimization
language: Go
source: gin-gonic/gin
updated: 2025-05-20
url: https://awesomereviewers.com/reviewers/gin-precompute-over-recalculate/
-->

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:
- Cache computed values that don't change between requests
- Avoid redundant parsing operations on each request
- Eliminate unnecessary pointer operations in hot paths

Example:
```go
// 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:
```go
// Prefer this when possible:
bytes, err := bson.Marshal(r.Data)

// Over this:
bytes, err := bson.Marshal(&r.Data)
```
