When we use Allocator.Temp
with a collection like NativeArray
, how long does the allocation last? We’ve seen that Temp
allocations are automatically disposed without the need to explicitly call Dispose
, but when does the automatic dispose happen? Today we’ll test to find out!
Archive for category Unity
IDisposable
is becoming more and more prevalent in Unity. Previously, it was typically only used for I/O types like FileStream
. Now it’s used for in-memory types like NativeArray<T>
to avoid the garbage collector. Needing to call Dispose
manually means we’re explicitly managing memory, just like we’d do in lower-level languages like C++. That comes with some challenges, especially with shared ownership, which we’ll deal with today.
Fixed-point types save memory compared to floating-point types, but can they also improve performance? Today’s article finds out!
Today we continue to explore how we can store values in less than a byte. We’ll expand the BitStream
struct with the capability to write values in addition to just reading them. Read on to see how to implement this functionality and for the full source code which you can use in your projects.
The smallest a C# type can be is one byte. The byte
type and and an empty struct are examples of this. But what if we want to store data in less than a byte to improve performance such as load times and CPU cache utilization? Today’s article does just this by packing at the bit level!
Floating-point math is fast these days, but fixed-point still has a purpose: we can use it to store real numbers in less than 32 bits. Saving a measly 16 or 24 bits off a float
might not sound appealing, but cutting the data size in half or quarter often does when multiplied across large amounts of real numbers. We can shrink downloads, improve load times, save memory, and fit more into the CPU’s data caches. So today we’ll look at storing numbers in fixed-point formats and see how easy it can be to shrink our data!
Unity provides exactly one collection: NativeArray<T>
. Compared to managed arrays in C#, these must be one-dimensional. So today we’re building a two-dimensional version of it: NativeArray<T>
. We’ll add this to the NativeCollections GitHub repository for easy inclusion into any project. Read on to learn more about the collection!
Unity provides different types of native memory allocators via the Allocator
enum: Persistent
, TempJob
, and Temp
. These names indicate the lifetime of the memory allocation: forever, a few frames, and one frame. They also indicate an underlying algorithm to perform the allocation and deallocation. These algorithms have varied results on our code that uses them, and that’s what we’ll explore in today’s article.
Job structs can’t contain managed types like string
, class
instances, or delegates. This is currently a pain as a lot of the Unity API relies on these and so we’re forced to deal with them. Today we’ll talk about how we can use managed types in our jobs to bridge the gap.
Bit fields give us the ability to use less than one byte to store an integer. This can be really useful to fit more data into a CPU cache or to reduce memory usage in general. Unfortunately, bit fields aren’t built into C#. Today we’ll work around that by creating our own!