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

loresoft / FluentCommand / 29258656563

13 Jul 2026 02:36PM UTC coverage: 65.856% (+0.2%) from 65.698%
29258656563

push

github

pwelter34
Add JsonElement converter support

1872 of 3634 branches covered (51.51%)

Branch coverage included in aggregate %.

17 of 28 new or added lines in 3 files covered. (60.71%)

14 existing lines in 2 files now uncovered.

5741 of 7926 relevant lines covered (72.43%)

298.72 hits per line

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

91.67
/src/FluentCommand/Internal/StringBuilderCache.cs
1
using System.Text;
2

3
namespace FluentCommand.Internal;
4

5
/// <summary>
6
/// Provides a cached, reusable instance of <see cref="StringBuilder"/> per thread to reduce memory allocations.
7
/// </summary>
8
/// <remarks>
9
/// This class is intended to optimize performance by reusing <see cref="StringBuilder"/> instances for short-lived operations.
10
/// The cache is thread-static, so each thread has its own cached instance.
11
/// </remarks>
12
public static class StringBuilderCache
13
{
14
    /// <summary>
15
    /// The maximum size, in characters, for a <see cref="StringBuilder"/> instance to be cached.
16
    /// </summary>
17
    internal const int MaxBuilderSize = 1024;
18

19
    private const int DefaultCapacity = 16; // == StringBuilder.DefaultCapacity
20

21
    [ThreadStatic]
22
    private static StringBuilder? t_cachedInstance;
23

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

42
        var sb = t_cachedInstance;
433✔
43
        if (sb == null)
433✔
44
            return new StringBuilder(capacity);
48✔
45

46
        // Avoid StringBuilder block fragmentation by getting a new StringBuilder
47
        // when the requested size is larger than the current capacity
48
        if (capacity > sb.Capacity)
385✔
49
            return new StringBuilder(capacity);
4✔
50

51
        t_cachedInstance = null;
381✔
52
        sb.Clear();
381✔
53

54
        return sb;
381✔
55
    }
56

57
    /// <summary>
58
    /// Places the specified <see cref="StringBuilder"/> instance in the cache if its capacity does not exceed <see cref="MaxBuilderSize"/>.
59
    /// </summary>
60
    /// <param name="sb">The <see cref="StringBuilder"/> instance to cache.</param>
61
    public static void Release(StringBuilder sb)
62
    {
63
        if (sb.Capacity <= MaxBuilderSize)
433✔
64
            t_cachedInstance = sb;
398✔
65
    }
433✔
66

67
    /// <summary>
68
    /// Converts the specified <see cref="StringBuilder"/> to a string and releases it to the cache.
69
    /// </summary>
70
    /// <param name="sb">The <see cref="StringBuilder"/> instance to convert and release.</param>
71
    /// <returns>The string representation of the <see cref="StringBuilder"/> content.</returns>
72
    public static string ToString(StringBuilder sb)
73
    {
74
        var result = sb.ToString();
433✔
75
        Release(sb);
433✔
76
        return result;
433✔
77
    }
78
}
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