Author Topic: Shrugger! Unity!  (Read 151303 times)

Offline Shrugging Khan

  • Hero Member Mark III
  • *****
  • Posts: 1,217
  • Neinzul Y PzKpfw Tiger!
Re: Shrugger! Unity!
« Reply #300 on: July 28, 2015, 01:15:39 pm »
Well, I only have to get a small of number of them per frame, so if they aren't ordered and I have to iterate over all of them to find out the ones with the correct dates, that might be a whole lot of iterating.

But yes, I'm optimising again...gah!  :'(
The beatings shall continue
until morale improves!

Offline keith.lamothe

  • Arcen Games Staff
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 19,505
Re: Shrugger! Unity!
« Reply #301 on: July 28, 2015, 02:57:11 pm »
Do all these objects have a spatial location? Does that location ever change between time-of-scheduling and time-they-should-be-processed?
Have ideas or bug reports for one of our games? Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline Shrugging Khan

  • Hero Member Mark III
  • *****
  • Posts: 1,217
  • Neinzul Y PzKpfw Tiger!
Re: Shrugger! Unity!
« Reply #302 on: July 28, 2015, 03:03:07 pm »
Yes they do and...well, it changes on being processed. What are you thinking of? Are we going to weird places now?

I also need a separate system that will function similarly; to be used for the AI. There'll be no spatial locations to use there; How would that change things?
The beatings shall continue
until morale improves!

Offline Draco18s

  • Resident Velociraptor
  • Core Member Mark V
  • *****
  • Posts: 4,251
Re: Shrugger! Unity!
« Reply #303 on: July 28, 2015, 04:23:43 pm »
Binary tree.  Done.

Insertion is very fast, and unity has a special IndexOf(...) operator that returns a value indicating where in a sorted array an element would be, if it was already in the array (said function requires the array be sorted and takes advantage of binary-tree search properties).

So as long as the array starts sorted (this is easy, an empty array is sorted) you can rapidly insert new items at the point at which they would normally be.  Then in order to do your scheduling, look at the first element (this one will have the smallest time) and say "is this greater than the current time?" and if so, you're done.

You'll need a wrapper object that holds the [thing your interested in] and the [time to check].  Override Equals() and GetHashCode() and return the values of checkTime.Equals() and checkTime.GetHashCode() for each.

Offline Shrugging Khan

  • Hero Member Mark III
  • *****
  • Posts: 1,217
  • Neinzul Y PzKpfw Tiger!
Re: Shrugger! Unity!
« Reply #304 on: July 28, 2015, 04:41:48 pm »
You do make it sound easy  :D

It's a mite late here by now, so I'll be trying it tomorrow. Already have that wrapper from my previous attempts, so expect horrifying results in about 10 hours. Thanks for the tip!

Wait, what exactly are you referring to here?
Quote from: Draco
checkTime.Equals() and checkTime.GetHashCode()
« Last Edit: July 28, 2015, 04:45:41 pm by Shrugging Khan »
The beatings shall continue
until morale improves!


Offline Shrugging Khan

  • Hero Member Mark III
  • *****
  • Posts: 1,217
  • Neinzul Y PzKpfw Tiger!
Re: Shrugger! Unity!
« Reply #306 on: July 29, 2015, 03:14:15 am »
Nonono, I meant *which* .Equals and .GetHashCode. Those of the wrapper?
The beatings shall continue
until morale improves!

Offline Draco18s

  • Resident Velociraptor
  • Core Member Mark V
  • *****
  • Posts: 4,251
Re: Shrugger! Unity!
« Reply #307 on: July 29, 2015, 11:28:47 am »
Nonono, I meant *which* .Equals and .GetHashCode. Those of the wrapper?

Oh, oh yes.  The wrapper should override Equals and GetHashCode.  You will need to implement the IComparable interface as well (although Lists can be sorted without it, it makes it much easier).  Those will make it so your wrapper can be the Key in a HashTable and such, which is important for storing and retrieving stuff from collections. ;)

You could probably pass on Equals and GetHashCode and just implement IComparable for the suggested a binary tree; it was late last night when I wrote that post, so I wasn't thinking straight.

Offline Shrugging Khan

  • Hero Member Mark III
  • *****
  • Posts: 1,217
  • Neinzul Y PzKpfw Tiger!
Re: Shrugger! Unity!
« Reply #308 on: September 12, 2015, 11:22:53 am »
Garbage Collection! Any good advice, resources or things-to-keep-in-mind about it? :)
The beatings shall continue
until morale improves!

Offline keith.lamothe

  • Arcen Games Staff
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 19,505
Re: Shrugger! Unity!
« Reply #309 on: September 12, 2015, 11:34:26 am »
Garbage Collection! Any good advice, resources or things-to-keep-in-mind about it? :)
Frequent GC's cause problems, notably music skipping, making the UI feel rough (mouse jumping around when GC happens mid-motion, etc). Really frequent GC's tank your framerate.

