Fps drops when camera moved, any ideas

I read a a file and create entities, there are 4000 of them, so not a big deal, fps is stable. I have cinemachine setup for orbiting around the objects. As soon as I move the camera fps drops to 40-50. I want to read protein file and display all atoms that are in the file. Here is the code (read file from MonoBehaviour, access system and trigger it through MonoBehaviour):

using System;
using System.IO;
using Core.Dots.Systems;
using Cysharp.Threading.Tasks;
using NaughtyAttributes;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;

namespace Core.Reader
{
    public class PdbReader : MonoBehaviour
    {
        public TextAsset PdfFile;
        private Protein _protein;
        
        [Button("Load")]
        public void Load()
        {
            if (string.IsNullOrEmpty(PdfFile.text)) return;
            
            _ = LoadPdpFile(PdfFile.text, () =>
            {
                CreateProtein();
            });
        }

        private void CreateProtein()
        {
            var world = World.DefaultGameObjectInjectionWorld;
            var systemHandle = world.Unmanaged.GetExistingSystemState<PdbEntityBuilderSystem>();
            
            // Collect parsed atom data
            var atoms = new NativeArray<AtomAuthoring.AtomComponent>(_protein.Atoms.Count, Allocator.Temp);
            var index = 0;
            foreach (var atom in _protein.Atoms)
            {
                atoms[index++] = new AtomAuthoring.AtomComponent
                {
                    AtomNumber = atom.Key,
                    AtomType = atom.Value.AtomType,
                    Position = atom.Value.Position,
                    Size = GetAtomSize(atom.Value.AtomType)
                };
            }
            
            world.Unmanaged.GetUnsafeSystemRef<PdbEntityBuilderSystem>(systemHandle.SystemHandle).LoadAtoms(atoms);
        }

        async UniTaskVoid LoadPdpFile(string data, Action onComplete)
        {
            await UniTask.RunOnThreadPool(() =>
            {
                ParsePDBFile(data);
            });

            Debug.Log(_protein.ToString());
            onComplete?.Invoke();
        }
        
        void ParsePDBFile(string data)
        {
            _protein = new Protein("Protein");
            using StringReader reader = new StringReader(data);
            while (reader.ReadLine() is { } line)
            {
                if (line.StartsWith("ATOM") || line.StartsWith("HETATM"))
                {
                    var atom = ParseAtomLine(line);
                    if (atom != null)
                    {
                        _protein.AddAtom(atom.AtomNumber, atom.AtomType, atom.Position);
                    }
                }
            }
        }
        
        Atom ParseAtomLine(string line)
        {
            try
            {
                int atomNumber = int.Parse(line.Substring(6, 5).Trim());
                string atomType = line.Substring(76, 2).Trim(); // Atom type
                float x = float.Parse(line.Substring(30, 8));
                float y = float.Parse(line.Substring(38, 8));
                float z = float.Parse(line.Substring(46, 8));

                return new Atom(atomNumber, atomType, new Vector3(x, y, z));
            }
            catch
            {
                Debug.LogWarning("Failed to parse line: " + line);
                return null;
            }
        }
        
        private float GetAtomSize(string atomType, float scaleFactor = 1.5f)
        {
            float baseSize = atomType switch
            {
                "H" => 1.2f,  // Hydrogen
                "C" => 1.7f,  // Carbon
                "N" => 1.55f, // Nitrogen
                "O" => 1.52f, // Oxygen
                "P" => 1.8f,  // Phosphorus
                "S" => 1.8f,  // Sulfur
                "Cl" => 1.75f, // Chlorine
                _ => 1.5f     // Default size for unknown atoms
            };

            // Apply scaling factor
            return baseSize * scaleFactor;
        }
    }
}

using Core.Dots.Components;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;

namespace Core.Dots.Systems
{
    [BurstCompile]
    public partial struct PdbEntityBuilderSystem : ISystem
    {
        private NativeList<AtomAuthoring.AtomComponent> _atoms;
        private bool _dataLoaded;
        private EntityManager _entityManager;
        
        public void OnCreate(ref SystemState state)
        {
            state.RequireForUpdate<PdbCreatorAuthoring.PdbCreatorComponent>();
            _dataLoaded = false;
            _atoms = new NativeList<AtomAuthoring.AtomComponent>(Allocator.Persistent);
            _entityManager = state.EntityManager;
        }
        
        public void OnDestroy(ref SystemState state)
        {
            if(_atoms.IsCreated) 
                _atoms.Dispose();
        }
        
