In this video, we will clarify an important performance bottleneck in your Go loops. If you're interested in finding out why 0 bytes matter in Go, check out this video: https://youtu.be/89guEpwuL4U
📝 Description Optimize your Go applications by uncovering a hidden performance trap inside the common `range` loop. In this performance-focused tutorial, we dive deep into Go's memory model to demonstrate how iterating over large structs can silently kill your application's speed. We will prove — using memory address analysis — that Go creates implicit copies during iteration, and then we will quantify the impact using Go's testing package. By the end, you will know exactly how to refactor your hot paths for maximum efficiency. Here’s what you will learn: • Visualize Go's value semantics in action by inspecting memory addresses, proving definitively that `range` loops create independent copies of your data rather than referencing the original slice elements • Measure the real-world cost of value copying by writing robust benchmarks (utilizing Go 1.24's new b.Loop() syntax) that reveal the drastic CPU and memory bandwidth overhead when processing large data structures • Implement zero-allocation iteration patterns by switching to index-based access, and master the decision framework for choosing between readability and raw performance based on struct size and mutability requirements
⏳ Timestamps: 00:00 - Introduction 00:30 - The Problem 03:05 - The Solution 06:00 - The Benchmark 10:49 - Outro
👋 Hey there! If you are new to this channel: Hey 👋 my name is Flo and I am a professional software engineer with a passion for coding in Golang, TypeScript, JavaScript, and Rust. This channel is where I share that passion and dive into the exciting world of software engineering.
Using a range loop over an area of structs can be quite inefficient,
particularly if the struct is large and is not really accessed via an index.
Therefore, in this video we will clarify why this is the case
and how you can actually resolve this performance bottleneck.
Alright, so we are in our normal project right here
and I do have a main.go file, but also a main underscore test.go file.
Remember, this is just for testing purposes
So let's just get right into the issue.
So let's just first declare a user struct right here.
So we say type user struct.
And this user struct just defines the data structure for our user.
So we can define any kind of struct field that belongs to the user in this struct.
Like a name for instance, maybe an email as well.
Now that we have declared the user struct right here with the name and an email as a struct field,
we can now declare the user slice.
So let's just say user is right here and then we just declare the user slice right here.
And in this slice we are just going to add two entries, two initial entries.
And we have the name right here and then maybe also the email,
which will be test at test.com.
And then we will maybe also have Bob and the email for Bob will be bob at test.com.
Okay, now we've created this slice or more like a dynamic array of user structs.
So what we can do is just a normal thing, maybe loop over it, right?
So loop over the users and maybe send a request, send an email to all the users, something like this.
So we can loop through each user in the user slice.
And right here this is actually the performance bottleneck.
Because right here I am going to demonstrate or I'm going to show you
that actually Go creates a copy of each element, so of each user in this case.
Let's just print f, in this case the user itself or more like the memory address of the user.
And this really shows where the individual user we are looping over is actually stored in memory.
So we can just say %p right here and then backslash n.
And then we can just use the user element right here.
And then to demonstrate the effect and the performance bottleneck,
we are also going to just print f the user in the original user slice.
So we are just going to say users and then i.
Obviously we do need the memory address right here, don't forget the n%.
And let's just see what is going on if we run this project.
So let's just say go run main.go.
And what we will actually see are four completely different memory addresses.
So for this particular use case, whenever we loop over the users itself,
every single time we have like a new user in the loop,
it is going to copy this kind of element and then we are using this copy in the loop.
And not really using the original user in the user slice.
And we can actually see this because the first memory address in this case for myself,
so for Flo, is actually different than the second printed address.
But in reality they kind of should be the same, right?
And you might even ask yourself how can we now kind of fix this.
And obviously because we are now copying the value right here,
so in this user variable every single iteration the user is kind of copied.
Modifying the user itself, so if we like say user.name and then modify it like this,
doesn't really affect the original element in the slice.
I think that's quite clear.
Now for these two users and a lot of other users potentially in the slice,
this would not create a huge bottleneck.
But let's just imagine that the user itself has metadata.
And this metadata can be quite big.
So let's just declare a new struct field up here and this will be of type byte array.
And the size of this array will be 8192.
So kind of 8 kilobytes of metadata.
And now this will create a huge performance bottleneck
because every single time we iterate over the user slice
and we kind of want to access or use this user right here with this syntax,
it is going to copy the whole metadata array
and all the other struct fields of this particular user, right?
Which can be quite inefficient.
And even if we declare a simple function right here called sendEmail
and we just want the pointer to the user data basically
and then maybe call it right here.
So now even if we pass in this memory address or the pointer to the value right here,
it just still copies the element from the slice due to the range loop.
And this is the potential performance bottleneck.
So the pointer actually points to the copy and not the original element.
And the performance problem is, like I said before,
for each loop iteration, Go just copies the entire user struct
including the 8 kilobytes of metadata.
And like with 10,000 users, that's maybe 80 megabytes of copying.
So this just wastes CPU time and memory bandwidth
and for hot paths, this would actually matter a lot.
Now there's also a different way of doing this or getting rid of this bottleneck
because we could also just use a slice of user pointers.
Now this would mean we would not copy large structs
because now each iteration only copies a pointer instead of the entire struct.
But this code then might be harder to reason about
because we could mutate the struct in the loop itself.
But we are going to leave things here as they are.
Now the fix for this is quite easy.
Just get rid of this user right here
and then the performance bottleneck is resolved, right?
Just use the index kind of way of accessing the user in a slice.
In this particular case, we are just accessing a specific user
in the original user slice.
And we are not copying anything.
We are just accessing the memory address of the user in the slice.
Now to prove it to you that I don't talk bullshit or something like this,
let's just go to the main test.go file
and let's just benchmark these two operations.
Like in the user struct, let's just create a data struct right here
and then we have data and then let's just use a really huge array size.
So we could, for instance, use this size right here.
So here we are choosing a really large array size intentionally
because obviously we want to demonstrate the performance difference
between copying and direct access.
Now what we are going to do is we are going to declare a new benchmark function.
So we are going to say benchmark range with copy.
And then we are going to use b and then pointer testing.b.
And here we are going to just demonstrate the inefficient way to iterate over a slice.
So let's just create quickly a item slice right here through the make function.
The type of this slice is going to be data obviously and the size will be 1000.
So here we are just creating a slice with 1000 data elements.
Then after that we are going to quickly reset the timer right here
to really exclude setup time from the benchmark.
So this just runs the benchmark loop and the b.loop functionality or method
is just only available in Go 1.24.
But if you are below that, we can just say, for instance, this right here.
So this would be a perfect alternative.
i is less than b.n and then i++
or we could even say for and then range b.n.
These three things are more or less kind of the same.
But obviously we are going to use the modern version right here.
And then what we can do is just use v and then range items right here.
So we are looping or ranging over the items.
And then we just do nothing with the value.
Now again, this loop just copies each data element into v for each iteration.
And the underscore in this case just means we are ignoring the index.
v just receives a copy here of each element,
which is really expensive because the data is quite large,
which just means that we will copy a lot of information and data for each iteration.
Now, this underscore is equal to v pretty much just means that we do nothing with v
just to show the copying cost.
And here we are kind of saying to the compiler that we know that we've declared this variable,
but we are intentionally not using it.
All right, quite cool. Let's just do the same thing with the direct access.
So we just say benchmark direct access.
And here we are going to demonstrate the efficient way to iterate over a kind of cost performance slice.
And instead of copying elements, we just use the index to access these elements directly.
And this just avoids the expensive copy operation in general and is pretty much just faster.
So let's just copy this logic right here.
Same thing, we are just going to create a new slice with 1000 data elements.
We are going to reset the timer right here to just exclude the setup time.
Then we run the benchmark loop.
And instead of using v, we are going to remove this and say i.
And then we say underscore items at i.
Right here, this loop just uses the index i to access the elements directly.
And this is much more efficient for large structs.
And again, this underscore equals to items at i.
Here we read the access elements directly without copying, maintaining better memory, locality, and cache efficiency.
And if we are now going to run this benchmark through gotest and then dashbench and benchmark.
What we will actually see is that the benchmark direct access function is a lot faster than range with copy.
And I think this kind of proves that you should basically use the direct access functionality or kind of way to access an element in a slice when you really have a huge struct and a lot of data to process.
Now what can we learn from that? I think it's quite simple.
Whenever we use this kind of way to range over a slice or array, it's best to always have in mind that we are copying the values right here.
And you should use this way whenever you have small values like ints, bools, or small structs like 100 bytes or something.
But this optimization threshold obviously depends and varies based on the use case.
You could also use this way when you have read-only operations or readability really matters and performance is not critical.
On the other hand, you should use this way right here, so the direct access way.
Whenever you have large structs to avoid unnecessary copies and you maybe want to modify the elements, you could also modify the elements quite easily by just directly accessing the element in the slice.
So to conclude, really the usage depends on the size of elements and when you need to modify things and obviously the performance requirements.
Now I hope that you've learned something today.
If you also want to find out what the purpose of an empty struct is in Go, feel free to watch this video here.
Anyway, thank you so much for watching, have a lovely day, and bye bye.