# C++ TArray Reference performance slow in 4.8 + 4.9

**URL:** <https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907>\
**Category:** Editor Scripting\
**Tags:** reference, tarray, UE4-9, unreal-engine, Performance, CPP, question\
**Created:** [September 3, 2015, 1:21pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907 "2015-09-03T13:21:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![DekanTrue](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DekanTrue](https://forums.unrealengine.com/u/DekanTrue)\
**Post date:** [September 3, 2015, 1:21pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/1 "2015-09-03T13:21:55Z")

</div>

I am having a performance issue with a TArray of structs, after upgrading from 4.7.5 to 4.8.3.

I’m passing a the reference to the array to a function in a function library, which was working and performing fine in 4.7, but in 4.8 and 4.9, there is a significant hit to performance.

I am a bit new to c++, so if anyone can see anything obvious, please tell me.

The Struct is declared in the same file, while the array is sent from blueprint.

```
FST_Hex UGameUtilFunctionLibrary::CubeToHex(FVector CubeCoord, const TArray<FST_Hex>& Hexes, int32 GridXCount)
{
	FST_Hex Hex;

	bool Found = false;
	FVector2D OffsetCoords = ConvertCubeToOffset(CubeCoord);
	int32 Index = GetHexIndex(OffsetCoords, GridXCount);
	if (HexIndexValid(Hexes, Index))
	{
		GetHex(Hexes, Index, Found, Hex);
	}

	return Hex;
}
```

---

<div class="post-metadata">

**Author:** ![plangton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@plangton](https://forums.unrealengine.com/u/plangton)\
**Post date:** [September 3, 2015, 2:35pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/2 "2015-09-03T14:35:41Z")

</div>

it is good to send the code of the following functions  
GetHexIndex and GetHex

---

<div class="post-metadata">

**Author:** ![DekanTrue](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DekanTrue](https://forums.unrealengine.com/u/DekanTrue)\
**Post date:** [September 3, 2015, 2:49pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/3 "2015-09-03T14:49:22Z")

</div>

int32 UGameUtilFunctionLibrary::GetHexIndex(FVector2D OffsetCoord, int32 GridXCount)  
{  
int32 GridWidth = (GridXCount - (GridXCount % 2)) / 2 - 1;  
int32 Index = GetGridIndex(GridWidth, FMath::FloorToInt(OffsetCoord.X), FMath::FloorToInt(OffsetCoord.Y), true);  
Index = Index - (FMath::FloorToInt(OffsetCoord.Y) / 2 \* ((GridXCount + 1) % 2));  
return Index;  
}

```
void UGameUtilFunctionLibrary::GetHex(const TArray<FST_Hex>& Hexes, const int32 Index, bool &Found, FST_Hex &Hex)
{
	Found = HexIndexValid(Hexes, Index);
	if (Found) {
		Hex = Hexes[Index];
	}
}
```

---

<div class="post-metadata">

**Author:** ![DekanTrue](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DekanTrue](https://forums.unrealengine.com/u/DekanTrue)\
**Post date:** [September 3, 2015, 2:55pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/4 "2015-09-03T14:55:57Z")

</div>

bool UGameUtilFunctionLibrary::HexIndexValid(const TArray\<FST\_Hex\>& Hexes, int32 Index)  
{  
bool valid = (Index \< Hexes.Num()) && (Index \>= 0);  
return valid;  
}

---

<div class="post-metadata">

**Author:** ![plangton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@plangton](https://forums.unrealengine.com/u/plangton)\
**Post date:** [September 3, 2015, 3:11pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/5 "2015-09-03T15:11:02Z")

</div>

I see no issues with this code. but it might be that filling and reading through your array is causing this in other parts of your code.

However I suggests you the following modification. you are using too much functions for a validity check. TArray already has a simple function for it name isValidIndex : [TArray | Unreal Engine Documentation](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Containers/TArray/index.html)

```
FST_Hex UGameUtilFunctionLibrary::CubeToHex(FVector CubeCoord, const TArray<FST_Hex>& Hexes, int32 GridXCount)
 {
     FVector2D OffsetCoords = ConvertCubeToOffset(CubeCoord);
     int32 Index = GetHexIndex(OffsetCoords, GridXCount);
     
     if ( Hexes.IsValidIndex(Index) )
     {
         return Hexes[Index];
     }
 
     return NULL; // it is safer that Hex as an object
 }

```

and you can remove two of your functions

---

<div class="post-metadata">

**Author:** ![DekanTrue](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DekanTrue](https://forums.unrealengine.com/u/DekanTrue)\
**Post date:** [September 3, 2015, 3:17pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/6 "2015-09-03T15:17:29Z")

</div>

I didn’t know that the TArray had that function. Thank you.

I made an isolated test, and problem definitely occurs with the passing of the array. I just don’t understand why this has such a large performance hit in 4.8 and up. I tried just returning the Index insted, and then do the rest in blueprint, which caused no performance hit.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [September 3, 2015, 3:26pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/7 "2015-09-03T15:26:15Z")

</div>

I don’t see anything intensive in this code, you not even interacting with TArray really, atleast not in a way that it would effect performence. How do you know that performance dropped?

You can try to pin point what is taking most time using profiler, which you can find in Window → Session Frontend. In order plug your code in to it first in ProjectName.h (projectname is name of your project) decler stat group

```
DECLARE_STATS_GROUP(TEXT("Project Name Stats"), STATGROUP_ProjectName, STATCAT_ProjectName)

```

Now in code you want to messure time of execution you place:

```
DECLARE_SCOPE_CYCLE_COUNTER(TEXT("Some name of stat that will be displayed"), STAT_ProjectName_NameOFStat, STATGROUP_ProjectName);

```

You place in specific scope you want to messure, scope is code field that you mark with bracets { } if you place decleration in specific { } it will messure execution time only for code that is in { }. C++ let you create dummy scope (without “if” or “for” etc.) and stat system will acknowledge that, so for example:

```
void UGameUtilFunctionLibrary::SomeFunction()
 {
    FunctionA();

    {
       DECLARE_SCOPE_CYCLE_COUNTER(TEXT("Some name of stat that will be displayed"), STAT_ProjectName_NameOFStat, STATGROUP_ProjectName);

       FunctionB();
       FunctionC();

    }
 }

```

Profiler will only messure FunctionB and FunctionC execution time

Also here you got profiler docs:

> **[Unreal Insights](https://docs.unrealengine.com/5.1/en-US/unreal-insights-in-unreal-engine/)**
>
> Profile your project with Unreal Insights

---

<div class="post-metadata">

**Author:** ![plangton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@plangton](https://forums.unrealengine.com/u/plangton)\
**Post date:** [September 3, 2015, 3:30pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/8 "2015-09-03T15:30:09Z")

</div>

I suggest you once clean the project and rebuild it

---

<div class="post-metadata">

**Author:** ![DekanTrue](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DekanTrue](https://forums.unrealengine.com/u/DekanTrue)\
**Post date:** [September 3, 2015, 3:47pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/9 "2015-09-03T15:47:52Z")

</div>

I haven’t made a session profile yet. I will do that now. I have only been using the stat unitgraph for monitoring the framerate. If I ran the function on tick, in 4.7.5 the game frame time was 5ms. In 4.8.3 its 15ms where the is a noticeable spike.

---

<div class="post-metadata">

**Author:** ![plangton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@plangton](https://forums.unrealengine.com/u/plangton)\
**Post date:** [September 3, 2015, 3:56pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/10 "2015-09-03T15:56:34Z")

</div>

And when you deactivate this function the frame rate is again 5ms?

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [September 3, 2015, 3:58pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/11 "2015-09-03T15:58:40Z")

</div>

Hmm i don’t know, you code looks ok, you not looping, you only accessing array which is same as reading varable, so study you code with profiler.

The most silent preference killer is memory allocation (when you spawn or create objects) you should avoid that if doing so in Tick

If you adding items array sometimes TArray need to reallocate the array in memory if array does not fit in specific segment of memory anymore and as i said above memory allocation takes preformence. To prevent that you can reserve memory before hand, best you be maximum value of items that you expect array to have, you can do that with this function:

> **[TArray::Reserve](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Core/Containers/TArray/Reserve/)**
>
> Reserves memory such that the array can contain at least Number elements.

Thats hints i can give ya ;]

---

<div class="post-metadata">

**Author:** ![DekanTrue](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DekanTrue](https://forums.unrealengine.com/u/DekanTrue)\
**Post date:** [September 3, 2015, 4:08pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/12 "2015-09-03T16:08:44Z")

</div>

Yes. I even made a side by side camparion with a 4.7.5, 4.8.3 and 4.9.0 builds with the exact same code.  
I also tried recreating the function in blueprint, and using the same functions. Somewhat simplified though.

 ![57013-untitled-1.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/3/8/638c8eb5e44f7ce0a8ee738949825364d272d501.jpeg)

---

<div class="post-metadata">

**Author:** ![DekanTrue](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DekanTrue](https://forums.unrealengine.com/u/DekanTrue)\
**Post date:** [September 3, 2015, 4:25pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/13 "2015-09-03T16:25:03Z")

</div>

![57017-untitled-2.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/2/1/3218b9cf9685a2770bcca6cf48fc3e923d85b2c1.jpeg)

I made a few screenshot for comparison the blueprint setup above. I made the function run on tick (Just for testing), and switched it on/off every 2 seconds. The top one is the “cube to hex c++ function”.  
The bottom is the more version where no array reference is passed to c++.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [September 3, 2015, 4:28pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/14 "2015-09-03T16:28:58Z")

</div>

So CubeToHex tames more time or blueprint? keep in mind that blueprint slower then native code

---

<div class="post-metadata">

**Author:** ![DekanTrue](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DekanTrue](https://forums.unrealengine.com/u/DekanTrue)\
**Post date:** [September 3, 2015, 4:35pm UTC](https://forums.unrealengine.com/t/c-tarray-reference-performance-slow-in-4-8-4-9/330907/15 "2015-09-03T16:35:58Z")

</div>

The “blueprint” version is just calling the same c++ functions as the cubeToHex function would. Yes my early test showed me blueprint to be 5-10 times slower. But keep in mind that the one without the spikes, are the “blueprint version”.
