From e8852089e1db25277e1d03d61fa516c94844977b Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 20 May 2024 20:10:47 -0700 Subject: [PATCH] slight dictionary optimizing --- .../LDtkTilesetImporterModules.cs | 6 ++++++ .../Extensions/Definition/TilesetDefinition.cs | 16 ++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Assets/LDtkUnity/Editor/ScriptedImporter/LDtkTilesetImporterModules.cs b/Assets/LDtkUnity/Editor/ScriptedImporter/LDtkTilesetImporterModules.cs index 82586ea6f..3833ef081 100644 --- a/Assets/LDtkUnity/Editor/ScriptedImporter/LDtkTilesetImporterModules.cs +++ b/Assets/LDtkUnity/Editor/ScriptedImporter/LDtkTilesetImporterModules.cs @@ -134,6 +134,12 @@ private bool ReformatRectMetaData(List srcRects) int jsonTileCount = srcRects.Count; + //it can pre allocate if the list is empty during a first-time import + if (_sprites.IsNullOrEmpty()) + { + _sprites = new List(jsonTileCount); + } + // trim metas off the end of the list to match the new src count. // LDtk handles this in the exact same way where if the tile count decreased, then any old tiles are complete if (_sprites.Count > jsonTileCount) diff --git a/Assets/LDtkUnity/Runtime/Data/Extensions/Definition/TilesetDefinition.cs b/Assets/LDtkUnity/Runtime/Data/Extensions/Definition/TilesetDefinition.cs index 677227302..47005f5ca 100644 --- a/Assets/LDtkUnity/Runtime/Data/Extensions/Definition/TilesetDefinition.cs +++ b/Assets/LDtkUnity/Runtime/Data/Extensions/Definition/TilesetDefinition.cs @@ -52,27 +52,23 @@ internal Dictionary CustomDataToDictionary() Dictionary dict = new Dictionary(CustomData.Length); foreach (TileCustomMetadata metadata in CustomData) { - if (!dict.ContainsKey(metadata.TileId)) - { - dict.Add(metadata.TileId, metadata.Data); - continue; - } - dict[metadata.TileId] = metadata.Data; + dict.Add(metadata.TileId, metadata.Data); } return dict; } internal Dictionary> EnumTagsToDictionary() { - Dictionary> dict = new Dictionary>(); + Dictionary> dict = new Dictionary>(EnumTags.Length); foreach (EnumTagValue tagValue in EnumTags) { foreach (int tileId in tagValue.TileIds) { - if (!dict.ContainsKey(tileId)) + if (!dict.TryGetValue(tileId, out List list)) { - dict.Add(tileId, new List()); + list = new List(1); + dict.Add(tileId, list); } - dict[tileId].Add(tagValue.EnumValueId); + list.Add(tagValue.EnumValueId); } } return dict;