Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj =================================================================== diff -u -r23766b602d9b509a2ffc030f284e8d5e82325a3e -rdd460a16f3fed1c1fc5abc2842ea57015f6f4a5c --- Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision 23766b602d9b509a2ffc030f284e8d5e82325a3e) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision dd460a16f3fed1c1fc5abc2842ea57015f6f4a5c) @@ -57,6 +57,7 @@ Properties\GlobalAssembly.cs + Index: Application/Ringtoets/src/Application.Ringtoets.Storage/BinaryConverters/BinaryDataEqualityHelper.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/BinaryConverters/BinaryDataEqualityHelper.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/BinaryConverters/BinaryDataEqualityHelper.cs (revision dd460a16f3fed1c1fc5abc2842ea57015f6f4a5c) @@ -0,0 +1,52 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +namespace Application.Ringtoets.Storage.BinaryConverters +{ + /// + /// Class that helps comparing byte-arrays in a performance optimized manner. + /// + public static class BinaryDataEqualityHelper + { + /// + /// Determines if two byte arrays are equal to each other. + /// + /// The first array, cannot be null. + /// The second array, cannot be null. + /// True if the two arrays are equals, false otherwise. + public static bool AreEqual(byte[] array1, byte[] array2) + { + if (array1.Length != array2.Length) + { + return false; + } + // Note: Do not turn this into a linq query, as that is less performance optimal! + for (int i = 0; i < array1.Length; i++) + { + if (!array1[i].Equals(array2[i])) + { + return false; + } + } + return true; + } + } +} \ No newline at end of file Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Update/Piping/StochasticSoilModelUpdateExtensions.cs =================================================================== diff -u -r23766b602d9b509a2ffc030f284e8d5e82325a3e -rdd460a16f3fed1c1fc5abc2842ea57015f6f4a5c --- Application/Ringtoets/src/Application.Ringtoets.Storage/Update/Piping/StochasticSoilModelUpdateExtensions.cs (.../StochasticSoilModelUpdateExtensions.cs) (revision 23766b602d9b509a2ffc030f284e8d5e82325a3e) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Update/Piping/StochasticSoilModelUpdateExtensions.cs (.../StochasticSoilModelUpdateExtensions.cs) (revision dd460a16f3fed1c1fc5abc2842ea57015f6f4a5c) @@ -91,7 +91,11 @@ private static void UpdateSoilModelSegment(StochasticSoilModel model, StochasticSoilModelEntity entity) { - entity.SegmentPoints = new Point2DBinaryConverter().ToBytes(model.Geometry); + byte[] currentSegmentPointData = new Point2DBinaryConverter().ToBytes(model.Geometry); + if (!BinaryDataEqualityHelper.AreEqual(entity.SegmentPoints, currentSegmentPointData)) + { + entity.SegmentPoints = currentSegmentPointData; + } } } } \ No newline at end of file Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj =================================================================== diff -u -r23766b602d9b509a2ffc030f284e8d5e82325a3e -rdd460a16f3fed1c1fc5abc2842ea57015f6f4a5c --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision 23766b602d9b509a2ffc030f284e8d5e82325a3e) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision dd460a16f3fed1c1fc5abc2842ea57015f6f4a5c) @@ -86,6 +86,7 @@ + Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BinaryConverters/BinaryDataEqualityHelperTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BinaryConverters/BinaryDataEqualityHelperTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BinaryConverters/BinaryDataEqualityHelperTest.cs (revision dd460a16f3fed1c1fc5abc2842ea57015f6f4a5c) @@ -0,0 +1,94 @@ +using System; +using System.Diagnostics; +using System.Linq; + +using Application.Ringtoets.Storage.BinaryConverters; + +using NUnit.Framework; + +namespace Application.Ringtoets.Storage.Test.BinaryConverters +{ + [TestFixture] + public class BinaryDataEqualityHelperTest + { + [Test] + public void AreEqual_ArraysAreNotEqual_ReturnFalse() + { + // Setup + var random = new Random(42); + const int arraySize = 10; + var array1 = new byte[arraySize]; + var array2 = new byte[arraySize]; + random.NextBytes(array1); + random.NextBytes(array2); + + // Precondition + CollectionAssert.AreNotEqual(array1, array2); + + // Call + bool areCollectionEqual = BinaryDataEqualityHelper.AreEqual(array1, array2); + + // Assert + Assert.IsFalse(areCollectionEqual); + } + + [Test] + public void AreEqual_ArraysAreEqual_ReturnTrue() + { + // Setup + var random = new Random(42); + const int arraySize = 10; + var array1 = new byte[arraySize]; + random.NextBytes(array1); + + // Precondition + CollectionAssert.AreEqual(array1, array1); + + // Call + bool areCollectionEqual = BinaryDataEqualityHelper.AreEqual(array1, array1); + + // Assert + Assert.IsTrue(areCollectionEqual); + } + + [Test] + public void GivenNotEqualData_WhenComparingPerformance_ThenPerformanceShouldBeBetterThenLinq() + { + // Given + var random = new Random(42); + const int arraySize = 100000000; + var array1 = new byte[arraySize]; + var array2 = new byte[arraySize]; + random.NextBytes(array1); + random.NextBytes(array2); + + // Precondition + CollectionAssert.AreNotEqual(array1, array2); + + // When + var stopwatch = new Stopwatch(); + long timeToBeat = 0, actualTime = 0; + for (int i = 0; i < 100000; i++) + { + stopwatch.Start(); + Assert.IsFalse(SlowBaselineLinqEqualty(array1, array2)); + stopwatch.Stop(); + timeToBeat += stopwatch.ElapsedTicks; + + stopwatch.Reset(); + stopwatch.Start(); + Assert.IsFalse(BinaryDataEqualityHelper.AreEqual(array1, array2)); + stopwatch.Stop(); + actualTime += stopwatch.ElapsedTicks; + } + + // Then + Assert.Less(actualTime, timeToBeat); // If you want to see the 'timings', just change it to Assert.Greater + } + + private static bool SlowBaselineLinqEqualty(byte[] array1, byte[] array2) + { + return array1.SequenceEqual(array2); + } + } +} \ No newline at end of file Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Update/Piping/PipingFailureMechanismUpdateExtensionsTest.cs =================================================================== diff -u -ra5714fc0488030773fff50dfc82041c6cb9edc2f -rdd460a16f3fed1c1fc5abc2842ea57015f6f4a5c --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Update/Piping/PipingFailureMechanismUpdateExtensionsTest.cs (.../PipingFailureMechanismUpdateExtensionsTest.cs) (revision a5714fc0488030773fff50dfc82041c6cb9edc2f) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Update/Piping/PipingFailureMechanismUpdateExtensionsTest.cs (.../PipingFailureMechanismUpdateExtensionsTest.cs) (revision dd460a16f3fed1c1fc5abc2842ea57015f6f4a5c) @@ -22,6 +22,7 @@ using System; using System.Linq; +using Application.Ringtoets.Storage.BinaryConverters; using Application.Ringtoets.Storage.Create; using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.Exceptions; @@ -241,7 +242,6 @@ // Setup MockRepository mocks = new MockRepository(); var ringtoetsEntities = RingtoetsEntitiesHelper.CreateStub(mocks); - mocks.ReplayAll(); var stochasticSoilModel = new StochasticSoilModel(-1, string.Empty, string.Empty) @@ -267,7 +267,8 @@ var stochasticSoilModelEntity = new StochasticSoilModelEntity { - StochasticSoilModelEntityId = stochasticSoilModel.StorageId + StochasticSoilModelEntityId = stochasticSoilModel.StorageId, + SegmentPoints = new Point2DBinaryConverter().ToBytes(new Point2D[0]) }; var rootCalculationGroupEntity = new CalculationGroupEntity { Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Update/Piping/StochasticSoilModelUpdateExtensionsTest.cs =================================================================== diff -u -r23766b602d9b509a2ffc030f284e8d5e82325a3e -rdd460a16f3fed1c1fc5abc2842ea57015f6f4a5c --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Update/Piping/StochasticSoilModelUpdateExtensionsTest.cs (.../StochasticSoilModelUpdateExtensionsTest.cs) (revision 23766b602d9b509a2ffc030f284e8d5e82325a3e) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Update/Piping/StochasticSoilModelUpdateExtensionsTest.cs (.../StochasticSoilModelUpdateExtensionsTest.cs) (revision dd460a16f3fed1c1fc5abc2842ea57015f6f4a5c) @@ -152,7 +152,8 @@ { StochasticSoilModelEntityId = 1, Name = string.Empty, - SegmentName = string.Empty + SegmentName = string.Empty, + SegmentPoints = new Point2DBinaryConverter().ToBytes(new Point2D[0]) }; ringtoetsEntities.StochasticSoilModelEntities.Add(modelEntity); @@ -190,7 +191,8 @@ var soilModelEntity = new StochasticSoilModelEntity { - StochasticSoilModelEntityId = 1 + StochasticSoilModelEntityId = 1, + SegmentPoints = new Point2DBinaryConverter().ToBytes(new Point2D[0]) }; ringtoetsEntities.StochasticSoilModelEntities.Add(soilModelEntity); @@ -236,7 +238,8 @@ StochasticSoilProfileEntities = { soilProfileEntity - } + }, + SegmentPoints = new Point2DBinaryConverter().ToBytes(new Point2D[0]) }; ringtoetsEntities.StochasticSoilModelEntities.Add(soilModelEntity);