As can be seen here, one of String.Join
's overloads works with raw pointers and uses something called UnSafeCharBuffer
. Why is this? Is it a performance optimization?
Asked
Active
Viewed 1,410 times
8

NetherGranite
- 1,940
- 1
- 14
- 42
-
9Yes; that's for performance. All of `string` is very heavily optimized. – SLaks Jan 17 '19 at 21:35
-
3`StringBuilder` still has to copy to a string as the final step. Writing directly to the return string skips that overhead. – Ry- Jan 17 '19 at 21:37
-
Also `StringBuilder` has a default size that may not be entirely consumed, and continues to increase by that buffer every time you exceed. So three or four smaller strings being created is still a lot faster. – Greg Jan 17 '19 at 21:53
-
@Greg: Three or four smaller strings? Anyway, you can create a `StringBuilder` with an exact initial (and maximum) capacity. – Ry- Jan 17 '19 at 21:54
-
@Ry- But that still is converted into a `string`. So `StringBuilder` will consume a percentage of memory defined, just to consume more memory when it becomes a `string`. Boils down to this http://www.yoda.arachsys.com/csharp/stringbuilder.html – Greg Jan 17 '19 at 22:01
-
@Greg: Correct. What do you mean by “three or four smaller strings” though? – Ry- Jan 18 '19 at 00:25
-
That smaller immutable strings consume less memory than the overhead generated by StringBuilder often. – Greg Jan 18 '19 at 06:28
1 Answers
15
Is a performance optimization?
Yes.
In general you should expect that unsafe code is either for low-level unmanaged language interop or for performance optimization. In this case it is the latter.
This then suggests the question:
Why not use the same techniques for StringBuilder?
Different scenarios can be tuned using different optimization techniques; StringBuilders are optimized for their scenarios.
The scenarios are different in several ways. Join
knows ahead of time exactly how many bytes will be returned; StringBuilder
does not. Join
knows that the resulting string will be generated exactly once, but a StringBuilder
has to support the create, append, ToString
, append, ToString
, ... workflow efficiently. And so on.

Eric Lippert
- 647,829
- 179
- 1,238
- 2,067
-
but there are three other overloads/implementations of `String.Join` that actually use `StringBuilder`... You're only talking about a specific one. – adjan Jan 17 '19 at 22:22
-
4@Adrian: I made the reasonable assumption that the original poster was talking about the implementation that does not use a stringbuilder, because the question was about that implementation, and the question was "Why doesn't C#'s String.Join use StringBuilder?" Why would anyone assume that the OP was asking about an implementation that does use StringBuilder, given that question? – Eric Lippert Jan 17 '19 at 22:26
-
2I agree one can infer this from the question, although i would say that it (especially the title) sounds like there was only *this one* implementation, and no other – adjan Jan 17 '19 at 22:33
-
@Adrian I agree that the title is a little misleading. I'll revise it. – NetherGranite Jan 18 '19 at 01:46