June 19 thru 27 2018 - Vertex lights, StringBuilder, Arrays vs Lists and Log 0

Over the past 2 weeks I did a lot of optimization, refactoring, and housecleaning of code. And I learned some very important lessons which I'll summarize for you here.

Vertex Lights

First of all, I can't believe I missed this one. In Unity when you create a point light you can choose between "Important" and "Not Important" which I believe is the difference between "pixel lights" and "vertex lights".  Vertex lights are way cheaper and still look pretty good, especially for hot metal that glows :) So now I can have 50 glowing & light-casting objects. I can probably boost that number higher but 50 looks pretty good for now.

String Builder

So, I finally got around to investigating some 5ms delays per frame during the cutting operation that were being caused by some string concatenations.  I read somewhere that I need to use String Builder which is more performant but also generates a lot less garbage. More specifically, I was converting and Int32 to a String and padding it with zeros then using those strings as keys in a dictionary. This was an optimization I made to store data calculated when a line segment is cut. Theoretically this would save the time of having to run the same calculations twice. I never actually benchmarked it though xD. But the stringbuilder update dropped the string concatenations down to less than 1ms from 5ms previously observed.

It was actually pretty difficult to google information on String Builder that I could comprehend and implement quickly so I spent a few days on this. In the end, the most helpful documentations came from this link about String Builder Padding

Arrays vs Lists

Another thing I've been reading a lot is that arrays are much more performant than lists so I looked for areas that would get looped the most often and to my I discovered that all of the mesh data was being collected in lists. So, the main challenge of converting lists to arrays is to dynamically allocate the number of items in the array. What I found was that you can do that with one simple command:

ResizeArray(<array>,<int>);

To be honest I don't remember to what extent I noticed a major change in performance but I think I was staring at the profiler an awful lot and I think I noticed a drop in the amount of GC.Alloc calls that I was seeing in the profiler's time line which is easy to see because they're bright red or pink.  And after I made this update I ran into issues with my obs settings so I got distracted. Fortunately, this is the last major update I made and we can get to the video capture unless you already scrolled down and checked it out ;)

But first...

log 0

Apparently taking a logarithm of zero returns bad things. I ran into a sneaky little bug. I was taking the logarithm of a number in my int32 to string conversion. It's the padding with zeros that was the issue. I was taking the log to determine how many digits the number as so that I could subtract that from the number padding.  But when the number 0 showed up something would go haywire and I would get a stringbuilder error, something like the number of characters in the string exceed the capacity.  Anyway, it was sneaky because I was generating a random number so the error would rarely show.

Anway, here's the latest video I captured on June 27, 2018:




And this is a note to my future self to not feel bad that I missed a few days last week. I was stuck with the log 0 bug and String Builder problem for a few days, then there was a low framerate obs thing, then I got sick on Friday, well actually I was sick most of the week. And then there was the game jam, so don't feel bad...

Comments

Popular Posts