- Home /
Is reffrencing a huge class better than reffrencing it's elements?
I do know that value types are easier on the GC but reference types don't are not deppendent on the size of a value they represent. So, is the first example more effective?
public static void UpdateChunk(ref Chunk_Data chunk_Data)
{
chunk_Data.meshFilter.mesh = MergeDiffrentMaterials(chunk_Data.combineInstances);
chunk_Data.meshRenderer.materials = chunk_Data.materials;
}
public static void UpdateChunk(ref MeshFilter meshFilter, ref MeshRenderer meshRenderer, CombineInstance[] combineInstances, Material[] materials)
{
meshFilter.mesh = MergeDiffrentMaterials(combineInstances);
meshRenderer.materials = materials;
}
Answer by JVene · Jul 20, 2018 at 03:06 AM
Of these two, the first is a little faster and more efficient.
This may be a little more obvious on a language like C++ than it is in C#.
In the first call, a reference is used, so the object isn't copied. The size of the class has no impact on what is happening at the machine level, only a location in memory (a pointer to C++ programmers) is required, though it is called a reference and the technical meaning is unique to C# (it is similar in Java), the means by which this is managed is just a numeric value indicating a location in memory, which is a fixed size and therefore performs the same work no matter what the size of the class.
The second version passes more references, which takes more space and time to complete the call to the function. If these parameters are members of the same class, the first call is faster, but...
The action being performed is on the class itself. I see no reason to make it a static function. This could be a public member function that takes no parameters at all and do the same thing. Everything that is happening involves the Chunk_Data passed by reference, so what is the reason it is made static?
Also, is MergeDifferentMaterials a static function? It may well be best as a public (possibly private) member function for similar reasons.
Thanks for the reply, just wanted to make sure. I usualy devide scripts into _Config, _Properties, _Data and _Logic. _Config runs at the start and fills _Properties. _Properties is static and filled with readonly values that don't change during the game. _Data contains values that change during the game. _Logic is static and has all functions related to _Data. After making few games I found this way of separating to be favorable in my opinion, as it makes it easy to implement multiplayer, ECS and just feels organized.
While I understand the division and organization you're using, that still doesn't lead me to understand why this:
public static void UpdateChunk(ref Chunk_Data chunk_Data)
{
chunk_Data.meshFilter.mesh = $$anonymous$$ergeDiffrent$$anonymous$$aterials(chunk_Data.combineInstances);
chunk_Data.meshRenderer.materials = chunk_Data.materials;
}
Which must be called with:
Chunk_Class_name.UpdateChunk( ref a_chunk );
Isn't better written as
public void UpdateChunk()
{
meshFilter.mesh = $$anonymous$$ergeDiffrent$$anonymous$$aterials( combineInstances );
meshRenderer.materials = materials;
}
Which is called with
a_chunk.UpdateChunk();
While doing exactly the same thing without passing parameters to a function other than "this" on a member function call.
Not really... It is called with Chunk_Logic.Update(ref Chunk_Data), as I stated _Logic is static class and _Data is totally separate so this is the only way.