Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by mikeschuld · Sep 10, 2013 at 06:14 AM · optimizationarraysmemory management

Ways to minimize Object.stelemref() calls (low level)

I am working on optimizing my A* algorithm in C#. In the background I have written a min priority queue that uses a heap as its backend data structure.

I have gotten the optimization basically as far down as I can get it with algorithmic and refactoring changes, but have now hit a spot where the Unity profiler is telling me the most called method is one I don't have a direct way to optimize.

As an example, when moving a character from the farthest possible corner to another on my map, the profiler is telling me that my FindPath method is taking 83.7% of the CPU time that frame. The two methods within that that use the most time are my Heap.Insert() and Heap.Remove() methods. Inside each of these Insert and Remove calls the profiler shows only one method being called a total of 60000+ times and that is Object.stelemref().

What exactly about my C# is causing this to be called in the background so many times? I am guessing this is an array/memory management issue, but have no idea how to reduce the number of times it is being called. Here is the code from the two methods in question:

(Note: in my sample, heap is an array with 250000 slots)

 public override void Insert(Cell cell) {
     int current = count;
     while (current > 0) {
         int parent = (current - 1) / 2;
 
         // Compare the new item with its current parent
         if (cell.Node.FScore < heap[parent].Node.FScore) {
             // If less "swap" them (swap parent item and current index)
             heap[current] = heap[parent];
             current = parent;
         } else {
             // If not, place in current spot, we are done
             break;
         }
     }
 
     heap[current] = cell;
     count++;
 }
 
 public override Cell Remove() {
     count--;
 
     // Save the top item and decrement the count
     Cell top = heap[0];
     heap[0] = heap[count];
 
     // Perform the heapify
     int current = 0;
     while (current < count) {
         int smallest = current;
         int left = smallest * 2 + 1;
         int right = left + 1;
 
         if (left < count && heap[left].Node.FScore < heap[smallest].Node.FScore) {
             smallest = left;
         }
 
         if (right < count && heap[right].Node.FScore < heap[smallest].Node.FScore) {
             smallest = right;
         }
 
         if (smallest == current) {
             break;
         }
 
         Cell swap = heap[smallest];
         heap[smallest] = heap[current];
         heap[current] = swap;
         current = smallest;
     }
 
     return top;
 }
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
0
Best Answer

Answer by Bunny83 · Sep 10, 2013 at 06:42 AM

Stelem.ref (store ref element) is an opcode, not a function. There's not much you can do to reduce it besides reducing the array-set instructions. Depending on the algorithm and use case that's probably not possible.

Comment
Add comment · 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

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

16 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

Related Questions

Maximum RAM memory exceeded causes app to crash in iOS 1 Answer

Is Destroying GameObjects bad for optimization ? 1 Answer

Optimizing a massive array of GameObjects pls help 1 Answer

Prefab with UI.Text is not unloaded 1 Answer

how are arrays stored in memory in the iOs Player 1 Answer


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