        public void OnUpdate(ref SystemState state)
        {
            if(!_dataLoaded || !_atoms.IsCreated) return;

            var pdbCreator = SystemAPI.GetSingleton<PdbCreatorAuthoring.PdbCreatorComponent>();
            var ecb = new EntityCommandBuffer(Allocator.Temp);

            foreach (var atom in _atoms)
            {
                var entity = ecb.Instantiate(pdbCreator.AtomPrefab);
                // ecb.AddComponent(entity, atom);
                ecb.SetComponent(entity,new LocalTransform()
                {
                    Position = atom.Position,
                    Rotation = Quaternion.identity,
                    Scale = atom.Size
                });
                
                ecb.AddComponent(entity, new URPMaterialPropertyBaseColor
                {
                    Value = GetColorFromAtomType(atom.AtomType)
                });
            }
            
            
            ecb.Playback(state.EntityManager);
            ecb.Dispose();
            _atoms.Dispose();
            _dataLoaded = false;
            // state.Enabled = false;
        }
        
        
        
        public void LoadAtoms(NativeArray<AtomAuthoring.AtomComponent> atoms)
        {
            if (_atoms.IsCreated)
                _atoms.Dispose();
            
            _atoms = new NativeList<AtomAuthoring.AtomComponent>(atoms.Length, Allocator.Persistent);
            _atoms.AddRange(atoms);
            Debug.Log($"{atoms.Length} {_atoms.Length}");
            NativeArray<AtomAuthoring.AtomComponent>.Copy(atoms, _atoms);
            _dataLoaded = true;
        }
        
        private static float4 GetColorFromAtomType(FixedString32Bytes atomType)
        {
            return atomType.Value switch
            {
                "C"  => new float4(0.3f, 0.3f, 0.3f, 1f),  // Carbon: Grey
                "H"  => new float4(1.0f, 1.0f, 1.0f, 1f),  // Hydrogen: White
                "O"  => new float4(1.0f, 0.0f, 0.0f, 1f),  // Oxygen: Red
                "N"  => new float4(0.0f, 0.0f, 1.0f, 1f),  // Nitrogen: Blue
                "S"  => new float4(1.0f, 1.0f, 0.0f, 1f),  // Sulfur: Yellow
                "P"  => new float4(1.0f, 0.5f, 0.0f, 1f),  // Phosphorus: Orange
                "Cl" => new float4(0.0f, 1.0f, 0.0f, 1f),  // Chlorine: Green
                "F"  => new float4(0.5f, 1.0f, 0.5f, 1f),  // Fluorine: Pale Green
                "Br" => new float4(0.65f, 0.16f, 0.16f, 1f), // Bromine: Brown
                "I"  => new float4(0.58f, 0.0f, 0.83f, 1f),  // Iodine: Violet
                _    => new float4(0.5f, 0.5f, 0.5f, 1f)   // Default: Grey
            };
        }
    }
}

For all performance and optimization issues, ALWAYS start by using the Profiler window:

Window → Analysis → Profiler

Generally optimization is:

  • avoid doing the thing that is slow (easiest)
  • do it fewer times and store its result
  • do the slow thing less frequently over time
  • do the slow thing when nobody cares much (during level loading or in another thread, etc.)
  • find a faster way to do the thing (hardest)

DO NOT OPTIMIZE “JUST BECAUSE…” If you don’t have a problem, DO NOT OPTIMIZE!

If you DO have a problem, there is only ONE way to find out: measuring with the profiler.

Failure to use the profiler first means you’re just guessing, making a mess of your code for no good reason.

Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

Do not blindly reach for multi-threading or async… understand your performance problems first:

Remember that you are gathering information at this stage. You cannot FIX until you FIND.

Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

Don’t forget about the Frame Debugger window either, available right near the Profiler in the menu system.

Notes on optimizing UnityEngine.UI setups:

When you are investigating a performance concern, you may even need to create custom tooling or scripts to clearly expose the issue and help you reason about possible solutions. This may even involve making special instrumented builds of your game capable of running on the actual problematic target hardware.

At a minimum you want to clearly understand what performance issues you are having:

  • running too slowly?
  • loading too slowly?
  • using too much runtime memory?
  • final bundle too large?
  • too much network traffic?
  • something else?

If you are unable to engage the profiler, then your next solution is gross guessing changes, such as “reimport all textures as 32x32 tiny textures” or “replace some complex 3D objects with cubes/capsules” to try and figure out what is bogging you down.

Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

This sort of speculative optimization assumes you’re properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.