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

loresoft / MediatR.CommandQuery / 13575541599

27 Feb 2025 08:45PM UTC coverage: 58.86% (-1.2%) from 60.086%
13575541599

push

github

pwelter34
add UrlBuilder

480 of 955 branches covered (50.26%)

Branch coverage included in aggregate %.

160 of 282 new or added lines in 3 files covered. (56.74%)

1440 of 2307 relevant lines covered (62.42%)

22.25 hits per line

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

75.0
/src/MediatR.CommandQuery/Services/StringBuilderCache.cs
1
using System.Text;
2

3
namespace MediatR.CommandQuery.Services;
4

5
/// <summary>Provide a cached reusable instance of StringBuilder per thread.</summary>
6
internal static class StringBuilderCache
7
{
8
    // The value 360 was chosen in discussion with performance experts as a compromise between using
9
    // as little memory per thread as possible and still covering a large part of short-lived
10
    // StringBuilder creations on the startup path of VS designers.
11
    internal const int MaxBuilderSize = 360;
12
    private const int DefaultCapacity = 16; // == StringBuilder.DefaultCapacity
13

14
    [ThreadStatic]
15
    private static StringBuilder? t_cachedInstance;
16

17
    /// <summary>Get a StringBuilder for the specified capacity.</summary>
18
    /// <remarks>If a StringBuilder of an appropriate size is cached, it will be returned and the cache emptied.</remarks>
19
    public static StringBuilder Acquire(int capacity = DefaultCapacity)
20
    {
21
        if (capacity > MaxBuilderSize)
70!
NEW
22
            return new StringBuilder(capacity);
×
23

24
        var sb = t_cachedInstance;
70✔
25
        if (sb is null)
70✔
26
            return new StringBuilder(capacity);
2✔
27

28
        // Avoid StringBuilder block fragmentation by getting a new StringBuilder
29
        // when the requested size is larger than the current capacity
30
        if (capacity > sb.Capacity)
68!
NEW
31
            return new StringBuilder(capacity);
×
32

33
        t_cachedInstance = null;
68✔
34
        sb.Clear();
68✔
35

36
        return sb;
68✔
37

38
    }
39

40
    /// <summary>Place the specified builder in the cache if it is not too big.</summary>
41
    public static void Release(StringBuilder stringBuilder)
42
    {
43
        if (stringBuilder is null)
70!
NEW
44
            throw new ArgumentNullException(nameof(stringBuilder));
×
45

46
        if (stringBuilder.Capacity <= MaxBuilderSize)
70✔
47
            t_cachedInstance = stringBuilder;
70✔
48
    }
70✔
49

50
    /// <summary>Release StringBuilder to the cache, and return the resulting string.</summary>
51
    public static string ToString(StringBuilder stringBuilder)
52
    {
53
        if (stringBuilder is null)
70!
NEW
54
            throw new ArgumentNullException(nameof(stringBuilder));
×
55

56
        string result = stringBuilder.ToString();
70✔
57
        Release(stringBuilder);
70✔
58

59
        return result;
70✔
60
    }
61
}
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