Skip to content

Commit

Permalink
perf: Deserializing json without a try-catch block to increase speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed May 20, 2024
1 parent 9a380fb commit b0cec83
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,19 @@ internal abstract class LDtkJsonImporter<T> : LDtkJsonImporter where T : Scripta
{
protected T ReadAssetText()
{
LDtkProfiler.BeginSample("ReadAllText");
string jsonText = File.ReadAllText(assetPath);
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("ScriptableObject.CreateInstance");
T file = ScriptableObject.CreateInstance<T>();
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample( "SetJson");
file.name = Path.GetFileNameWithoutExtension(assetPath) + "_Json";
file.SetJson(jsonText);
LDtkProfiler.EndSample();

return file;
}

Expand All @@ -346,16 +354,8 @@ public static TJson FromJson<TJson>(string path, LDtkDebugInstance debug = null)
byte[] bytes = File.ReadAllBytes(path);
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample($"FromJson");
TJson json = default;
try
{
json = Utf8Json.JsonSerializer.Deserialize<TJson>(bytes);
}
catch (Exception e)
{
LDtkDebug.LogError($"Failure to deserialize json: {e}", debug);
}
LDtkProfiler.BeginSample($"Deserialize");
TJson json = Utf8Json.JsonSerializer.Deserialize<TJson>(bytes);
LDtkProfiler.EndSample();

LDtkProfiler.EndSample(); //this end sample for the caller up the stack
Expand Down

0 comments on commit b0cec83

Please sign in to comment.