The Go Wiki includes a slice trick on filtering slices without allocating.
filtered := items[:0]
for _, x := range items {
if condition(x) {
filtered = append(filtered, x)
}
}
This post will attempt to explain how that works.
1. How slices work
Go slices are triples containing the following properties.
- Data
-
Pointer to an array that holds the slice elements.
- Length
-
Number of elements added to the slice.
- Capacity
-
Number of elements that the underlying array can hold.
The following shows the internal representation of slices.
|
|
|
Visualized in a more approachable format:
|
|
|
Two slices can have the same backing array.
|
If one slice changes the contents of the array, it affects all slices backed by that array.
|
Sharing the underlying array and modifying it in-place is the key to filtering slices without allocating.
2. Filtering without allocating
Let’s go back to the original code sample.
filtered := items[:0]
for _, x := range items {
if condition(x) {
filtered = append(filtered, x)
}
}
Consider,
|
Where e2
and e3
match condition(e)
.
condition(e1) // false condition(e2) // true condition(e3) // true
We create a new empty slice filtered
backed by the same array.
|
As we iterate through items
, we change the array by appending into
filtered
.
|
For the first case, nothing happens because condition(e1)
is false. We
move on to the second element.
|
In this case, because the condition was true, we append to filtered
.
This modifies the underlying array but it doesn’t affect the for
iteration because we’re already past that element.
|
This repeats with the last element and we end up with,
The new slice contains the filtered elements and the original slice is now unusable.
3. Conclusion
This post should have clarified how zero-allocation slice filtering works.
The trick itself is useful but use it only if you own the slice you are filtering, and you are certain that it’s no longer needed.
4. Further reading
The Go Slices: usage and internals blog post explains slice internals in more detail.