Something new found in String. Format source code: StringBuilderCache,
Cause:
I can't remember why I want to see the source code of String. Format for Mao Dian F12 today. I can see the bird thing at all:
Is it true that StringBuilder can get this information?
StringBuilderCache class
The following events are also simple, and the F12 source code is clicked on StringBuilderCache.(Resharpe is really a good thing ...)
First, we can see that this isInternal!
After some research, I finally figured out what this product was doing: the role of this class is to cache a StringBuilder object for those that are not doing a lot of Append operations, we also see thatPrivate const int MAX_BUILDER_SIZE = 360,It is used internally to determine that the threshold value of a new boundary value is 360 by using the cached StringBuilder function.
The process of this class is as follows:
1. Call the Acquire method to obtain a StringBuilder. The capacity is greater than 360 and new is used directly. Otherwise, use StringBuilderCache. the ThreadStatic variable CachedInstance. Of course, you need to clear the variable CachedInstance to prevent the thread from continuously calling the Acquire method to obtain the same StringBuilder reference.
2. GetStringAndRelease returns the final string object and caches the StringBuilder to the CachedInstance variable.
Test performance
Wrote a simple console program to verify StringBuilderCache:
The final result shows the advantages of a large number of calls:
Source Code address: https://github.com/a407121393/MyDotNetTestProj/tree/master/MyTestSln/TestStringBuilderCache
The Util class used is in another directory: https://github.com/a407121393/MyDotNetTestProj/tree/master/MyTestSln/Util