< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 14
Coverable lines: 14
Total lines: 59
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Acquire(...)0%660%
Release(...)0%220%
GetStringAndRelease(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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)
 21        {
 022            if (capacity <= MaxBuilderSize)
 23            {
 024                StringBuilder? sb = t_cachedInstance;
 025                if (sb != null)
 26                {
 27                    // Avoid stringbuilder block fragmentation by getting a new StringBuilder
 28                    // when the requested size is larger than the current capacity
 029                    if (capacity <= sb.Capacity)
 30                    {
 031                        t_cachedInstance = null;
 032                        sb.Clear();
 033                        return sb;
 34                    }
 35                }
 36            }
 37
 038            return new StringBuilder(capacity);
 39        }
 40
 41        /// <summary>Place the specified builder in the cache if it is not too big.</summary>
 42        public static void Release(StringBuilder sb)
 43        {
 044            if (sb.Capacity <= MaxBuilderSize)
 45            {
 046                t_cachedInstance = sb;
 47            }
 048        }
 49
 50        /// <summary>ToString() the stringbuilder, Release it to the cache, and return the resulting string.</summary>
 51        public static string GetStringAndRelease(StringBuilder sb)
 52        {
 053            string result = sb.ToString();
 054            Release(sb);
 055            return result;
 56        }
 57    }
 58}
 59