Course contentsShow
← Unreal Engine Game Development
Lesson 211 of 8803. C++ and UObject ArchitectureFree lesson

Reducing Heap Allocation with TInlineAllocator

Utilize inline allocators to store small collections directly within the container object, significantly reducing heap pressure and improving cache locality.

In this lesson, you will learn how to boost performance by using TInlineAllocator to keep small, frequently used data collections inside your objects instead of scattered in the heap.

The Problem with Heap Allocation

When you use a standard TArray, it defaults to allocating memory on the heap. While this is necessary for large or dynamic collections, it becomes inefficient for very small sets of data (like storing 2 or 3 items). Every time you allocate on the heap, the engine must find a free block of memory, which is relatively slow. Furthermore, if that data is far away from your object in memory, the CPU suffers a "cache miss," meaning it has to wait longer to retrieve the information.

Leveraging TInlineAllocator

TInlineAllocator allows you to define a specific amount of space within the container itself to hold elements. If your collection stays under a certain size, the data is stored "inline"—right alongside the rest of your object's variables. This eliminates the need for a separate heap allocation entirely.

Here is how you define a TArray that holds up to 4 integers inline:

// This array stores the first 4 integers inside the memory footprint of the class itself.
// If you add a 5th integer, it will then spill over to the heap.
TArray<int32, TInlineAllocator<4>> SmallInventoryIndices;

Why This Matters

By keeping these small arrays "inline," you achieve two major wins:

  1. Reduced Memory Pressure: You stop asking the heap allocator for tiny chunks of memory, which reduces fragmentation.
  2. Improved Cache Locality: Because the data is physically sitting inside your object, the CPU can grab the object and its collection in one go, resulting in much faster access times.

This technique is a staple for performance-critical systems like inventory slots, ability cooldowns, or small logic lists where the number of elements is usually small and predictable. By choosing an appropriate inline size, you optimize for the "common case" while still maintaining the safety of a dynamic array should the list grow larger.

Key Takeaway: Use TInlineAllocator with TArray to store small collections directly inside your objects, drastically reducing heap fragmentation and improving cache efficiency.