Go: Simple optimization notes
When your VPS is running multiple service applications, but one of them sometimes takes up all the resources, making it impossible to access the server through ssh. You move to using a Kubernetes cluster and set limits for all applications. We then saw some applications being restarted as the OOM-killer fixed the memory "leak" issue.
Of course, OOM is not always a leak problem, it can also be a resource overrun. Leakage problems are most likely caused by program errors. The topic we are talking about today is how to avoid this situation as much as possible.
Excessive resource consumption can hurt the wallet, which means we need to take immediate action.
Don’t optimize prematurely
Now let’s talk about optimization. Hopefully you can understand why we shouldn’t optimize prematurely!
First, optimization may be useless work. Because we should study the entire application first, and your code will most likely not be the bottleneck. What we need is quick results, MVP (Minimum Viable Product, minimum viable product), and then we will consider its problems. #Second, optimization must have a basis. That is to say, every optimization should be based on a benchmark, and we must prove how much profit it brings us. #Third, optimization may bring complexity. What you need to know is that most optimizations make your code less readable. You need to strike this balance.
Optimization suggestions
Now we give some practical suggestions according to the standard entity classification in Go.
1. Arrays and slices
Allocate memory for slices in advance
Try to use the third parameter: <span style="font-size: 15px;"> make([]T, 0, len)</span>
If you don’t know the exact number of elements and the slice is short-lived, you can allocate a larger size to ensure that the slice does not will grow.
Don’t forget to use copy
Try not to use append when copying, such as when merging two or more slices.
Iterate correctly
For a slice containing many elements or large elements, use for to get a single element. This way unnecessary duplication will be avoided.
Multiple Slices
If some operation is performed on the incoming slice and returns a modified result, we can return it. This avoids new memory allocation.
Don't leave unused portions of the slice
If you need to cut off a small piece from the slice and use it only, the main part of the slice will also be retained. The correct approach is to use a new copy of this small slice and throw the old slice to the GC.
2. Strings
Correct splicing
If splicing strings can be completed in one statement, use <span style="font-size: 15px;"> </span> Operator. If you need to do this in a loop, use <span style="font-size: 15px;">string.Builder</span>, and use its <span style="font-size: 15px;">Grow</span> Method pre-specifies the size of <span style="font-size: 15px;">Builder</span> to reduce the number of memory allocations.
Conversion optimization
string and []byte are very similar in underlying structure, and sometimes strong conversion can be used between these two types to avoid memory allocation.
String resident
Strings can be pooled, thus helping the compiler store the same string only once.
Avoid allocation
We can use map (cascade) instead of composite keys, we can use byte slices. Try not to use the <span style="font-size: 15px;">fmt</span> package because all its methods use reflection.
3. Structure
Avoid copying large structures
The small structure we understand is no more than 4 fields and no more than one machine word size.
Some typical copy scenes
Project to interface -
Channel reception and transmission Replace elements in the map Add elements to the slice Iterate (range)
Avoid accessing structure fields through pointers
Dereferencing is expensive and we should do it as little as possible, especially in loops. It also loses the ability to use fast registers.
Handling small structures
This work is optimized by the editor, which means it is cheap.
Use alignment to reduce structure size
We can reduce the size of a structure by aligning it (arranging them in the correct order according to the size of the fields) size itself.
4. Functions
Use inline functions or inline them yourself
Try writing small functions that can be inlined by the compiler and it will Fast, even faster than embedding the code in the function yourself. This is especially true for hot paths.
Which ones will not be inline
recovery select block Type declaration defer -
##goroutine for-range
Try to use small parameters because of their duplication will be optimized. Try to keep replication and stack growth balanced on the GC load. Avoid large numbers of parameters and let your program use fast registers (their number is limited).
Named return valuesThis seems more efficient than declaring these variables in the function body.
Save intermediate resultsHelp the compiler optimize your code, save the intermediate results, and then there will be more options to optimize your code.
Use defer carefullyTry not to use defer, or at least don't use it in a loop.
Help hot pathAvoid allocating memory in the hot path, especially for short-lived objects. Make the most common branches (if, switch)
5. MapAllocate memory in advanceSame as slice, when initializing map, specify its size .
Use empty structs as valuesstruct{} is nothing (takes up no memory), so it is very beneficial to use it when passing signals for example.
Clear map
map can only grow, not shrink. When we need to reset the map, deleting all its elements won't help.
Try not to use pointers in keys and values
If the map does not contain pointers, then the GC will not waste precious time on it. Strings also use pointers, so you should use byte arrays instead of strings as keys.
Reduce the number of modifications
Similarly, we don’t want to use pointers, but we can use a combination of map and slice, storing the keys in the map and the values in the slice. This way we can change the value without restrictions.
6. Interface
Calculate memory allocation
Remember that when you want to assign a value to an interface, you first need to copy it somewhere, Then paste the pointer to it. The key is to copy. It turns out that the cost of boxing and unboxing the interface will be approximately the same as an allocation of the struct size.
Selecting the optimal type
In some cases, there is no allocation during boxing and unboxing of an interface. For example, small or Boolean values of variables and constants, structures with one simple field, pointers (including map, channel, func)
Avoid memory allocation
As elsewhere, try to avoid unnecessary allocations. For example assigning one interface to another instead of boxing twice.
Use only when needed
Avoid using interfaces in frequently called function parameters and return results. We don't need additional unpacking operations. Reduce the frequency of using interface method calls as it prevents inlining.
7. Pointers, Channels, Bounds Checks
Avoid unnecessary dereferences
Especially in loops as it turns out to be too expensive . Dereferencing is something we don't want to do at our own expense.
Using channels is inefficient
Channel synchronization is slower than other synchronization primitive methods. In addition, the more cases in select, the slower our program will be. However, select, case plus default have been optimized.
Avoid unnecessary bounds checks
This is also expensive and we should avoid it. For example, check (get) the maximum slice index only once, not multiple times. It’s best to try getting the extreme option now.
Summary
Throughout this article, we saw some of the same optimization rules.
Help the compiler make the right decision and it will thank you. Allocate memory at compile time, use intermediate results, and try to keep your code readable.
I reiterate that for implicit optimization, benchmarks are mandatory. If something that worked yesterday won't work tomorrow because the compiler changes too quickly between versions, and vice versa.
Don’t forget to use Go’s built-in profiling and tracing tools.
The above is the detailed content of Go: Simple optimization notes. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
How do you work with environment variables in Golang?
Aug 19, 2025 pm 02:06 PM
Goprovidesbuilt-insupportforhandlingenvironmentvariablesviatheospackage,enablingdeveloperstoread,set,andmanageenvironmentdatasecurelyandefficiently.Toreadavariable,useos.Getenv("KEY"),whichreturnsanemptystringifthekeyisnotset,orcombineos.Lo
How to implement a generic LRU cache in Go
Aug 18, 2025 am 08:31 AM
Use Go generics and container/list to achieve thread-safe LRU cache; 2. The core components include maps, bidirectional linked lists and mutex locks; 3. Get and Add operations ensure concurrency security through locks, with a time complexity of O(1); 4. When the cache is full, the longest unused entry will be automatically eliminated; 5. In the example, the cache with capacity of 3 successfully eliminated the longest unused "b". This implementation fully supports generic, efficient and scalable.
What is the reason for the rise of OKB coins? A detailed explanation of the strategic driving factors behind the surge in OKB coins
Aug 29, 2025 pm 03:33 PM
What is the OKB coin in the directory? What does it have to do with OKX transaction? OKB currency use supply driver: Strategic driver of token economics: XLayer upgrades OKB and BNB strategy comparison risk analysis summary In August 2025, OKX exchange's token OKB ushered in a historic rise. OKB reached a new peak in 2025, up more than 400% in just one week, breaking through $250. But this is not an accidental surge. It reflects the OKX team’s thoughtful shift in token model and long-term strategy. What is OKB coin? What does it have to do with OKX transaction? OKB is OK Blockchain Foundation and
Parsing RSS and Atom Feeds in a Go Application
Aug 18, 2025 am 02:40 AM
Use the gofeed library to easily parse RSS and Atomfeed. First, install the library through gogetgithub.com/mmcdole/gofeed, then create a Parser instance and call the ParseURL or ParseString method to parse remote or local feeds. The library will automatically recognize the format and return a unified feed structure. Then iterate over feed.Items to get standardized fields such as title, link, and publishing time. It is also recommended to set HTTP client timeouts, handle parsing errors, and use cache optimization performance to ultimately achieve simple, efficient and reliable feed resolution.
How to handle panics in a goroutine in Go
Aug 24, 2025 am 01:55 AM
Tohandlepanicsingoroutines,usedeferwithrecoverinsidethegoroutinetocatchandmanagethemlocally.2.Whenapanicisrecovered,logitmeaningfully—preferablywithastacktraceusingruntime/debug.PrintStack—fordebuggingandmonitoring.3.Onlyrecoverfrompanicswhenyoucanta
Performance Comparison: Java vs. Go for Backend Services
Aug 14, 2025 pm 03:32 PM
Gotypicallyoffersbetterruntimeperformancewithhigherthroughputandlowerlatency,especiallyforI/O-heavyservices,duetoitslightweightgoroutinesandefficientscheduler,whileJava,thoughslowertostart,canmatchGoinCPU-boundtasksafterJIToptimization.2.Gouseslessme
How do you define and call a function in Go?
Aug 14, 2025 pm 06:22 PM
In Go, defining and calling functions use the func keyword and following fixed syntax, first clarify the answer: the function definition must include name, parameter type, return type and function body, and pass in corresponding parameters when calling; 1. Use funcfunctionName(params) returnType{} syntax when defining functions, such as funcadd(a,bint)int{return b}; 2. Support multiple return values, such as funcdivide(a,bfloat64)(float64,bool){}; 3. Calling functions directly uses the function name with brackets to pass parameters, such as result:=add(3,5); 4. Multiple return values can be received by variables or
How to use Go for building blockchain applications
Aug 17, 2025 am 03:04 AM
To start building blockchain applications using Go, you must first master the core concepts of blockchain, 1. Understand blocks, hashing, immutability, consensus mechanism, P2P network and digital signatures; 2. Install Go and initialize projects, and use Go modules to manage dependencies; 3. Build a simple blockchain to learn principles by defining the block structure, implementing SHA-256 hashing, creating blockchain slices, generating new blocks and verification logic; 4. Use mature frameworks and libraries such as CosmosSDK, TendermintCore, Go-Ethereum or Badger in actual development to avoid duplicate wheels; 5. Use Go's goroutine and net/http or gorilla/websocke in actual development; 5. Use Go's goroutine and net/http or gorilla/websocke


