Skip to content

Commit e764d49

Browse files
authored
Add capacity parameter to StringBuilderCacheAlt.Allocate for custom sizing (ServiceStack#1365)
1 parent 33423b7 commit e764d49

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

ServiceStack.Text/src/ServiceStack.Text/StringBuilderCache.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static StringBuilder Allocate()
1818
return new StringBuilder();
1919

2020
ret.Length = 0;
21-
cache = null; //don't re-issue cached instance until it's freed
21+
cache = null; //don't re-issue cached instance until it's freed
2222
return ret;
2323
}
2424

@@ -43,17 +43,22 @@ public static class StringBuilderCacheAlt
4343
[ThreadStatic]
4444
static StringBuilder cache;
4545

46-
public static StringBuilder Allocate()
46+
public static StringBuilder Allocate(int capacity)
4747
{
4848
var ret = cache;
4949
if (ret == null)
50-
return new StringBuilder();
50+
return new StringBuilder(capacity);
5151

5252
ret.Length = 0;
53-
cache = null; //don't re-issue cached instance until it's freed
53+
cache = null; //don't re-issue cached instance until it's freed
5454
return ret;
5555
}
5656

57+
public static StringBuilder Allocate()
58+
{
59+
return Allocate(16); // 16 is the default capacity for StringBuilder
60+
}
61+
5762
public static void Free(StringBuilder sb)
5863
{
5964
cache = sb;
@@ -80,7 +85,7 @@ public static StringBuilder Allocate()
8085
return new StringBuilder();
8186

8287
ret.Length = 0;
83-
cache = null; //don't re-issue cached instance until it's freed
88+
cache = null; //don't re-issue cached instance until it's freed
8489
return ret;
8590
}
8691

0 commit comments

Comments
 (0)