Share
Embed
Transcript
20 slides
-
Topics 1 About Me 2 I/O Operations Optimization 3 Zero-Cost
Abstractions & Inline Funcs 4 Loop Optimization 5 Other Tricks -
Who Am I ? Software Engineer @ OSS Maintainer @
Formerly: @Teknasyon, @Permify, @Getir, @VirtualMetric -
I/O Operations Optimization 1. Use Buffered I/O Buffered I/O is
a technique that uses a buffer to minimize direct read and write operations to the disk. Golang's bufio package provides this functionality, allowing you to wrap your file objects in a buffered reader or writer. This can significantly speed up I/O operations by reducing the number of system calls. -
I/O Operations Optimization 2. Use io.Copy for Large Files When
you need to copy large files, io.Copy is your friend. It's optimized for copying data from one source to another using a fixed-size buffer. This method is efficient because it avoids loading the entire file into memory. -
I/O Operations Optimization 3. Parallel I/O with Goroutines Golang's concurrency
model makes it simple to perform I/O operations in parallel. By using goroutines and channels, you can process parts of a file concurrently, which is especially beneficial for CPU-bound I/O tasks. -
Zero-Cost Abstractions Examples of Zero-Cost Abstractions in Go: Interfaces: Go
interfaces allow you to define behavior contracts. When you use interfaces, the compiler can often generate efficient code that directly calls the underlying methods. Goroutines: Goroutines are threads that make concurrency easy in Go. The overhead of creating and managing goroutines is minimal, allowing you to create thousands or even millions of them. Slices: Go's slice operations are highly optimized, and working with slices is often as efficient as working with raw arrays. * https://ueokande.github.io/go-slice-tricks/ -
Inline Funcs Inline Functions Inline functions allow small and frequently
used functions to be placed inside the code by the compiler to eliminate the cost of calls. This avoids stack operations caused by function calls. Key Points: Reduces function call cost. Improves performance in loops or performance critical code blocks. -
Loop Optimization Reducing Runtime Overhead with Pre-Allocation By pre-allocating the
slice with an appropriate capacity at compile time, you can avoid these dynamic growth operations and the associated costs. -
Loop Optimization Explanation of the Optimization: Pre-Allocating Capacity Reduced ‘gcmalloc’
Calls: With pre-allocated capacity, the slice does not need to dynamically grow, which reduces the number of garbage collection-related memory allocations. Better Performance -
Padding Bytes It's a simple yet effective method that encourages
careful consideration of the struct fields we need. This approach optimizes the memory blocks pre-allocated by the Go compiler based on the data types used in struct fields, ensuring efficient memory usage. -
Padding Bytes In this way, we start to get significant
efficiency from the structs that we allocate continuously. In addition, this allows the CPU to easily navigate through the memory and perform pinpoint addressing. CLI Tool for Examine structs: dkorunic/betteralign -
An addition other than code "Distroless" Container Images Smaller Image
Size Fewer Vulnerabilities Faster Startup Time Simpler Maintenance Performance Improvements