From c7f021e62d5be3a7e1f43a6f8c8d8caf774a5edb Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 23 Jan 2022 21:18:13 +0100 Subject: [PATCH] Marked CopyExtensions as obsolete --- CHANGELOG.md | 3 +++ MLEM.Data/CopyExtensions.cs | 7 ++++++- Sandbox/GameImpl.cs | 8 -------- Tests/DataTests.cs | 28 ---------------------------- 4 files changed, 9 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9ecdac..e29bfd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,9 @@ Improvements - Rethrow exceptions when no RawContentManager readers could be constructed - Make Newtonsoft.Json dependency optional +Removals +- Marked CopyExtensions as obsolete + ## 5.2.0 ### MLEM Additions diff --git a/MLEM.Data/CopyExtensions.cs b/MLEM.Data/CopyExtensions.cs index 189e52e..0ab79a4 100644 --- a/MLEM.Data/CopyExtensions.cs +++ b/MLEM.Data/CopyExtensions.cs @@ -7,6 +7,7 @@ namespace MLEM.Data { /// /// A set of extensions for dealing with copying objects. /// + [Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")] public static class CopyExtensions { private const BindingFlags DefaultFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; @@ -21,6 +22,7 @@ namespace MLEM.Data { /// A predicate that determines whether or not the given field should be copied. If null, all fields will be copied. /// The type of the object to copy /// A shallow copy of the object + [Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")] public static T Copy(this T obj, BindingFlags flags = DefaultFlags, Predicate fieldInclusion = null) { var copy = (T) Construct(typeof(T), flags); obj.CopyInto(copy, flags, fieldInclusion); @@ -36,6 +38,7 @@ namespace MLEM.Data { /// A predicate that determines whether or not the given field should be copied. If null, all fields will be copied. /// The type of the object to copy /// A deep copy of the object + [Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")] public static T DeepCopy(this T obj, BindingFlags flags = DefaultFlags, Predicate fieldInclusion = null) { var copy = (T) Construct(typeof(T), flags); obj.DeepCopyInto(copy, flags, fieldInclusion); @@ -50,6 +53,7 @@ namespace MLEM.Data { /// The binding flags for field searching /// A predicate that determines whether or not the given field should be copied. If null, all fields will be copied. /// The type of the object to copy + [Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")] public static void CopyInto(this T obj, T otherObj, BindingFlags flags = DefaultFlags, Predicate fieldInclusion = null) { foreach (var field in typeof(T).GetFields(flags)) { if (fieldInclusion == null || fieldInclusion(field)) @@ -66,6 +70,7 @@ namespace MLEM.Data { /// The binding flags for field searching /// A predicate that determines whether or not the given field should be copied. If null, all fields will be copied. /// The type of the object to copy + [Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")] public static void DeepCopyInto(this T obj, T otherObj, BindingFlags flags = DefaultFlags, Predicate fieldInclusion = null) { foreach (var field in obj.GetType().GetFields(flags)) { if (fieldInclusion != null && !fieldInclusion(field)) @@ -109,6 +114,6 @@ namespace MLEM.Data { /// /// An attribute that, when added to a constructor, will make that constructor the one used by , and . /// - [AttributeUsage(AttributeTargets.Constructor)] + [AttributeUsage(AttributeTargets.Constructor), Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")] public class CopyConstructorAttribute : Attribute {} } \ No newline at end of file diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 76cce39..42e6835 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -119,13 +119,6 @@ namespace Sandbox { Console.WriteLine(vec + " -> " + dir); } - var copy = obj.DeepCopy(); - Console.WriteLine(copy); - - var intoCopy = new Test(Vector2.One, "test") {OtherTest = new Test(Vector2.One, "other")}; - obj.DeepCopyInto(intoCopy); - Console.WriteLine(intoCopy); - var writer = new StringWriter(); this.Content.GetJsonSerializer().Serialize(writer, obj); //Console.WriteLine(writer.ToString()); @@ -345,7 +338,6 @@ namespace Sandbox { public Direction2 Dir { get; set; } public Test OtherTest; - [CopyConstructor] public Test(Vector2 test, string test2) { Console.WriteLine("Constructed with " + test + ", " + test2); } diff --git a/Tests/DataTests.cs b/Tests/DataTests.cs index ef6a0e1..7073ced 100644 --- a/Tests/DataTests.cs +++ b/Tests/DataTests.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.IO; using System.Numerics; using Microsoft.Xna.Framework; @@ -38,33 +37,6 @@ namespace Tests { Assert.AreEqual(this.testObject, read); } - [Test] - public void TestCopy() { - var copy = this.testObject.Copy(); - Assert.AreEqual(this.testObject, copy); - Assert.AreSame(this.testObject.OtherTest, copy.OtherTest); - - var deepCopy = this.testObject.DeepCopy(); - Assert.AreEqual(this.testObject, deepCopy); - Assert.AreNotSame(this.testObject.OtherTest, deepCopy.OtherTest); - } - - [Test] - public void TestCopySpeed() { - const int count = 1000000; - var stopwatch = Stopwatch.StartNew(); - for (var i = 0; i < count; i++) - this.testObject.Copy(); - stopwatch.Stop(); - TestContext.WriteLine($"Copy took {stopwatch.Elapsed.TotalMilliseconds / count * 1000000}ns on average"); - - stopwatch.Restart(); - for (var i = 0; i < count; i++) - this.testObject.DeepCopy(); - stopwatch.Stop(); - TestContext.WriteLine($"DeepCopy took {stopwatch.Elapsed.TotalMilliseconds / count * 1000000}ns on average"); - } - [Test] public void TestDynamicEnum() { var flags = new TestEnum[100];