Performance becomes important when a Go application needs to process large amounts of data. Image processing, numerical calculations, compression, cryptography, machine learning, and analytics workloads often perform the same operation repeatedly across thousands or millions of values.
A normal Go loop processes these values one at a time. Modern processors, however, support a technique called SIMD that allows a single instruction to operate on multiple data elements simultaneously.
Until recently, developers who wanted direct SIMD support in Go generally had to write assembly code or depend on external libraries. That approach required processor-specific knowledge and made code harder to maintain.
Go 1.26 introduced experimental architecture-specific SIMD APIs for amd64. Go 1.27 expanded this work with support for arm64 and WebAssembly and introduced a portable SIMD package.
These APIs give Go developers a new way to optimize data-intensive code without immediately writing assembly. The portable simd package hides many processor differences, while simd/archsimd provides more direct access to architecture-specific instructions.
This article explains how SIMD works in Go, how to use the new APIs, and how to decide whether vectorization is worth introducing into an application.
What Is SIMD?
SIMD stands for Single Instruction, Multiple Data. It is a processor capability that performs the same operation on several values at once.
Consider a simple operation that adds two arrays:
func addArrays(a, b, result []float32) {
for i := 0; i < len(a); i++ {
result[i] = a[i] + b[i]
}
}
This function processes one pair of values during each loop iteration.
A processor with SIMD support can load multiple values into a vector register and perform an addition across all elements in that register.
For example, a 128-bit vector can hold four 32-bit floating-point values. A 256-bit vector can hold eight, and a 512-bit vector can hold sixteen.
The exact vector width available depends on the processor and its supported instruction sets.
<text color="secondary" size="sm">Conceptual comparison of scalar and vector processing</text>
Processing style | Operation |
|---|
Scalar | Add one pair of values at a time |
128-bit SIMD | Add four float32 pairs in one vector operation |
256-bit SIMD | Add eight float32 pairs in one vector operation |
512-bit SIMD | Add sixteen float32 pairs in one vector operation |
These examples describe how many values fit into the registers, not guaranteed performance improvements. Actual speed depends on memory access, instruction selection, data size, and other factors.
SIMD is particularly useful when an algorithm performs the same calculation repeatedly on independent values.
Why Go Introduced Native SIMD APIs
Go has traditionally provided excellent support for concurrency through goroutines and channels. However, concurrency and SIMD solve different performance problems.
Goroutines allow multiple tasks to execute concurrently. SIMD allows one processor instruction to perform an operation on multiple data elements.
Before the new SIMD APIs, developers who needed direct access to vector instructions generally had to use Go assembly or external libraries.
That approach introduced several challenges:
Assembly code is more difficult to write and maintain.
Processor-specific implementations require separate code paths.
Supporting multiple architectures increases development effort.
Testing low-level operations requires additional care.
Small performance improvements may not justify maintaining custom assembly.
The new APIs aim to make vectorized programming more accessible while preserving the ability to use architecture-specific instructions when necessary.
Go provides two main packages for this purpose:
Package | Purpose |
|---|
simd | Portable, vector-size-independent SIMD operations |
simd/archsimd | Architecture-specific SIMD operations and vector types |
Both packages are experimental in Go 1.27 and require the SIMD experiment to be enabled during compilation.
Understanding the Portable simd Package
The simd package provides a portable interface for vector operations.
Instead of requiring developers to choose a fixed vector width, it uses vector types whose size is determined by the implementation.
For example:
Int8s represents a vector of signed 8-bit integers.
Int32s represents a vector of signed 32-bit integers.
Uint8s represents a vector of unsigned 8-bit integers.
Float32s represents a vector of 32-bit floating-point values.
Float64s represents a vector of 64-bit floating-point values.
The actual number of elements in a vector is not fixed in the application code. Developers can use the Len() method to determine how many elements fit into the current vector.
This makes it possible to write an algorithm without hardcoding assumptions about whether the processor supports 128-bit, 256-bit, or 512-bit operations.
Enabling SIMD Support
The SIMD APIs are experimental in Go 1.27. To use them, enable the experiment when building or testing the application.
On Linux and macOS:
GOEXPERIMENT=simd go build ./...
To run tests:
GOEXPERIMENT=simd go test ./...
On PowerShell:
$env:GOEXPERIMENT="simd"
go test ./...
The experiment must be enabled when compiling the application. It is not enough to set the environment variable only when running an already compiled executable.
Because the APIs are experimental, developers should review the documentation for the exact Go release they are using before adopting them in production.
Adding Two Slices With the Portable SIMD API
A good way to understand the new API is to implement a simple array addition operation.
First, consider the regular implementation:
package main
func addScalar(
dst, a, b []float32,
) {
if len(dst) != len(a) || len(a) != len(b) {
panic("slice lengths must match")
}
for i := range a {
dst[i] = a[i] + b[i]
}
}
This implementation uses a normal Go loop. It is simple, portable, and easy to maintain.
Now consider a SIMD implementation:
//go:build goexperiment.simd
package main
import "simd"
func addSIMD(
dst, a, b []float32,
) {
if len(dst) != len(a) || len(a) != len(b) {
panic("slice lengths must match")
}
var vector simd.Float32s
width := vector.Len()
i := 0
for ; i+width <= len(a); i += width {
x := simd.LoadFloat32s(a[i : i+width])
y := simd.LoadFloat32s(b[i : i+width])
x.Add(y).Store(dst[i : i+width])
}
for ; i < len(a); i++ {
dst[i] = a[i] + b[i]
}
}
The implementation follows a simple process.
First, it creates a Float32s vector and calls Len() to determine the number of values supported by the current vector.
Next, it processes complete groups of values. LoadFloat32s loads a slice of float32 values into a vector. The Add method performs element-wise addition, and Store writes the result back into the destination slice.
Finally, a regular loop processes any remaining elements that do not fill a complete vector.
This final loop is important. The input length may not be evenly divisible by the vector width.
For example, if the current vector contains eight elements and the input contains eighteen values, the implementation processes sixteen values through SIMD and the remaining two through scalar operations.
This technique is commonly called handling the vector tail.
Handling Partial Vectors With LoadFloat32sPart
The portable SIMD API also provides partial loading and storing operations.
LoadFloat32sPart loads as many elements as are available and zero-fills the remaining vector lanes. It also returns the number of elements loaded.
For example:
package main
import (
"fmt"
"simd"
)
func main() {
values := []float32{10, 20, 30}
vector, loaded := simd.LoadFloat32sPart(values)
fmt.Println("Elements loaded:", loaded)
fmt.Println("Vector:", vector)
}
The vector may contain more lanes than the three values provided. The remaining lanes are initialized to zero.
This is useful when an algorithm needs to process the final incomplete group without reading beyond the input slice.
The returned count is important because it tells the application how many actual input elements were loaded.
Developers can also use StorePart when writing only the valid portion of a vector back to a slice.
However, partial operations do not automatically make every algorithm correct. If the operation involves multiplication, accumulation, or comparisons, the application must still ensure that zero-filled lanes do not affect the final result incorrectly.
Using SIMD for Numerical Calculations
SIMD can be especially useful in numerical workloads where the same operation is repeated across large arrays.
Consider calculating the element-wise product of two float32 slices.
//go:build goexperiment.simd
package main
import "simd"
func multiplySlices(
dst, a, b []float32,
) {
if len(dst) != len(a) || len(a) != len(b) {
panic("slice lengths must match")
}
var vector simd.Float32s
width := vector.Len()
i := 0
for ; i+width <= len(a); i += width {
x := simd.LoadFloat32s(a[i : i+width])
y := simd.LoadFloat32s(b[i : i+width])
x.Mul(y).Store(dst[i : i+width])
}
for ; i < len(a); i++ {
dst[i] = a[i] * b[i]
}
}
This implementation uses the Mul method to multiply corresponding vector elements.
The same structure can be applied to several other operations, including addition, subtraction, and supported floating-point calculations.
For applications processing large numerical datasets, vectorization can reduce the number of loop iterations and make better use of available processor instructions.
However, the performance benefit depends on the workload. If the application spends most of its time waiting for memory or reading data from storage, SIMD may not provide a significant improvement.
Understanding the simd/archsimd Package
The portable simd package is useful for algorithms that can be expressed through a common set of vector operations.
Sometimes an application needs an operation that is specific to a particular processor architecture. In such cases, Go provides simd/archsimd.
This package exposes architecture-specific vector types and operations.
For example, it includes types such as:
Float32x4
Int32x8
Uint8x16
Mask32x4
The type names communicate both the element type and the number of elements in the vector.
Unlike the portable package, developers work with explicit vector widths.
Supported Architectures
Go 1.27 expands architecture-specific SIMD support beyond the initial amd64 implementation.
Architecture | SIMD support |
|---|
amd64 | AVX, AVX2, and supported AVX-512 extensions |
arm64 | NEON |
WebAssembly | 128-bit SIMD |
The exact available operations depend on the architecture and processor features.
The amd64 implementation includes several vector widths, while arm64 NEON and WebAssembly use 128-bit vector operations.
The architecture-specific API is not considered stable in Go 1.27. Applications that depend on it should account for potential API changes in later releases.
Loading and Storing Architecture-Specific Vectors
The architecture-specific package provides functions for loading values from slices.
For example, a Float32x4 vector contains four float32 elements.
//go:build goexperiment.simd
package main
import (
"fmt"
"simd/archsimd"
)
func main() {
values := []float32{
10, 20, 30, 40,
}
vector := archsimd.LoadFloat32x4(values)
result := make([]float32, vector.Len())
vector.Store(result)
fmt.Println(result)
}
The LoadFloat32x4 function loads four values into a vector, and Store writes the vector contents into a destination slice.
Architecture-specific vector types make the width explicit. This is useful when an algorithm is designed around a particular processor instruction set.
The package also provides partial loading functions such as LoadFloat32x4Part, which can be used when fewer than four values remain.
Performing Vector Arithmetic With archsimd
The architecture-specific API uses methods such as Add and Mul to perform vector operations.
For example:
//go:build goexperiment.simd
package main
import (
"fmt"
"simd/archsimd"
)
func main() {
a := archsimd.LoadFloat32x4(
[]float32{1, 2, 3, 4},
)
b := archsimd.LoadFloat32x4(
[]float32{10, 20, 30, 40},
)
result := a.Add(b)
output := make([]float32, result.Len())
result.Store(output)
fmt.Println(output)
}
The Add method performs element-wise addition across the four vector elements.
The architecture-specific package also provides operations for multiplication, comparisons, bitwise operations, and other supported instruction patterns.
Developers should check the package documentation before using a particular operation because availability varies by architecture and vector type.
Understanding SIMD Masks and Conditional Operations
Some algorithms need to perform an operation only when a condition is true.
For example, an application may need to replace every negative value with zero.
A normal implementation might look like this:
func clampNegative(values []float32) {
for i := range values {
if values[i] < 0 {
values[i] = 0
}
}
}
SIMD APIs support vector comparisons that produce masks. A mask represents which vector elements satisfy a condition.
The portable package uses mask types associated with the element width. For example, comparisons involving Float32s produce a Mask32s value.
The architecture-specific API provides similar mask operations through architecture-specific mask types.
The archsimd package supports operations such as:
Greater and other comparison methods.
Masked, which zeros elements where the mask is false.
IfElse, which selects values based on a mask.
These operations make it possible to express conditional vector processing without writing an individual branch for every element.
However, the exact operations available in the portable package are more limited than those in archsimd. Check the API documentation before designing an algorithm around a particular mask operation.
When Should You Use simd Instead of archsimd?
Both packages provide vector operations, but they serve different purposes.
Area | simd | simd/archsimd |
|---|
Portability | Designed for portable code | Architecture-specific |
Vector width | Determined by the implementation | Explicit vector types |
API complexity | Smaller common operation set | Larger, architecture-dependent operation set |
Hardware access | Uses supported hardware operations or emulation | Provides access to architecture-specific operations |
Code maintenance | Easier to share across platforms | Requires architecture-aware design |
Best use | General vectorized algorithms | Specialized processor-specific optimization |
For most applications, the portable simd package is the better starting point.
Use archsimd when the algorithm requires an operation that is unavailable in the portable package or when a specific hardware instruction provides a measurable benefit.
Avoid choosing the architecture-specific API simply because it looks closer to processor instructions. The additional control also introduces more responsibility for platform support and fallback behavior.
Benchmarking SIMD Against Regular Go Code
SIMD can improve performance, but developers should never assume that vectorized code is automatically faster.
A simple benchmark can compare the scalar and SIMD implementations.
package main
import "testing"
const size = 1 << 20
var (
inputA = make([]float32, size)
inputB = make([]float32, size)
output = make([]float32, size)
)
func BenchmarkAddScalar(b *testing.B) {
for i := 0; i < b.N; i++ {
addScalar(output, inputA, inputB)
}
}
func BenchmarkAddSIMD(b *testing.B) {
for i := 0; i < b.N; i++ {
addSIMD(output, inputA, inputB)
}
}
Run the benchmarks with:
GOEXPERIMENT=simd go test -bench=. -benchmem
This example assumes both implementations are available in the same benchmark package and that the SIMD build configuration is enabled.
When benchmarking, consider the following:
Use realistic input sizes.
Test small and large slices.
Run multiple benchmark iterations.
Avoid unnecessary memory allocations inside the measured function.
Compare results on the actual deployment architecture.
Check CPU utilization and memory bandwidth.
Verify that both implementations produce identical results.
SIMD may provide a meaningful improvement for large numerical workloads but little benefit for small slices. Loading and storing vectors also introduces work, and some applications are limited by memory bandwidth rather than arithmetic operations.
Floating-point operations deserve additional attention. Different instruction sequences can produce slightly different rounding results, particularly when calculations are reordered or fused. Tests should use an appropriate tolerance when exact floating-point equality is not guaranteed.
Handling Platforms Without Native SIMD Support
One advantage of the portable simd package is that its design does not require every target platform to provide the same SIMD instructions.
The package uses native hardware instructions where supported and provides emulation for platforms that do not have the necessary hardware support.
This means developers can write one implementation without maintaining a separate vector algorithm for every supported architecture.
However, emulation does not provide the same performance benefits as native SIMD instructions.
For applications with strict performance requirements, benchmark the code on every important target platform.
The architecture-specific package has a different approach. It exposes architecture-specific operations, so applications using it need to account for the supported target architecture and processor features.
Common Mistakes When Using Go SIMD APIs
Assuming SIMD Always Makes Code Faster
SIMD is most useful when a workload performs repeated operations over large groups of data.
For small inputs, vector setup and memory access may cost more than the time saved through parallel arithmetic.
Always benchmark against a straightforward scalar implementation.
Forgetting the Remaining Elements
Vector operations process a fixed number of elements at a time within a vector.
If the input length is not a multiple of the vector width, the remaining elements must still be processed.
Use a scalar tail loop or the appropriate partial loading and storing functions.
Hardcoding a Vector Width in Portable Code
The portable simd package intentionally hides the vector width.
Use Len() to determine the number of elements in a vector instead of assuming a particular processor width.
Hardcoding a width defeats one of the main advantages of the portable API.
Ignoring Processor Feature Availability
The architecture-specific API exposes operations that depend on processor capabilities.
Do not assume every amd64 machine supports every AVX or AVX-512 operation.
Use the appropriate feature checks and provide a fallback when the required instruction set is unavailable.
Treating Experimental APIs as Stable
The new SIMD APIs require GOEXPERIMENT=simd in Go 1.27.
Their interfaces are experimental and may change in future releases. Review release notes and test upgrades before depending on these APIs in a production system.
Testing Only One Architecture
An implementation that performs well on an amd64 development machine may behave differently on arm64 or WebAssembly.
Test the application on the architectures that matter to your deployment.
Best Practices for Production Go Applications
Before introducing SIMD into an application, consider these practices:
Profile the application and identify a CPU-intensive operation first.
Keep a simple scalar implementation as a correctness reference and fallback.
Start with the portable simd package.
Use vector Len() rather than assuming a fixed vector width.
Handle incomplete vector groups correctly.
Validate input and output slice lengths.
Benchmark realistic data sizes and workloads.
Test floating-point results with appropriate tolerances.
Use archsimd only when architecture-specific operations are necessary.
Check processor feature availability before using specialized instructions.
Test every supported deployment architecture.
Review experimental API changes when upgrading Go.
SIMD should be introduced as a targeted optimization, not as a replacement for ordinary Go code throughout an application.
Advantages and Limitations of Go SIMD APIs
The new APIs make low-level performance optimization more accessible to Go developers.
The portable package reduces the need to maintain separate vector implementations for different architectures. The architecture-specific package provides access to operations that cannot always be expressed through a portable interface.
Both approaches can help applications use modern processor capabilities without requiring developers to write assembly for every operation.
There are still limitations to consider:
The APIs are experimental in Go 1.27.
The portable package supports a smaller set of operations.
Architecture-specific code requires processor awareness.
Native SIMD performance depends on hardware and compiler support.
Some workloads are limited by memory bandwidth rather than computation.
Vectorized floating-point calculations may produce different rounding results.
The best choice depends on the algorithm, supported hardware, and measured performance requirements.
Summary
Go's SIMD APIs provide a new way to optimize computationally intensive applications without relying entirely on hand-written assembly.
The portable simd package offers vector-size-independent operations that can use hardware instructions where available and emulation where necessary. The simd/archsimd package provides more direct access to architecture-specific vector types and operations.
Go 1.27 expands the experimental SIMD support to amd64, arm64, and WebAssembly while introducing the portable interface.
For most developers, starting with the portable package is the practical approach. It allows algorithms to work across supported platforms without hardcoding vector widths. When specialized instructions are necessary, archsimd provides additional control.
Before using either API in production, benchmark the implementation, test all target architectures, handle incomplete vector groups, and maintain a reliable fallback. SIMD is most valuable when it solves a measured performance problem rather than adding complexity without a clear benefit.
Join the conversation! Your thoughts help the community grow.