• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IgneousRed · Jul 20, 2018 at 02:51 AM · referencevaluemethodefficiencygarbage collection

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;
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgneousRed · Jul 20, 2018 at 04:31 AM 1
Share

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.

avatar image JVene IgneousRed · Jul 20, 2018 at 09:56 PM 1
Share

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.

avatar image IgneousRed JVene · Jul 21, 2018 at 04:33 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Helper properties get reference to player position 2 Answers

Grabbing a value using a string in the reference 1 Answer

Call Functions Across Scripts, Null Object Error 1 Answer

Is this the best way to store my data? It doesn't feel like it. 1 Answer

How to change value of var? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges