ptupitsyn commented on a change in pull request #306: URL: https://github.com/apache/ignite-3/pull/306#discussion_r703251864
########## File path: modules/platforms/dotnet/Apache.Ignite/Internal/Buffers/PooledArrayBufferWriter.cs ########## @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Internal.Buffers +{ + using System; + using System.Buffers; + using System.Diagnostics; + using System.Net; + using MessagePack; + + /// <summary> + /// Pooled buffer writer: integrates <see cref="MessagePackWriter"/> with <see cref="ArrayPool{T}"/>, + /// and adds the logic to prepend messages with size and other data (opcode, request id). + /// <para /> + /// We reserve some bytes for the prefix because message size, op code and request ID are not known initially. + /// <para /> + /// There are two ways to use <see cref="MessagePackWriter"/>: with a <see cref="SequencePool"/>, + /// or with a <see cref="IBufferWriter{T}"/>. SequencePool approach uses buffer pooling too, but still allocates + /// the final array with <see cref="MessagePackWriter.FlushAndGetArray"/>. We want to avoid all array allocations, + /// so we implement our own <see cref="IBufferWriter{T}"/> here. + /// <para /> + /// Based on <see cref="ArrayBufferWriter{T}"/>, but uses <see cref="ArrayPool{T}.Shared"/> to allocate arrays. + /// <para /> + /// Not a struct because <see cref="GetMessageWriter"/> will cause boxing. + /// </summary> + internal sealed class PooledArrayBufferWriter : IBufferWriter<byte>, IDisposable + { + /** Reserved prefix size. */ + private const int ReservedPrefixSize = 4 + 4 + 9; // Size (4 bytes) + OpCode (4 bytes) + RequestId (9 bytes)/ + + /** Underlying pooled array. */ + private byte[] _buffer; + + /** Index within the array. */ + private int _index; + + /** Index within the array. */ + private int _index2; Review comment: Agree, fixed. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
