From ffecf24ae1d1be25674573339d05e42b6d47ef2a Mon Sep 17 00:00:00 2001
From: Per-Morten Straume <gamma_ray_per@hotmail.com>
Date: Tue, 16 Mar 2021 20:22:33 +0000
Subject: [PATCH] Dispose of objectsDispose as early as possible

objectsDispose was originally disposed of at the end of the
LoadFile function. However, we can dispose of it as soon as the
MakeFileObjectSoAJob is finished.

Also added an objectCount variable for clarity, and to avoid using
objects.Length after scheduling objectDispose.Dispose.
---
 Assets/_Scripts/ObjectLoader.cs | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/Assets/_Scripts/ObjectLoader.cs b/Assets/_Scripts/ObjectLoader.cs
index d202217..69ee814 100644
--- a/Assets/_Scripts/ObjectLoader.cs
+++ b/Assets/_Scripts/ObjectLoader.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Diagnostics.CodeAnalysis;
@@ -53,10 +53,11 @@ public class ObjectLoader : MonoBehaviour
         var fileContents = System.IO.File.ReadAllText(filepath);
         var objectsList = JsonUtility.FromJson<FileObjects>(fileContents).Objects;
 
+        var objectCount = objectsList.Count;
         var objectsDispose = objectsList.ViewAsNativeArray(out var objects);
-        var transforms = new NativeArray<float4x4>(objects.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
-        var meshTypes = new NativeArray<GraphicsSystem.MeshType>(objects.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
-        var materialIndices = new NativeArray<UInt16>(objects.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
+        var transforms = new NativeArray<float4x4>(objectCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
+        var meshTypes = new NativeArray<GraphicsSystem.MeshType>(objectCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
+        var materialIndices = new NativeArray<UInt16>(objectCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
 
         JobHandle handle;
         handle = new MakeFileObjectSoAJob
@@ -66,9 +67,11 @@ public class ObjectLoader : MonoBehaviour
             MeshTypes = meshTypes,
             Transforms = transforms,
         }
-        .Schedule(objects.Length, 64);
+        .Schedule(objectCount, 64);
 
-        var sortingIndices = new NativeArray<int>(objects.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
+        handle = objectsDispose.Dispose(handle);
+
+        var sortingIndices = new NativeArray<int>(objectCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
         handle = JobAlgorithms.ScheduleConsecutiveFill(sortingIndices, handle);
         handle = new PartitionByMaterial
         {
@@ -84,7 +87,7 @@ public class ObjectLoader : MonoBehaviour
         handle = JobAlgorithms.ScheduleShuffle(meshTypes.Reinterpret<byte>(), sortingIndices, handle);
         handle = JobAlgorithms.ScheduleShuffle(materialIndices, sortingIndices, handle);
 
-        var materialRanges = new NativeList<RangeInt>(materialIndices.Length, Allocator.Persistent);
+        var materialRanges = new NativeList<RangeInt>(objectCount, Allocator.Persistent);
         handle = new RangeifyByMaterial
         {
             MaterialIndices = materialIndices,
@@ -122,7 +125,7 @@ public class ObjectLoader : MonoBehaviour
         handle = materialRanges.Dispose(handle);
         handle = sortingIndices.Dispose(handle);
 
-        var finalRangesList = new NativeList<RangeInt>(materialIndices.Length, Allocator.Persistent);
+        var finalRangesList = new NativeList<RangeInt>(objectCount, Allocator.Persistent);
         handle = new RangeifyByMaterialAndMesh
         {
             MaterialIndices = materialIndices,
@@ -131,13 +134,12 @@ public class ObjectLoader : MonoBehaviour
         }
         .Schedule(handle);
 
-        handle = objectsDispose.Dispose(handle);
-        
+
         // Need to wait for the jobs to complete here because we need to copy finalRangesList to ranges
         JobHandle.ScheduleBatchedJobs();
         while (!handle.IsCompleted)
             yield return null;
-        
+
         handle.Complete();
 
         var ranges = new NativeArray<RangeInt>(finalRangesList, Allocator.Persistent);
-- 
GitLab