Tips for managing better performance

Now that we have a grasp on how to profile our apps, let's take a look at the primary items that will impact performance. The order of these items is ordered by general importance, but the individual requirements of your app may alter these priorities. Feel free to consult the following checklist the next time you need or want to profile your app:

  • Rendering (includes all CPU and memory resources responsible for rendering a frame):
    • Render loop (CPU performance): Check the timing of the render function and watch for any expensive calls. Ensure that you minimize any object instantiation, logging, or inner loops. Remember that the render function, typically called Update, will be called 30 times per second or more. All the tools we looked at will let you perform this vital task.
    • Frame rate (render time): Outside of optimizing your code, the frame rate will often be dictated by the complexity and number of objects we are rendering. As such, you may want to go as low as optimizing shaders, but many times, you can get great performance gains by reducing the number of triangles or complexity of your models. In a mobile app, this means looking for low poly simple models as assets. Another useful option is to build various Levels Of Detail (LOD) for your model and use the appropriate version for the appropriate detail level. Unity provides an excellent set of free and paid assets for LOD optimization that can make this task easy.
    • Lighting and materials: Not only will the complexity of a model impact performance but also the textures or materials (shaders) and lights you are using to render the model. Ensure that you limit the size of textures or ensure that all your shaders have a fallback or simplification. You will also want to simplify lighting where possible.
    • Memory (graphics): As a general rule, the more memory your app is using, the more expensive a frame will be to render. Of course, there are exceptions, but watching the memory can pinpoint potential issues or even memory leaks. A high memory will often point to models, textures, or other assets that may need optimization.
  • Loading (the process of adding, replacing, or updating new content in the scene):
    • Object instantiation: Large complex meshes with multiple detailed textures will require extra load times. You will often want to cache or preload objects in order to reduce interruptions during loading. For most of our examples, this wasn't an issue, but a good example of where this was a problem was in Chapter 10, Mixing in Mixed Reality, where we used the 3D map.
    • Streaming: Streaming is a great way to load media resources such as audio or video to play just the content you need. In Unity, setting a resource to stream instead of loading completely is fairly easy and can be done at the resource definition, as shown in the following screenshot:
Enabling streaming on an audio resource
    • Garbage collection: All of our platforms manage object lifetime through some form of garbage collection while the app is running. Keeping the number of objects you create and destroy to a minimum will alleviate pressure on the GC. If the GC fills up quickly, this will often trigger an expensive collection operation, which may freeze your app. You can reduce object instantiation and collection by creating object pooling. Object pooling is where you keep a stock of objects in memory, adding and removing objects from the scene as you need.
  • Interaction (includes any activity by the user or the environment, be it physical or artificial):
    • Environment detection: This is a requirement more specific to AR apps and crucial to ARCore. If you are planning to augment detection of point clouds or planes, ensure that you optimize this code as much as possible.
    • Object interaction (physics): Limit the number of objects that you need to test for ray casting or collisions. You can do this by tagging your objects and then filter the tags. In Unity, this feature is built in, but it is fairly easy to implement for other platforms.
    • AI (machine learning): If your app needs to do any AI for an non-player character (NPC) or other agent, then you may want to limit any expensive calls for AI or learning. Instead of running your AI for every frame, you may want to limit it to every fifth or tenth frame, for instance. Often, this has the added benefit of making the AI more realistic or smarter, since it appears to think for a short period before action.

The preceding list is a good place to start when looking for possible performance problems, and it should suit you well as a guide for any platform you need to profile. In the next section, we will cover some general troubleshooting tips that you can use for each platform when developing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.35.81