< Summary

Information
Class: System.Text.StringBuilderCache
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\Common\src\System\Text\StringBuilderCache.cs
Line coverage
96%
Covered lines: 25
Uncovered lines: 1
Coverable lines: 26
Total lines: 58
Line coverage: 96.1%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Acquire(...)66.66%6693.33%
Release(...)100%22100%
GetStringAndRelease(...)100%11100%

File(s)

D:\runner\runtime\src\libraries\Common\src\System\Text\StringBuilderCache.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4namespace System.Text
 5{
 6    /// <summary>Provide a cached reusable instance of stringbuilder per thread.</summary>
 7    internal static class StringBuilderCache
 8    {
 9        // The value 360 was chosen in discussion with performance experts as a compromise between using
 10        // as little memory per thread as possible and still covering a large part of short-lived
 11        // StringBuilder creations on the startup path of VS designers.
 12        internal const int MaxBuilderSize = 360;
 13        private const int DefaultCapacity = 16; // == StringBuilder.DefaultCapacity
 14
 15        [ThreadStatic]
 16        private static StringBuilder? t_cachedInstance;
 17
 18        /// <summary>Get a StringBuilder for the specified capacity.</summary>
 19        /// <remarks>If a StringBuilder of an appropriate size is cached, it will be returned and the cache emptied.</re
 20        public static StringBuilder Acquire(int capacity = DefaultCapacity)
 2727321        {
 2727322            if (capacity <= MaxBuilderSize)
 2727323            {
 2727324                StringBuilder? sb = t_cachedInstance;
 2727325                if (sb != null)
 2707426                {
 27                    // Avoid stringbuilder block fragmentation by getting a new StringBuilder
 28                    // when the requested size is larger than the current capacity
 2707429                    if (capacity <= sb.Capacity)
 2707430                    {
 2707431                        t_cachedInstance = null;
 2707432                        sb.Clear();
 2707433                        return sb;
 34                    }
 035                }
 19936            }
 37
 19938            return new StringBuilder(capacity);
 2727339        }
 40
 41        /// <summary>Place the specified builder in the cache if it is not too big.</summary>
 42        public static void Release(StringBuilder sb)
 2727343        {
 2727344            if (sb.Capacity <= MaxBuilderSize)
 2707545            {
 2707546                t_cachedInstance = sb;
 2707547            }
 2727348        }
 49
 50        /// <summary>ToString() the stringbuilder, Release it to the cache, and return the resulting string.</summary>
 51        public static string GetStringAndRelease(StringBuilder sb)
 2727352        {
 2727353            string result = sb.ToString();
 2727354            Release(sb);
 2727355            return result;
 2727356        }
 57    }
 58}