< Summary

Line coverage
96%
Covered lines: 25
Uncovered lines: 1
Coverable lines: 26
Total lines: 59
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)

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)
 4047021        {
 4047022            if (capacity <= MaxBuilderSize)
 4047023            {
 4047024                StringBuilder? sb = t_cachedInstance;
 4047025                if (sb != null)
 4016326                {
 27                    // Avoid stringbuilder block fragmentation by getting a new StringBuilder
 28                    // when the requested size is larger than the current capacity
 4016329                    if (capacity <= sb.Capacity)
 4016330                    {
 4016331                        t_cachedInstance = null;
 4016332                        sb.Clear();
 4016333                        return sb;
 34                    }
 035                }
 30736            }
 37
 30738            return new StringBuilder(capacity);
 4047039        }
 40
 41        /// <summary>Place the specified builder in the cache if it is not too big.</summary>
 42        public static void Release(StringBuilder sb)
 4047043        {
 4047044            if (sb.Capacity <= MaxBuilderSize)
 4016445            {
 4016446                t_cachedInstance = sb;
 4016447            }
 4047048        }
 49
 50        /// <summary>ToString() the stringbuilder, Release it to the cache, and return the resulting string.</summary>
 51        public static string GetStringAndRelease(StringBuilder sb)
 4047052        {
 4047053            string result = sb.ToString();
 4047054            Release(sb);
 4047055            return result;
 4047056        }
 57    }
 58}
 59