< 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
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Acquire(...)83.33%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)
 82640621        {
 82640622            if (capacity <= MaxBuilderSize)
 82640623            {
 82640624                StringBuilder? sb = t_cachedInstance;
 82640625                if (sb != null)
 82486426                {
 27                    // Avoid stringbuilder block fragmentation by getting a new StringBuilder
 28                    // when the requested size is larger than the current capacity
 82486429                    if (capacity <= sb.Capacity)
 82486430                    {
 82486431                        t_cachedInstance = null;
 82486432                        sb.Clear();
 82486433                        return sb;
 34                    }
 035                }
 154236            }
 37
 154238            return new StringBuilder(capacity);
 82640639        }
 40
 41        /// <summary>Place the specified builder in the cache if it is not too big.</summary>
 42        public static void Release(StringBuilder sb)
 82640643        {
 82640644            if (sb.Capacity <= MaxBuilderSize)
 82486545            {
 82486546                t_cachedInstance = sb;
 82486547            }
 82640648        }
 49
 50        /// <summary>ToString() the stringbuilder, Release it to the cache, and return the resulting string.</summary>
 51        public static string GetStringAndRelease(StringBuilder sb)
 82640652        {
 82640653            string result = sb.ToString();
 82640654            Release(sb);
 82640655            return result;
 82640656        }
 57    }
 58}