Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
2
Question by Strange Quirk · Jun 24, 2011 at 07:15 PM · arraynewavoid

Using arrays of vector3s to avoid new

I've seen a script that requires creating lots of temporary vectors each frame, and the author, to avoid using "new" all the time, instead had a large array of Vector3s and Vector2s. Every frame, he reset the pointer to the array back to 0 and incremented it each time he needed a new vector, before changing the vector's components.

My question: does this really provide any significant benefit?

Comment
Add comment · Show 8
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 Jessy · Jun 24, 2011 at 07:24 PM 2
Share

It's a struct, so I can't imagine it's too helpful. Did the author of the code say otherwise?

avatar image flaviusxvii · Jun 24, 2011 at 07:28 PM 0
Share

I'm not sure I follow the reasoning behind "It's a struct...". Certainly reducing the amount of memory being allocated/deallocated each frame can lead to a performance benefit if those allocations are large enough. Using a pool of re-useable items can help, or using static variables in some instances is the way to go (not thread safe).

avatar image Mischa · Jun 24, 2011 at 07:38 PM 0
Share

there's a very good unite keynote about performance optimation in the unity ressources: http://unity3d.com/support/resources/unite-presentations/performance-optimization

avatar image Eric5h5 · Jun 24, 2011 at 07:45 PM 0
Share

@flaviusxvii: structs like Vector3 are allocated on the stack, rather than the heap like classes. I'd guess that using a convoluted "array of Vector3s" scheme might actually result in worse performance. At least, it would be much more annoying code for no gain that I'm aware of.

avatar image aldonaletto · Jun 24, 2011 at 08:49 PM 1
Share

At least in the old times (C, Pascal etc.), only temporary variables were allocated in the stack, because the stack frame was released on return, and everything in it was lost. Every non-temporary variable should be allocated in the heap or in the global data segment. Ok, there are a lot of new things in C# and other modern languages, but as far as I know the calling proccess is the same - a stack frame is allocated, the function is executed, and upon return the stack frame is released.
NOTE: Forgive me, @Eric5h5 - due to some inexplicable and stupid reason I was thinking about non-temporary arrays. You're right: simple temporary variables like Vector2 or Vector3 don't require memory allocation for their components. A more complex class could require memory allocation, thus consu$$anonymous$$g heap resoureces even if it was a temporary variable. And stack allocation is cheap (or free, to be more precise) - since there's no garbage collection (blocks are allocated and deallocated sequentially).

Show more comments

1 Reply

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

Answer by Eric5h5 · Jun 24, 2011 at 08:35 PM

It's not a good idea to do that, at least when it comes to structs like Vector3. Here's the result of a test involving 1000 instantiated objects. It uses a class that's essentially a Vector3, except it's a class and not a struct:

 class Test {
     var x : float;
     var y : float;
     var z : float;
     function Test (x : float, y : float, z : float) {
         this.x = x;
         this.y = y;
         this.z = z;
     }
 }

The script on each of the 1000 objects allocates a new Test class every frame in Update:

 function Update () {
     var foo = new Test(0.0, 0.0, 0.0);
 }

gc test 1

As you can see, there are GC spikes, and it allocates 19.5 KB per frame. So, here's the result of a test that allocates a new Vector3 in Update:

 function Update () {
     var foo = new Vector3(0.0, 0.0, 0.0);
 }

gc test 2

As you can see, there are no GC collection spikes, and no bytes being allocated per frame. It's perfectly OK to allocate new structs like Vector3 in Update. It is, in fact, preferable to the alternative, namely convoluted messy code that doesn't actually accomplish anything. Simply keep variables local as usual, and don't try any fancy schemes involving re-using array entries (unless you're dealing with classes and not structs, I guess).

Comment
Add comment · Show 6 · 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 Joshua · Jun 24, 2011 at 09:22 PM 1
Share

Very interesting answer, good to know! For everyone who would like to read up on what a struct exactly is and how it differs from a class, this is a pretty good explanation http://msdn.microsoft.com/en-us/library/aa664471(v=vs.71).aspx

avatar image flaviusxvii · Jun 24, 2011 at 10:13 PM 0
Share

@Joshua interesting link. Although it doesn't support the assertions that classes and structs are allocated in different places in memory based on their type. But.. I'm being ignored, so no bother.

avatar image Eric5h5 · Jun 24, 2011 at 10:19 PM 1
Share

That's weird, the 1st screenshot isn't showing up anymore. In case this is happening for others, here are the screenshot links:

http://www.starscenesoftware.com/stuff/pics/gctest1.png

http://www.starscenesoftware.com/stuff/pics/gctest2.png

avatar image flaviusxvii · Jun 25, 2011 at 03:17 AM 0
Share

I'm still not convinced this test is even testing the right thing. I thought the question was about making an object pool vs. allocating on-the-fly. How is this even remotely addressing that?

avatar image Jessy · Jun 25, 2011 at 03:43 AM 0
Share

@flaviusxvii You can't make an object pool if you aren't using objects.

Show more comments

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. 0 Answers

Help with arrays of arrays please 2 Answers

Very slow performance when variable has serialize tag 3 Answers

Different Score for Active OnMouseDown objects 0 Answers

How to call all instances of a script in a list 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges