From 747662ea8caacabc1354a6c42eafc1e5096fa065 Mon Sep 17 00:00:00 2001 From: Anon Date: Thu, 26 Mar 2026 18:01:56 +0100 Subject: [PATCH] More minor optimizations --- MinecraftClient/Mapping/Movement.cs | 4 ++-- MinecraftClient/Physics/BlockShapes.cs | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/MinecraftClient/Mapping/Movement.cs b/MinecraftClient/Mapping/Movement.cs index 6786dbee..0e972e09 100644 --- a/MinecraftClient/Mapping/Movement.cs +++ b/MinecraftClient/Mapping/Movement.cs @@ -232,8 +232,8 @@ namespace MinecraftClient.Mapping int tentativeGScore = current.GScore + (int)current.Location.DistanceSquared(neighbor); // If the neighbor is not in the GScoreDict OR its current tentativeGScore is lower than the previously saved one: - if (!gScoreDict.ContainsKey(neighbor) || - (gScoreDict.ContainsKey(neighbor) && tentativeGScore < gScoreDict[neighbor])) + if (!gScoreDict.TryGetValue(neighbor, out int existingGScore) || + tentativeGScore < existingGScore) { // Save the new relation between the neighbored block and the current one cameFrom[neighbor] = current.Location; diff --git a/MinecraftClient/Physics/BlockShapes.cs b/MinecraftClient/Physics/BlockShapes.cs index 535c1ee4..c960913c 100644 --- a/MinecraftClient/Physics/BlockShapes.cs +++ b/MinecraftClient/Physics/BlockShapes.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.IO; using System.Linq; @@ -19,7 +20,7 @@ namespace MinecraftClient.Physics private static readonly Aabb[] FullBlockArray = { FullBlock }; private static readonly Aabb[] EmptyArray = Array.Empty(); - private static Dictionary? stateToShape; + private static FrozenDictionary? stateToShape; private static Dictionary? prismarineBlocks; private static Dictionary? prismarineShapes; @@ -136,14 +137,21 @@ namespace MinecraftClient.Physics private static void BuildStateMap() { - stateToShape = new Dictionary(); + var builder = new Dictionary(); if (prismarineBlocks is null || prismarineShapes is null) + { + stateToShape = builder.ToFrozenDictionary(); return; + } var palette = Block.Palette; var dict = GetPaletteDict(palette); - if (dict is null) return; + if (dict is null) + { + stateToShape = builder.ToFrozenDictionary(); + return; + } // Group consecutive state IDs by Material to find state ranges per block var materialRanges = new Dictionary>(); @@ -182,20 +190,20 @@ namespace MinecraftClient.Physics { var shapes = prismarineShapes.GetValueOrDefault(singleShapeId, EmptyArray); for (int sid = start; sid <= end; sid++) - stateToShape[sid] = shapes; + builder[sid] = shapes; } else if (blockShapeData is List shapeIdList) { for (int i = 0; i < stateCount && (globalStateOffset + i) < shapeIdList.Count; i++) { int shapeId = shapeIdList[globalStateOffset + i]; - stateToShape[start + i] = prismarineShapes.GetValueOrDefault(shapeId, EmptyArray); + builder[start + i] = prismarineShapes.GetValueOrDefault(shapeId, EmptyArray); } } globalStateOffset += stateCount; } } - + stateToShape = builder.ToFrozenDictionary(); } ///