-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
72 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Text; | ||
|
||
namespace FluentRest; | ||
|
||
/// <summary>Provide a cached reusable instance of StringBuilder per thread.</summary> | ||
internal static class StringBuilderCache | ||
{ | ||
// The value 360 was chosen in discussion with performance experts as a compromise between using | ||
// as little memory per thread as possible and still covering a large part of short-lived | ||
// StringBuilder creations on the startup path of VS designers. | ||
internal const int MaxBuilderSize = 360; | ||
private const int DefaultCapacity = 16; // == StringBuilder.DefaultCapacity | ||
|
||
[ThreadStatic] | ||
private static StringBuilder t_cachedInstance; | ||
|
||
/// <summary>Get a StringBuilder for the specified capacity.</summary> | ||
/// <remarks>If a StringBuilder of an appropriate size is cached, it will be returned and the cache emptied.</remarks> | ||
public static StringBuilder Acquire(int capacity = DefaultCapacity) | ||
{ | ||
if (capacity > MaxBuilderSize) | ||
return new StringBuilder(capacity); | ||
|
||
var sb = t_cachedInstance; | ||
if (sb == null) | ||
return new StringBuilder(capacity); | ||
|
||
// Avoid StringBuilder block fragmentation by getting a new StringBuilder | ||
// when the requested size is larger than the current capacity | ||
if (capacity > sb.Capacity) | ||
return new StringBuilder(capacity); | ||
|
||
t_cachedInstance = null; | ||
sb.Clear(); | ||
|
||
return sb; | ||
|
||
} | ||
|
||
/// <summary>Place the specified builder in the cache if it is not too big.</summary> | ||
public static void Release(StringBuilder sb) | ||
{ | ||
if (sb.Capacity <= MaxBuilderSize) | ||
t_cachedInstance = sb; | ||
} | ||
|
||
/// <summary>Release StringBuilder to the cache, and return the resulting string.</summary> | ||
public static string ToString(StringBuilder sb) | ||
{ | ||
string result = sb.ToString(); | ||
Release(sb); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters