Something new found in String. Format source code: StringBuilderCache,

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.