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

loresoft / FluentRest / 13399786835

18 Feb 2025 08:48PM UTC coverage: 57.756% (-0.4%) from 58.107%
13399786835

push

github

pwelter34
add support for nullable, minor cleanup, improve UrlBuilder

293 of 646 branches covered (45.36%)

Branch coverage included in aggregate %.

238 of 380 new or added lines in 20 files covered. (62.63%)

3 existing lines in 3 files now uncovered.

824 of 1288 relevant lines covered (63.98%)

56.98 hits per line

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

75.0
/src/FluentRest/StringBuilderCache.cs
1
using System.Text;
2

3
namespace FluentRest;
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)
446!
22
            return new StringBuilder(capacity);
×
23

24
        var sb = t_cachedInstance;
446✔
25
        if (sb is null)
446✔
26
            return new StringBuilder(capacity);
96✔
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)
350!
31
            return new StringBuilder(capacity);
×
32

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

36
        return sb;
350✔
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)
446!
NEW
44
            throw new ArgumentNullException(nameof(stringBuilder));
×
45

46
        if (stringBuilder.Capacity <= MaxBuilderSize)
446✔
47
            t_cachedInstance = stringBuilder;
446✔
48
    }
446✔
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)
446!
NEW
54
            throw new ArgumentNullException(nameof(stringBuilder));
×
55

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

59
        return result;
446✔
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

© 2025 Coveralls, Inc