Author Topic: OnEnable is Expensive  (Read 206 times)

f0ff886f

  • Newbie
  • *
  • Posts: 15
    • View Profile
OnEnable is Expensive
« on: March 25, 2020, 02:43:28 PM »
Hello!

We've been using Highlight Plus for a while now, and we're finally getting close to launching our game, so its time for performance checks.

Now, this being our first game, we made lots of mistakes along the way. One of those is we turn on and off gameobjects (and often its folders with many gameobjects) to hide things that aren't supposed to be visible in that area at that time.

What I've realized is that when I do that for objects that have a Highlight Effect, the OnEnable cost is really quite high. For  example:



This is about 12ms of time.

Currently I am trying to create lists of the highlight effect components, disable them all, then turn them on frame by frame to reduce the heavy stutter.

Do you know why it is so expensive? This is on a Ryzen 3700X (but in the editor, in builds the time is similar).

There are about 23-24 mesh renderers in the single instance that takes 3.88ms. Is there anything I can do to lower this cost save for rewriting our entire system of hiding objects :D

I think we literally *only* use the Outer Glow, maybe we could cut out portions of the code that aren't important?



Thanks for any help or ideas!
Matt

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5705
    • View Profile
Re: OnEnable is Expensive
« Reply #1 on: March 25, 2020, 02:51:48 PM »
The first time OnEnable is called on Highlight Effect component, it needs to setup materials and depending on settings, smooth normals which can take some ms depending on mesh complexity (this only occurs for mesh-based outline and glow effects). If you disable and enable an existing object, that shouldn’t take much time though as the smoothed normals are cached.
Try switching Outline quality to Highest even if it’s disabled and see if there’s any improvement.
I’ll review this behaviour though just in case there’s something else we can optimize.


f0ff886f

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: OnEnable is Expensive
« Reply #2 on: March 25, 2020, 03:06:59 PM »
Thank you for the response, it didn't help the performance but now I know a potential alternative (turn on and off every object during the loading screen...)

Is is possible to execute OnEnable on a disabled GameObject and control exactly when this setup happens?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5705
    • View Profile
Re: OnEnable is Expensive
« Reply #3 on: March 25, 2020, 03:11:50 PM »
You could try calling Refresh() method of Highlight Effect component to force the initialisation.
We’ll do some mass enable and try to optimize that.

f0ff886f

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: OnEnable is Expensive
« Reply #4 on: March 25, 2020, 04:33:43 PM »
That didn't seem to help either. If you are able to find anything, it would be a huge help. If there are cut down versions of the effect that we can remove to at least alleviate some of the pain, that would be a huge help too :)

I stepped into SetupMaterials() and it early outs right away:

Code
        void SetupMaterial()
        {

#if UNITY_EDITOR
            staticChildren = false;
#endif

            if (target == null || fxMatMask == null)
                return; <-- here, so something else needs to be setup

I tried setting target to the transform since I see that in OnEnable, but that got a NRE in Fork() on the Instantiate call.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5705
    • View Profile
Re: OnEnable is Expensive
« Reply #5 on: March 26, 2020, 03:08:59 AM »
Try the latest v4.0 update - it comes with optimizations in this field.

f0ff886f

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: OnEnable is Expensive
« Reply #6 on: March 26, 2020, 04:31:04 AM »
Thank you! I only had a chance to spot check a few areas but its looking better, thank you!

The same area as posted in the first post is now


So we went from around 12ms to ~5.7ms, a huge speedup. (Don't worry about the 86ms loop, I still have some reflection probes rendering on awake, a quick bake takes that away completely)).

I think that's enough for now, if I have really bad folders I still have my code to throttle the enabling of the highlight effect and I can apply it only where needed, instead of now needing to do it in the whole game :)

Thank you for such a quick turnaround and so quick support!
Matt

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5705
    • View Profile
Re: OnEnable is Expensive
« Reply #7 on: March 26, 2020, 04:38:15 AM »
Thanks Matt for confirming the results on your end :)