Frequent GC's come from a ton of transient heap allocation, meaning stuff that doesn't go on the stack (generally speaking, anything in a function's parameter list is on the stack, though params[] stuff may get put on the heap sometimes, not sure) that is allocated via new and then goes out of scope quickly after that allocation.

Obviously, the solution isn't simply to hold onto everything you ever allocate, or you'll run out of RAM.

Nor is the solution to put everything on the stack, which is technically difficult to accomplish and leads to lots of bugs because the copy-by-value behavior of structs can be non-intuitive with complex stuff (it's also impossible with recursive data structures).

But it does sometimes mean holding onto allocated space longer than you might otherwise, and sometimes putting stuff on the stack you wouldn't otherwise.

The first things to check for if you're seeing frequent GC's, though, are things like List<T>.Contains() where T is a value type (int, enum, struct, etc), if that happens in a section that's hit a lot it leads to a ton of boxing that thrashes the heap with transient allocation.

That doesn't mean doing a find-all on Contains and just hitting whatever you find, though, but rather running the unity profiler with judicious use of Profiler.BeginSample() and Profiler.EndSample() (we use wrappers around those), and looking at the memory column of the profiler to see what sections are causing the most allocation during your normal frames. Then narrow down the sections with more begin-sample and end-sample (best not to nest them too much, though, to keep overhead down). Generally speaking allocation for idle-state can be kept to zero bytes, or close enough for government work.
Have ideas or bug reports for one of our games? Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline Shrugging Khan

  • Hero Member Mark III
  • *****
  • Posts: 1,217
  • Neinzul Y PzKpfw Tiger!
Re: Shrugger! Unity!
« Reply #310 on: September 13, 2015, 04:09:23 pm »
Holy what!? The Profiler is now included in the free version!  :o

Thanks for that information, it kinda passed me by so far. I now have a lot of things to do, all at once!
The beatings shall continue
until morale improves!

Offline keith.lamothe

  • Arcen Games Staff
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 19,505
Re: Shrugger! Unity!
« Reply #311 on: September 13, 2015, 05:17:08 pm »
Holy what!? The Profiler is now included in the free version!  :o
Is it? I'd forgotten you were on the free version. The profiler is... not nearly as good as ANTS or whatever like we were able to use in .NET itself. But it's better than nothing, once you learn how to use it (and how to not use it).
Have ideas or bug reports for one of our games? Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!

Offline Draco18s

  • Resident Velociraptor
  • Core Member Mark V
  • *****
  • Posts: 4,251
Re: Shrugger! Unity!
« Reply #312 on: September 13, 2015, 08:13:13 pm »
You on 5?  Pretty much everything is free in 5.  The stuff that isn't is stuff that either takes manpower or cloud resources.

Offline Shrugging Khan

  • Hero Member Mark III
  • *****
  • Posts: 1,217
  • Neinzul Y PzKpfw Tiger!
Re: Shrugger! Unity!
« Reply #313 on: September 14, 2015, 03:54:30 am »
Yeah, we switched to 5 on a "more stuff can't hurt (too much)" basis, but I kinda assumed the profiler was still restricted to pro because...I dunno. Because they'd never give away something that important for free? I considered buying Unity Pro pretty much just for the profiler, a while ago, but I don't have that much money to spend.
I had even written my own pseudo-profiler based on Time.realtimesincestartup comparisons, flawed as that was.

Now I can actually see that our recent spikes in delta-time were caused not just by this-and-that, but even by which method call exactly. This will greatly simplify optimisation - but with great optimisation potential comes an opportunity to waste all my time on that, so better not go overboard...

...also, I still don't understand how to read the Profiler's information on GC. Guess I have some more delving into the Unity Docs to do  :o
The beatings shall continue
until morale improves!

Offline keith.lamothe

  • Arcen Games Staff
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 19,505
Re: Shrugger! Unity!
« Reply #314 on: September 14, 2015, 08:46:56 am »
This will greatly simplify optimisation - but with great optimisation potential comes an opportunity to waste all my time on that, so better not go overboard...
Premature optimization is the root of all evil (or at least most of it) in programming. -Donald Knuth

Quote
...also, I still don't understand how to read the Profiler's information on GC. Guess I have some more delving into the Unity Docs to do  :o
On the CPU Usage mode (modes are along the left side of the profiler window, on the top part), the bottom grid in my display has the following columns:

Overview (which code section it's talking about)
Total (% CPU for that frame in that section and all subsections under it)
Self (% CPU for that frame in just that section, not including anything in a subsection under it)
Calls (# of times that section was entered during that frame)
GC Alloc (# of bytes allocated on the heap in that section during that frame)
Time ms (# of milliseconds spent in that section during that frame and all subsections under it)
Self ms (# of milliseconds spent in just that section during that frame, not including anything in a subsection under it)

If you don't get all those, try right-clicking one of the column headers you do have, and see if GC Alloc is in the list, and click it.
Have ideas or bug reports for one of our games? Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!