-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce memory allocationt. #2109
Open
xljiulang
wants to merge
54
commits into
dotnet:master
Choose a base branch
from
xljiulang:buffer-reader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 38 commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
a793a3a
Reduce memory allocation of DecodePublishPacket.
xljiulang 6c7e0e5
Reduce memory allocation of MqttBufferReader.
xljiulang e7f4248
Change the type of the Packet field from ArraySegment<byte> to ReadOn…
xljiulang e578f0c
MqttBufferWriter.WriteBinary(byte[]) parameter type changed to ReadOn…
xljiulang 9fa3337
Change the type of PayloadSegment to ReadOnlyMemory<byte>
xljiulang 7edac8f
Change the type of PayloadSegment to ReadOnlyMemory<byte>
xljiulang 0a31a61
Optimizing MqttBufferReader
xljiulang 8b89a54
More byte[] parameters or properties are changed to ReadonlyMemory<byte>
xljiulang b26b4eb
MqttPacketInspector.FillReceiveBuffer: ReadOnlyMemory->ReadOnlySpan
xljiulang b1a1865
Add high performance payload to json deserialization extension
xljiulang 53242ee
MqttBufferWriter: Rename WriteBinary(byte[],int,int) to Write(Readonl…
xljiulang 42076f4
Using BinaryPrimitives to write length values.
xljiulang 8f76c02
MqttBufferWriter: Use Span<byte> GetSpan(int) to get the buffer to wr…
xljiulang 07772a1
MqttConnectPacket: Restore Password to byte[]? type.
xljiulang e2ee3f8
MqttConnectPacket: Restore Password to byte[]? type.
xljiulang f1593ff
MqttClientExtensions: Using ArrayPool makes PublishStringAsync reach …
xljiulang 8f69184
Add 0 allocated MqttPayloadOwnerFactory to build mqtt Payload.
xljiulang b992a4f
MqttPayloadOwnerFactory: Improve implementation and add unit tests.
xljiulang 7a61bc2
MqttServerExtensions: Add high-performance Inject related extension m…
xljiulang 94f0f9e
Add support for payloadFactory extension methods
xljiulang 41331c5
MqttPayloadOwnerFactory: CancellationToken support.
xljiulang f2367aa
Add PublishStreamAsync() and InjectStreamAsync()
xljiulang eb334f0
Improve ConvertPayloadToJson
xljiulang 180b31b
Add CreateJsonReaderOptions
xljiulang 58390e8
Reduce the use of unnecessary MqttApplicationMessageBuilder
xljiulang 6664f15
Update Client_Publish_Samples.
xljiulang d1792a5
SingleSegmentPayloadOwner: use payloadFactory
xljiulang 0ed0b48
Rpc.ExecuteAsync: Reduce memory allocation.
xljiulang c7ab618
Add ExecuteTimeOutAsync() extendsion methods.
xljiulang 5a98f70
Merge branch 'dotnet:master' into buffer-reader
xljiulang a3ac26b
MqttRetainedMessageModel: copy the payload.
xljiulang 0a25ad6
Merge branch 'main' into buffer-reader
xljiulang 5b2a995
MqttBufferWriter: Add GetWrittenArraySegment method.
xljiulang 6394733
Add the BufferSize property, which is used for the pool strategy.
xljiulang ee6b950
Do not use the PayloadSegment field in anticipation of its removal in…
xljiulang 7ef0a2c
Merge branch 'buffer-reader' of https://github.com/xljiulang/MQTTnet …
xljiulang 4025d18
ValidatingConnectionEventArgs: Restore RawPasswordto byte[]? type.
xljiulang 8aea746
Some PublishAsync and InjectAsync methods remove await and return dir…
xljiulang c661764
Merge branch 'main' into buffer-reader
xljiulang a8b6fe9
ConfigureAwait(false)!
xljiulang 22aae2d
MqttChannelAdapter: Use ArrayPool to reduce memory allocation.
xljiulang 20d7866
Fixed the buffer size issue of PacketInspector.
xljiulang c7a16a9
Add more ExecuteTimeoutAsync overload methods;
xljiulang 5ae8522
Update doc and unit test.
xljiulang d2c36ee
Enhance unit testing of MqttPayloadOwnerFactory.
xljiulang 8df3a96
Update and fix MessageProcessingBenchmark.
xljiulang f2a81ea
MqttPacketInspector: add FillReceiveBuffer(ReadOnlySequence<byte>) me…
xljiulang 759a816
Use Pipe to reduce the memory allocation of MqttPacketInspector.
xljiulang 52daba1
InspectPacketAsync() is performed only in the Fill state.
xljiulang 83138b9
Fixed the issue of writing 0-byte Binary.
xljiulang f82344a
MqttBufferWriter: Add WriteFourByteInteger(uint) method.
xljiulang cc09847
MqttBufferWriter: Added IBufferWriter<byte> AsLowLevelBufferWriter() …
xljiulang d0e1ce4
Fixed duplicate write of (MqttPropertyId id, ushort value).
xljiulang 057365d
Merge branch 'dotnet:master' into buffer-reader
xljiulang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
using MQTTnet.Internal; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace MQTTnet.Benchmarks | ||
{ | ||
[SimpleJob(RuntimeMoniker.Net80)] | ||
[MemoryDiagnoser] | ||
public class JsonPayloadBenchmark : BaseBenchmark | ||
{ | ||
[Params(1 * 1024, 4 * 1024, 8 * 1024)] | ||
public int PayloadSize { get; set; } | ||
private Model model; | ||
|
||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
var stringValue = new char[PayloadSize]; | ||
model = new Model { StringValue = new string(stringValue) }; | ||
} | ||
|
||
[Benchmark] | ||
public ValueTask SerializeToString_Payload_SendAsync() | ||
{ | ||
string payload = JsonSerializer.Serialize(model); | ||
var message = new MqttApplicationMessageBuilder() | ||
.WithTopic("t") | ||
.WithPayload(payload) | ||
.Build(); | ||
|
||
// send message async | ||
return ValueTask.CompletedTask; | ||
} | ||
|
||
[Benchmark] | ||
public ValueTask SerializeToUtf8Bytes_Payload_SendAsync() | ||
{ | ||
byte[] payload = JsonSerializer.SerializeToUtf8Bytes(model); | ||
var message = new MqttApplicationMessageBuilder() | ||
.WithTopic("t") | ||
.WithPayload(payload) | ||
.Build(); | ||
|
||
// send message async | ||
return ValueTask.CompletedTask; | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public async ValueTask MqttPayloadOwnerFactory_Payload_SendAsync() | ||
{ | ||
await using var payloadOwner = await MqttPayloadOwnerFactory.CreateMultipleSegmentAsync(async writer => | ||
await JsonSerializer.SerializeAsync(writer.AsStream(leaveOpen: true), model)); | ||
|
||
var message = new MqttApplicationMessageBuilder() | ||
.WithTopic("t") | ||
.WithPayload(payloadOwner.Payload) | ||
.Build(); | ||
|
||
// send message async | ||
} | ||
|
||
|
||
|
||
public class Model | ||
{ | ||
public string StringValue { get; set; } | ||
|
||
public int IntValue { get; set; } | ||
|
||
public bool BoolValue { get; set; } | ||
|
||
public double DoubleValue { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try this on .NET 9, if you commented it out because of the 100s of warning messages
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's 100s of error messages! We can unify this issue in #2120.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see https://developercommunity.visualstudio.com/t/Code-Rules-that-were-Informational-are-n/10789409#TPIN-N10795393