From 105b0f57d03f0f2ae6f8d16135a947a6b454e8a3 Mon Sep 17 00:00:00 2001 From: yankejustin Date: Fri, 7 Aug 2015 13:49:15 -0400 Subject: [PATCH 1/5] Reduced string concatenation Reduce the amount of string concatenation when converting an ItemStack to a string. --- TrueCraft.API/ItemStack.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/TrueCraft.API/ItemStack.cs b/TrueCraft.API/ItemStack.cs index 6921d34..d6e0f44 100644 --- a/TrueCraft.API/ItemStack.cs +++ b/TrueCraft.API/ItemStack.cs @@ -252,11 +252,14 @@ namespace TrueCraft.API { if (Empty) return "(Empty)"; - string result = "ID: " + ID; - if (Count != 1) result += "; Count: " + Count; - if (Metadata != 0) result += "; Metadata: " + Metadata; - if (Nbt != null) result += Environment.NewLine + Nbt.ToString(); - return "(" + result + ")"; + + StringBuilder resultBuilder = new StringBuilder("ID: " + ID); + + if (Count != 1) resultBuilder.Append("; Count: " + Count); + if (Metadata != 0) resultBuilder.Append("; Metadata: " + Metadata); + if (Nbt != null) resultBuilder.Append(Environment.NewLine + Nbt.ToString()); + + return "(" + resultBuilder.ToString() + ")"; } /// From cf5d54b9dac175489a412dbd0cd2d954e83cbed8 Mon Sep 17 00:00:00 2001 From: yankejustin Date: Fri, 7 Aug 2015 13:58:58 -0400 Subject: [PATCH 2/5] Optimize ray intersection calculation Check to make sure the direction is not 0 only once. Also, check to make sure it is 0 first so we don't unnecessarily calculate if the position is greater-than or less-than another without purpose. --- TrueCraft.API/Ray.cs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/TrueCraft.API/Ray.cs b/TrueCraft.API/Ray.cs index dbd3e57..2da021e 100644 --- a/TrueCraft.API/Ray.cs +++ b/TrueCraft.API/Ray.cs @@ -94,18 +94,29 @@ namespace TrueCraft.API Vector3 maxT = new Vector3(-1.0f); //Vector3 minT = new Vector3(-1.0f); //calcul intersection with each faces - if (Position.X < box.Min.X && Direction.X != 0.0f) - maxT.X = (box.Min.X - Position.X) / Direction.X; - else if (Position.X > box.Max.X && Direction.X != 0.0f) - maxT.X = (box.Max.X - Position.X) / Direction.X; - if (Position.Y < box.Min.Y && Direction.Y != 0.0f) - maxT.Y = (box.Min.Y - Position.Y) / Direction.Y; - else if (Position.Y > box.Max.Y && Direction.Y != 0.0f) - maxT.Y = (box.Max.Y - Position.Y) / Direction.Y; - if (Position.Z < box.Min.Z && Direction.Z != 0.0f) - maxT.Z = (box.Min.Z - Position.Z) / Direction.Z; - else if (Position.Z > box.Max.Z && Direction.Z != 0.0f) - maxT.Z = (box.Max.Z - Position.Z) / Direction.Z; + if (Direction.X != 0.0f) + { + if (Position.X < box.Min.X) + maxT.X = (box.Min.X - Position.X) / Direction.X; + else if (Position.X > box.Max.X) + maxT.X = (box.Max.X - Position.X) / Direction.X; + } + + if (Direction.Y != 0.0f) + { + if (Position.Y < box.Min.Y) + maxT.Y = (box.Min.Y - Position.Y) / Direction.Y; + else if (Position.Y > box.Max.Y) + maxT.Y = (box.Max.Y - Position.Y) / Direction.Y; + } + + if (Direction.Z != 0.0f) + { + if (Position.Z < box.Min.Z) + maxT.Z = (box.Min.Z - Position.Z) / Direction.Z; + else if (Position.Z > box.Max.Z) + maxT.Z = (box.Max.Z - Position.Z) / Direction.Z; + } //get the maximum maxT if (maxT.X > maxT.Y && maxT.X > maxT.Z) From 1ba64128bb1d74020ca3f3d14b51fc9f7a4afd2f Mon Sep 17 00:00:00 2001 From: yankejustin Date: Fri, 7 Aug 2015 14:11:33 -0400 Subject: [PATCH 3/5] Dispose of old textures If there were previously-stored fonts, dispose of them before creating new fonts. --- TrueCraft.Client/Rendering/Font.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/TrueCraft.Client/Rendering/Font.cs b/TrueCraft.Client/Rendering/Font.cs index 9900c8e..0e48441 100644 --- a/TrueCraft.Client/Rendering/Font.cs +++ b/TrueCraft.Client/Rendering/Font.cs @@ -73,6 +73,14 @@ namespace TrueCraft.Client.Rendering using (var contents = File.OpenRead(Path.Combine(contentManager.RootDirectory, definitionPath))) _definition = FontLoader.Load(contents); + if (_textures != null) + { + for (int i = 0; i < _textures.Length; i++) + { + _textures[i].Dispose(); + } + } + // We need to support multiple texture pages for more than plain ASCII text. _textures = new Texture2D[_definition.Pages.Count]; for (int i = 0; i < _definition.Pages.Count; i++) From c4cc852d60d861faa969844dc8d33f7bd81532c1 Mon Sep 17 00:00:00 2001 From: yankejustin Date: Fri, 7 Aug 2015 14:36:02 -0400 Subject: [PATCH 4/5] Ensure stream is disposed Make sure that, even if an exception is thrown in between, that the MemoryStream is disposed of by using a 'using' block. --- TrueCraft.Client/Rendering/TextureMapper.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/TrueCraft.Client/Rendering/TextureMapper.cs b/TrueCraft.Client/Rendering/TextureMapper.cs index 29d4c79..2003877 100644 --- a/TrueCraft.Client/Rendering/TextureMapper.cs +++ b/TrueCraft.Client/Rendering/TextureMapper.cs @@ -101,11 +101,12 @@ namespace TrueCraft.Client.Rendering { try { - var ms = new MemoryStream(); - CopyStream(stream, ms); - ms.Seek(0, SeekOrigin.Begin); - AddTexture(key, new PngReader().Read(ms, Device)); - ms.Dispose(); + using (var ms = new MemoryStream()) + { + CopyStream(stream, ms); + ms.Seek(0, SeekOrigin.Begin); + AddTexture(key, new PngReader().Read(ms, Device)); + } } catch (Exception ex) { Console.WriteLine("Exception occured while loading {0} from texture pack:\n\n{1}", key, ex); } } From 3830061005c5b62b11c02cb4aeea49a068a7609a Mon Sep 17 00:00:00 2001 From: yankejustin Date: Fri, 7 Aug 2015 14:38:09 -0400 Subject: [PATCH 5/5] Don't bother clearing the Dictionary We disposed of the pairs' values. Just remove the reference and let the garbage collector collect the rest. By calling clear, we are just wasting our time "removing" the items that the garbage collector would have to do anyways. Essentially this call just takes up time without reason. --- TrueCraft.Client/Rendering/TextureMapper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/TrueCraft.Client/Rendering/TextureMapper.cs b/TrueCraft.Client/Rendering/TextureMapper.cs index 2003877..0e0486c 100644 --- a/TrueCraft.Client/Rendering/TextureMapper.cs +++ b/TrueCraft.Client/Rendering/TextureMapper.cs @@ -185,7 +185,6 @@ namespace TrueCraft.Client.Rendering foreach (var pair in Customs) pair.Value.Dispose(); - Customs.Clear(); Customs = null; Device = null; IsDisposed = true;