diff options
author | csharptest <roger@csharptest.net> | 2011-06-08 17:40:51 -0500 |
---|---|---|
committer | rogerk <devnull@localhost> | 2011-06-08 17:40:51 -0500 |
commit | 4ba365d7932ca661640a402c93d2dfb2816ffd62 (patch) | |
tree | 2dec30345745badd00ebad92a83227237c4c0d47 /src | |
parent | 2772dfe8a1eae7c942bb50d84bda3f45b5d7b683 (diff) | |
download | protobuf-4ba365d7932ca661640a402c93d2dfb2816ffd62.tar.gz protobuf-4ba365d7932ca661640a402c93d2dfb2816ffd62.tar.bz2 protobuf-4ba365d7932ca661640a402c93d2dfb2816ffd62.zip |
Performance fix for double/float on read
Diffstat (limited to 'src')
-rw-r--r-- | src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs | 101 | ||||
-rw-r--r-- | src/ProtocolBuffers.Test/CompatTests/TextCompatibilityTests.cs | 23 | ||||
-rw-r--r-- | src/ProtocolBuffers/CodedInputStream.cs | 35 |
3 files changed, 25 insertions, 134 deletions
diff --git a/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs index e74f2b08..5ce069be 100644 --- a/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs +++ b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs @@ -15,107 +15,6 @@ namespace Google.ProtocolBuffers.CompatTests where TMessage : IMessageLite<TMessage, TBuilder>
where TBuilder : IBuilderLite<TMessage, TBuilder>;
- #region RunBenchmark
-
- protected void RunBenchmark<TMessage, TBuilder>(byte[] buffer, bool write)
- where TMessage : IMessageLite<TMessage, TBuilder>
- where TBuilder : IBuilderLite<TMessage, TBuilder>, new()
- {
- TBuilder builder = new TBuilder();
- TMessage message = new TBuilder().MergeFrom(buffer).Build();
- System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
- //simple warm-up
- object content = SerializeMessage<TMessage, TBuilder>(message);
- Assert.AreEqual(message, DeerializeMessage<TMessage, TBuilder>(content, new TBuilder(), ExtensionRegistry.Empty).Build());
- //timming
- long time = 0, sample = 1;
- while (time < 100)
- {
- sample *= 10;
- watch.Reset();
- watch.Start();
- if (write)
- {
- for (int i = 0; i < sample; i++)
- SerializeMessage<TMessage, TBuilder>(message);
- }
- else
- {
- for (int i = 0; i < sample; i++)
- DeerializeMessage<TMessage, TBuilder>(content, builder, ExtensionRegistry.Empty);
- }
- watch.Stop();
- time = watch.ElapsedMilliseconds;
- }
-
- ulong rounds = (ulong)((100.0 / watch.ElapsedMilliseconds) * sample);
- //test
- watch.Reset();
- watch.Start();
-
- if (write)
- {
- for (ulong i = 0; i < rounds; i++)
- SerializeMessage<TMessage, TBuilder>(message);
- }
- else
- {
- for (ulong i = 0; i < rounds; i++)
- DeerializeMessage<TMessage, TBuilder>(content, builder, ExtensionRegistry.Empty);
- }
-
- watch.Stop();
- System.Diagnostics.Trace.TraceInformation(
- "\r\n{0} {4} {5} {3:n0} rps ({1:n0} rounds in {2:n0} ms)", typeof(TMessage).Name, rounds,
- watch.ElapsedMilliseconds, (1000.0 / watch.ElapsedMilliseconds) * (double)rounds, TestName, write ? " write" : " read");
- GC.GetTotalMemory(true);
- GC.WaitForPendingFinalizers();
- }
-
- [Test]
- public virtual void Message1OptimizeSizeWriterPerf()
- {
- RunBenchmark<SizeMessage1, SizeMessage1.Builder>(TestResources.google_message1, true);
- }
- [Test]
- public virtual void Message1OptimizeSpeedWriterPerf()
- {
- RunBenchmark<SpeedMessage1, SpeedMessage1.Builder>(TestResources.google_message1, true);
- }
- [Test]
- public virtual void Message2OptimizeSizeWriterPerf()
- {
- RunBenchmark<SizeMessage2, SizeMessage2.Builder>(TestResources.google_message2, true);
- }
- [Test]
- public virtual void Message2OptimizeSpeedWriterPerf()
- {
- RunBenchmark<SpeedMessage2, SpeedMessage2.Builder>(TestResources.google_message2, true);
- }
-
- [Test]
- public virtual void Message1OptimizeSizeReadPerf()
- {
- RunBenchmark<SizeMessage1, SizeMessage1.Builder>(TestResources.google_message1, false);
- }
- [Test]
- public virtual void Message1OptimizeSpeedReadPerf()
- {
- RunBenchmark<SpeedMessage1, SpeedMessage1.Builder>(TestResources.google_message1, false);
- }
- [Test]
- public virtual void Message2OptimizeSizeReadPerf()
- {
- RunBenchmark<SizeMessage2, SizeMessage2.Builder>(TestResources.google_message2, false);
- }
- [Test]
- public virtual void Message2OptimizeSpeedReadPerf()
- {
- RunBenchmark<SpeedMessage2, SpeedMessage2.Builder>(TestResources.google_message2, false);
- }
-
- #endregion
-
[Test]
public virtual void RoundTripMessage1OptimizeSize()
{
diff --git a/src/ProtocolBuffers.Test/CompatTests/TextCompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/TextCompatibilityTests.cs index 5eba0d33..83f19ae3 100644 --- a/src/ProtocolBuffers.Test/CompatTests/TextCompatibilityTests.cs +++ b/src/ProtocolBuffers.Test/CompatTests/TextCompatibilityTests.cs @@ -23,17 +23,6 @@ namespace Google.ProtocolBuffers.CompatTests }
[Test, Explicit, Description("This test can take a very long time to run.")]
- public override void Message2OptimizeSizeReadPerf()
- {
- base.Message2OptimizeSizeReadPerf();
- }
- [Test, Explicit, Description("This test can take a very long time to run.")]
- public override void Message2OptimizeSpeedReadPerf()
- {
- base.Message2OptimizeSpeedReadPerf();
- }
-
- [Test, Explicit, Description("This test can take a very long time to run.")]
public override void RoundTripMessage2OptimizeSize()
{
base.RoundTripMessage2OptimizeSize();
@@ -44,17 +33,5 @@ namespace Google.ProtocolBuffers.CompatTests {
base.RoundTripMessage2OptimizeSpeed();
}
-
- [Test, Explicit, Description("This test can take a very long time to run.")]
- public override void Message2OptimizeSizeWriterPerf()
- {
- base.Message2OptimizeSizeWriterPerf();
- }
- [Test, Explicit, Description("This test can take a very long time to run.")]
- public override void Message2OptimizeSpeedWriterPerf()
- {
- base.Message2OptimizeSpeedWriterPerf();
- }
-
}
}
\ No newline at end of file diff --git a/src/ProtocolBuffers/CodedInputStream.cs b/src/ProtocolBuffers/CodedInputStream.cs index 4f9bdc23..105dc378 100644 --- a/src/ProtocolBuffers/CodedInputStream.cs +++ b/src/ProtocolBuffers/CodedInputStream.cs @@ -204,15 +204,22 @@ namespace Google.ProtocolBuffers public bool ReadDouble(ref double value)
{
#if SILVERLIGHT2 || COMPACT_FRAMEWORK_35
- byte[] rawBytes = ReadRawBytes(8);
- if (!BitConverter.IsLittleEndian)
- Array.Reverse(rawBytes);
- value = BitConverter.ToDouble(rawBytes, 0);
- return true;
+ if (BitConverter.IsLittleEndian && 8 <= bufferSize - bufferPos)
+ {
+ value = BitConverter.ToDouble(buffer, bufferPos);
+ bufferPos += 8;
+ }
+ else
+ {
+ byte[] rawBytes = ReadRawBytes(8);
+ if (!BitConverter.IsLittleEndian)
+ Array.Reverse(rawBytes);
+ value = BitConverter.ToDouble(rawBytes, 0);
+ }
#else
value = BitConverter.Int64BitsToDouble((long) ReadRawLittleEndian64());
- return true;
#endif
+ return true;
}
/// <summary>
@@ -220,10 +227,18 @@ namespace Google.ProtocolBuffers /// </summary>
public bool ReadFloat(ref float value)
{
- byte[] rawBytes = ReadRawBytes(4);
- if (!BitConverter.IsLittleEndian)
- Array.Reverse(rawBytes);
- value = BitConverter.ToSingle(rawBytes, 0);
+ if (BitConverter.IsLittleEndian && 4 <= bufferSize - bufferPos)
+ {
+ value = BitConverter.ToSingle(buffer, bufferPos);
+ bufferPos += 4;
+ }
+ else
+ {
+ byte[] rawBytes = ReadRawBytes(4);
+ if (!BitConverter.IsLittleEndian)
+ Array.Reverse(rawBytes);
+ value = BitConverter.ToSingle(rawBytes, 0);
+ }
return true;
}
|