-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Writing Fast and Lean Code
It's imperative to keep latency and memory consumption of Istio components low. To that end, we present here a few general guidelines and pointers.
Other docs you may enjoy:
Go is a garbage collected environment. This is great for correctness, but it can lead to substantial perf issues. Allocating memory is by no means free and should be done carefully. We want to minimize the occurrence of garbage collection and reduce the amount of work the GC is asked to perform.
Preallocate memory and reuse it over and over again. This not only reduces strain on the GC, it also results
in considerably better CPU cache and TLB efficiency which can make your code 10x faster. The Go
sync.Pool
type can be useful here.
Having distinct objects in memory is inherently expensive:
- You need at least 8 bytes to point to the object
- There is hidden overhead associated with each object (probably between 8 to 16 bytes per object)
- Writing to references tends to be more expensive due to GC write barriers
Programmers coming from Java aren't used to this distinction since Java doesn't have general support for value types and thus everything is an object and pointers abound. But Go does have good value semantics, so we use them.
So prefer:
type MyContainer struct {
inlineStruct OtherStuff
}
When possible as opposed to:
type MyContainer struct {
outoflineStruct *OtherStruct
}
For example, consider using the second method signature rather than the first one as it avoids potentially large allocations.
No: func (r *Reader) Read() ([]byte, error)
Yes: func (r *Reader) Read(buf []byte) (int, error)
Goroutines are said to be cheap, but they need to be used judiciously otherwise performance will suffer.
- Don’t create goroutines in the main request serving path. Prefer to create them a priori and have them wait for input.
Human beings have proven incapable of predicting the real-world performance of complex systems. Performance tuning should therefore follow the rule of the three Ms:
- Measure before doing an optimization
- Measure after doing an optimization
- Measure continuously as part of every check-in
Visit istio.io to learn how to use Istio.
- Preparing for Development Mac
- Preparing for Development Linux
- Troubleshooting Development Environment
- Repository Map
- GitHub Workflow
- Github Gmail Filters
- Using the Code Base
- Developing with Minikube
- Remote Debugging
- Verify your Docker Environment
- Istio Test Framework
- Working with Prow
- Test Grid
- Code Coverage FAQ
- Writing Good Integration Tests
- Test Flakes
- Release Manager Expectations
- Preparing Istio Releases
- 1.5 Release Information
- 1.6 Release Information
- 1.7 Release Information
- 1.8 Release Information
- 1.9 Release Information
- 1.10 Release Information
- 1.11 Release Information
- 1.12 Release Information
- 1.13 Release Information
- 1.14 Release Information
- 1.15 Release Information
- 1.16 Release Information
- 1.17 Release Information
- 1.18 Release Information
- 1.19 Release Information
- 1.20 Release Information
- 1.21 Release Information
- 1.22 Release Information
- 1.23 Release Information
- 1.24 Release Information