From c2bb07879128ee1df0fb0abcc52bdd00baf44c59 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sat, 14 Feb 2015 13:35:24 +0100 Subject: [PATCH 1/6] Make the rot13 test filter stream its output --- .../TestHelpers/SubstitutionCipherFilter.cs | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs b/LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs index 25a90be4c..b4749ca6c 100644 --- a/LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs +++ b/LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs @@ -35,23 +35,22 @@ protected override int Smudge(string path, Stream input, Stream output) public static int RotateByThirteenPlaces(Stream input, Stream output) { - using (var streamReader = new StreamReader(input, Encoding.UTF8)) + using (var streamWriter = new StreamWriter(output, Encoding.UTF8)) { - var inputString = streamReader.ReadToEnd(); - char[] array = inputString.ToCharArray(); - for (int i = 0; i < inputString.Length; i++) + while (!streamReader.EndOfStream) { - var value = inputString[i]; + var value = streamReader.Read(); if ((value >= 'a' && value <= 'm') || (value >= 'A' && value <= 'M')) - array[i] = (char)(value + 13); + { + value += 13; + } else if ((value >= 'n' && value <= 'z') || (value >= 'N' && value <= 'Z')) - array[i] = (char)(value - 13); - } + { + value -= 13; + } - using (var streamWriter = new StreamWriter(output, Encoding.UTF8)) - { - streamWriter.Write(array); + streamWriter.Write((char)value); } return 0; From d9ae441f373aa6faed1228a4bbaba55dcae097ba Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sat, 14 Feb 2015 17:24:17 +0100 Subject: [PATCH 2/6] Keep Filter tests alpha sorted --- ...ipherFilterFixture.cs => FilterSubstitutionCipherFixture.cs} | 2 +- LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename LibGit2Sharp.Tests/{SubstitutionCipherFilterFixture.cs => FilterSubstitutionCipherFixture.cs} (98%) diff --git a/LibGit2Sharp.Tests/SubstitutionCipherFilterFixture.cs b/LibGit2Sharp.Tests/FilterSubstitutionCipherFixture.cs similarity index 98% rename from LibGit2Sharp.Tests/SubstitutionCipherFilterFixture.cs rename to LibGit2Sharp.Tests/FilterSubstitutionCipherFixture.cs index 39ccf1f5b..f2fc14b66 100644 --- a/LibGit2Sharp.Tests/SubstitutionCipherFilterFixture.cs +++ b/LibGit2Sharp.Tests/FilterSubstitutionCipherFixture.cs @@ -6,7 +6,7 @@ namespace LibGit2Sharp.Tests { - public class SubstitutionCipherFilterFixture : BaseFixture + public class FilterSubstitutionCipherFixture : BaseFixture { [Fact] public void CorrectlyEncodesAndDecodesInput() diff --git a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj index 6f5f64e63..21bf7faba 100644 --- a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj +++ b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj @@ -96,7 +96,7 @@ - + From 5635527ed1acbd91ed191b2f9bf69165073fe40e Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sat, 14 Feb 2015 19:55:34 +0100 Subject: [PATCH 3/6] More barebone rot13 filter implementation --- .../TestHelpers/SubstitutionCipherFilter.cs | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs b/LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs index b4749ca6c..184ca526e 100644 --- a/LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs +++ b/LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.IO; -using System.Text; namespace LibGit2Sharp.Tests.TestHelpers { @@ -35,26 +34,23 @@ protected override int Smudge(string path, Stream input, Stream output) public static int RotateByThirteenPlaces(Stream input, Stream output) { - using (var streamReader = new StreamReader(input, Encoding.UTF8)) - using (var streamWriter = new StreamWriter(output, Encoding.UTF8)) + int value; + + while ((value = input.ReadByte()) != -1) { - while (!streamReader.EndOfStream) + if ((value >= 'a' && value <= 'm') || (value >= 'A' && value <= 'M')) + { + value += 13; + } + else if ((value >= 'n' && value <= 'z') || (value >= 'N' && value <= 'Z')) { - var value = streamReader.Read(); - if ((value >= 'a' && value <= 'm') || (value >= 'A' && value <= 'M')) - { - value += 13; - } - else if ((value >= 'n' && value <= 'z') || (value >= 'N' && value <= 'Z')) - { - value -= 13; - } - - streamWriter.Write((char)value); + value -= 13; } - return 0; + output.WriteByte((byte)value); } + + return 0; } } } From 291d41944397e95f508fe958a24622787726a473 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sat, 14 Feb 2015 19:56:52 +0100 Subject: [PATCH 4/6] Make filter leverage a buffered stream This will flush out chunks of 4kB to the underlying writer --- LibGit2Sharp/Filter.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/LibGit2Sharp/Filter.cs b/LibGit2Sharp/Filter.cs index 596b8feb0..5697d7d6c 100644 --- a/LibGit2Sharp/Filter.cs +++ b/LibGit2Sharp/Filter.cs @@ -237,10 +237,11 @@ int ApplyCallback(GitFilter gitFilter, IntPtr payload, var filterSource = FilterSource.FromNativePtr(filterSourcePtr); using (var reader = new GitBufReadStream(gitBufferFromPtr)) using (var writer = new GitBufWriteStream(gitBufferToPtr)) + using (var bufferedWriter = new BufferedStream(writer)) { return filterSource.SourceMode == FilterMode.Clean ? - Clean(filterSource.Path, reader, writer) : - Smudge(filterSource.Path, reader, writer); + Clean(filterSource.Path, reader, bufferedWriter) : + Smudge(filterSource.Path, reader, bufferedWriter); } } } From 9bfcf1e63265cadc8ee8ee8086066c6e21fa8375 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sun, 15 Feb 2015 18:40:40 +0100 Subject: [PATCH 5/6] Preallocate 4kB --- LibGit2Sharp/Core/GitBufWriteStream.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LibGit2Sharp/Core/GitBufWriteStream.cs b/LibGit2Sharp/Core/GitBufWriteStream.cs index f727fec1e..44980406b 100644 --- a/LibGit2Sharp/Core/GitBufWriteStream.cs +++ b/LibGit2Sharp/Core/GitBufWriteStream.cs @@ -13,7 +13,7 @@ internal GitBufWriteStream(IntPtr gitBufPointer) this.gitBufPointer = gitBufPointer; //Preallocate the buffer - Proxy.git_buf_grow(gitBufPointer, 1024); + Proxy.git_buf_grow(gitBufPointer, 4096); } public override void Flush() From 46a703450c52ad2208b33903cba93e6b8c99ef4a Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sun, 15 Feb 2015 18:42:21 +0100 Subject: [PATCH 6/6] Drop redundant autogrowing --- LibGit2Sharp/Core/GitBufWriteStream.cs | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/LibGit2Sharp/Core/GitBufWriteStream.cs b/LibGit2Sharp/Core/GitBufWriteStream.cs index 44980406b..7cc281f22 100644 --- a/LibGit2Sharp/Core/GitBufWriteStream.cs +++ b/LibGit2Sharp/Core/GitBufWriteStream.cs @@ -37,32 +37,9 @@ public override int Read(byte[] buffer, int offset, int count) public override void Write(byte[] buffer, int offset, int count) { - AutoGrowBuffer(count); - Proxy.git_buf_put(gitBufPointer, buffer, offset, count); } - private void AutoGrowBuffer(int count) - { - var gitBuf = gitBufPointer.MarshalAs(); - - var ulongCount = Convert.ToUInt64(count); - var asize = (ulong)gitBuf.asize; - var size = (ulong)gitBuf.size; - - var isBufferLargeEnoughToHoldTheNewData = (asize - size) > ulongCount; - var filledBufferPercentage = (100.0 * size / asize); - - if (isBufferLargeEnoughToHoldTheNewData && filledBufferPercentage < 90) - { - return; - } - - var targetSize = (ulong)(1.5 * (asize + ulongCount)); - - Proxy.git_buf_grow(gitBufPointer, targetSize); - } - public override bool CanRead { get { return false; }