microsoft/Microsoft.IO.RecyclableMemoryStream

Reduce the number of Method/Constructor overloads.

grbell-ms opened this issue · 2 comments

  • Remove obsolete overloads (e.g GetStream(Memory<byte> buffer) et al.)
  • Remove redundant overloads (e.g. no need for an int overload when there's an equivalent long overload)
  • Consider creating a MemoryStreamManagerOptions struct to simplify creation and DI integration.

API proposal:

    /// <summary>
    /// Provides the ability for the user to define custom behavior when using RecyclableMemoryStream.
    /// </summary>
    public struct MemoryStreamManagerOptions
    {
        /// <summary>
        /// Size of each block that is pooled. Must be > 0.
        /// </summary>
        /// <remarks>
        /// By default, it's set to 131072.
        /// </remarks>
        public int BlockSize { get; set; }

        /// <summary>
        /// Each large buffer will be a multiple/exponential of this value.
        /// </summary>
        /// <remarks>
        /// By default, it's set to 1048576.
        /// </remarks>
        public int LargeBufferMultiple { get; set; }

        /// <summary>
        /// Buffers larger than this are not pooled.
        /// </summary>
        /// <remarks>
        /// By default, it's set to 134217728.
        /// </remarks>
        public int MaximumBufferSize { get; set; }

        /// <summary>
        /// Switch to exponential large buffer allocation strategy.
        /// </summary>
        /// <remarks>
        /// By default, it's set to false.
        /// </remarks>
        public bool UseExponentialLargeBuffer { get; set; }

        /// <summary>
        /// Maximum number of bytes to keep available in the small pool before future buffers.
        /// </summary>
        /// <remarks>
        /// By default, it's set to 0.
        /// </remarks>
        public long MaximumSmallPoolFreeBytes { get; set; }

        /// <summary>
        /// Maximum number of bytes to keep available in the large pool before future buffers get dropped for garbage collection.
        /// </summary>
        /// <remarks>
        /// By default, it's set to 0.
        /// </remarks>
        public long MaximumLargePoolFreeBytes { get; set; }
    }
    public partial class RecyclableMemoryStreamManager
    {
        /// <summary>
        /// Initializes the memory manager with the default block/buffer specifications and maximum free bytes specifications.
        /// </summary>
        /// <param name="options">Defines the customized behavior of the <see cref="RecyclableMemoryStream"/>.</param>
        public RecyclableMemoryStreamManager(MemoryStreamManagerOptions options)
            : this(options.BlockSize, options.LargeBufferMultiple, options.MaximumBufferSize, options.UseExponentialLargeBuffer, options.MaximumSmallPoolFreeBytes, options.MaximumLargePoolFreeBytes)
        {
        }

and we may consider to obsolete the other constructor overloads

It's been a while since I've been able to work on this, but I'm hoping in the next couple of weeks to try to get some real progress done on this work item, and anything else slated for 3.0. I haven't forgotten...