You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/11/18 22:07:24 UTC
[GitHub] [pulsar-dotpulsar] sunero4 opened a new pull request, #128: Reduce allocations by not copying ReadOnlySequence
sunero4 opened a new pull request, #128:
URL: https://github.com/apache/pulsar-dotpulsar/pull/128
# Description
I recently started working at Danske Commodities where I had to use this library, and decided to peek around the source code a bit to see what it does. It seems to me that a significant amount of allocations could be avoided by not copying the slice to a new array in `Connection.cs` line 311, as the slice itself is already a `ReadOnlySequence<byte>`. I ran some simple benchmarks of performing such a copy (benchmark code and results is found below) that verifies that this indeed results in allocating the equivalent of the slice once more.
Besides changing this, I looked through the project and found a few places where the `static` modifier could be applied to reduce allocations for lambdas, but there were very few of these, probably because LINQ seems to be used very sparsely in the project.
# Benchmark results
```
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|----------------------------------- |----------:|----------:|----------:|-------:|----------:|
| ReturnSliceDirectly (Input = 256 B) | 8.699 ns | 0.1168 ns | 0.1093 ns | - | - |
| ReturnNewReadOnlySequenceFromArray (Input = 256 B) | 38.693 ns | 0.7455 ns | 0.7322 ns | 0.0485 | 152 B |
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|----------------------------------- |-----------:|----------:|----------:|-------:|----------:|
| ReturnSliceDirectly (Input = 2048 B) | 8.716 ns | 0.1092 ns | 0.1021 ns | - | - |
| ReturnNewReadOnlySequenceFromArray (Input = 2048 B) | 174.669 ns | 3.3067 ns | 3.3957 ns | 0.6194 | 1944 B |
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|----------------------------------- |-------------:|-----------:|-----------:|--------:|----------:|
| ReturnSliceDirectly (Input = 65536 B) | 8.748 ns | 0.1240 ns | 0.1160 ns | - | - |
| ReturnNewReadOnlySequenceFromArray (Input = 65536 B) | 4,809.186 ns | 78.2922 ns | 73.2346 ns | 20.4010 | 65432 B |
```
# Benchmark code
```
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Buffers;
[MemoryDiagnoser]
public class Program
{
private static readonly ReadOnlySequence<byte> Input = new ReadOnlySequence<byte>(new byte[65536]);
public static void Main()
{
BenchmarkRunner.Run<Program>();
Console.ReadLine();
}
[Benchmark]
public ReadOnlySequence<byte> ReturnSliceDirectly()
{
return Input.Slice(128);
}
[Benchmark]
public ReadOnlySequence<byte> ReturnNewReadOnlySequenceFromArray()
{
return new ReadOnlySequence<byte>(Input.Slice(128).ToArray());
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar-dotpulsar] sunero4 commented on pull request #128: Reduce allocations by not copying ReadOnlySequence
Posted by GitBox <gi...@apache.org>.
sunero4 commented on PR #128:
URL: https://github.com/apache/pulsar-dotpulsar/pull/128#issuecomment-1320942559
It seems like I was a bit too trigger-happy with the pull request after all, and should have read the [documentation](https://learn.microsoft.com/en-us/dotnet/standard/io/buffers#readonlysequencet) for consuming `ReadOnlySequence` a bit closer :) Apparently `Slice` advances the enumerator of the `ReadOnlySequence`, making it unusable later on, and the correct way of consuming it is in fact to materialize it as an array or using the `SequenceReader<byte>`.
The documentation does however advise against using the `ToArray` method in hot paths, so I might try and play around with using an `ArrayPool` instead. For now I'll decline the PR :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar-dotpulsar] sunero4 closed pull request #128: Reduce allocations by not copying ReadOnlySequence
Posted by GitBox <gi...@apache.org>.
sunero4 closed pull request #128: Reduce allocations by not copying ReadOnlySequence
URL: https://github.com/apache/pulsar-dotpulsar/pull/128
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org