• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / SchemaSaurus / 27082453498

07 Jun 2026 04:17AM UTC coverage: 86.88% (-0.03%) from 86.906%
27082453498

push

github

pwelter34
Delete AddSpatialDataScript.cs

1064 of 1430 branches covered (74.41%)

Branch coverage included in aggregate %.

5737 of 6398 relevant lines covered (89.67%)

227.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.17
/src/SchemaSaurus.Metadata/Internal/StringBuilderCache.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4

5
namespace SchemaSaurus.Metadata.Internal;
6

7
/// <summary>
8
/// Provides a cached, reusable instance of <see cref="StringBuilder"/> per thread to reduce memory allocations.
9
/// </summary>
10
/// <remarks>
11
/// This class is intended to optimize performance by reusing <see cref="StringBuilder"/> instances for short-lived operations.
12
/// The cache is thread-static, so each thread has its own cached instance.
13
/// </remarks>
14
public static class StringBuilderCache
15
{
16
    /// <summary>
17
    /// The maximum size, in characters, for a <see cref="StringBuilder"/> instance to be cached.
18
    /// </summary>
19
    /// <remarks>
20
    /// The value 360 was chosen as a compromise between minimizing memory usage per thread and covering
21
    /// a large portion of short-lived <see cref="StringBuilder"/> creations, especially during application startup.
22
    /// </remarks>
23
    internal const int MaxBuilderSize = 360;
24

25
    private const int DefaultCapacity = 16; // == StringBuilder.DefaultCapacity
26

27
    [ThreadStatic]
28
    private static StringBuilder? t_cachedInstance;
29

30
    /// <summary>
31
    /// Retrieves a <see cref="StringBuilder"/> instance with the specified capacity.
32
    /// </summary>
33
    /// <param name="capacity">
34
    /// The minimum capacity of the returned <see cref="StringBuilder"/>. Defaults to 16 if not specified.
35
    /// </param>
36
    /// <returns>
37
    /// A <see cref="StringBuilder"/> instance with at least the specified capacity. If a suitable cached instance is available,
38
    /// it is returned; otherwise, a new instance is created.
39
    /// </returns>
40
    /// <remarks>
41
    /// If the requested capacity exceeds <see cref="MaxBuilderSize"/>, a new <see cref="StringBuilder"/> is always created.
42
    /// </remarks>
43
    public static StringBuilder Acquire(int capacity = DefaultCapacity)
44
    {
45
        if (capacity > MaxBuilderSize)
6!
46
            return new StringBuilder(capacity);
×
47

48
        var sb = t_cachedInstance;
6✔
49
        if (sb == null)
6✔
50
            return new StringBuilder(capacity);
2✔
51

52
        // Avoid StringBuilder block fragmentation by getting a new StringBuilder
53
        // when the requested size is larger than the current capacity
54
        if (capacity > sb.Capacity)
4!
55
            return new StringBuilder(capacity);
×
56

57
        t_cachedInstance = null;
4✔
58
        sb.Clear();
4✔
59

60
        return sb;
4✔
61
    }
62

63
    /// <summary>
64
    /// Places the specified <see cref="StringBuilder"/> instance in the cache if its capacity does not exceed <see cref="MaxBuilderSize"/>.
65
    /// </summary>
66
    /// <param name="sb">The <see cref="StringBuilder"/> instance to cache.</param>
67
    public static void Release(StringBuilder sb)
68
    {
69
        if (sb.Capacity <= MaxBuilderSize)
6!
70
            t_cachedInstance = sb;
6✔
71
    }
6✔
72

73
    /// <summary>
74
    /// Converts the specified <see cref="StringBuilder"/> to a string and releases it to the cache.
75
    /// </summary>
76
    /// <param name="sb">The <see cref="StringBuilder"/> instance to convert and release.</param>
77
    /// <returns>The string representation of the <see cref="StringBuilder"/> content.</returns>
78
    public static string ToString(StringBuilder sb)
79
    {
80
        var result = sb.ToString();
6✔
81
        Release(sb);
6✔
82
        return result;
6✔
83
    }
84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc