NLog源码解读——StringBuilderPool
生活随笔
收集整理的這篇文章主要介紹了
NLog源码解读——StringBuilderPool
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
剛剛騷情的跑了下NLog測試,,,,,一篇文章就那么Crash了~~~
好吧,這次簡化下,直接進入正題,有關GC的東東參考網上大部分文章吧。
源碼分析
// StringBuilder 的建立也會耗費大量的資源,因此共用他們,使用這個類來管理池子 //需要了從這里獲取一個,用完自動釋放 ItemHolder 即可釋放會池子 // 例如 using (var itemHolder = pool.Acquire()){} internal class StringBuilderPool{private StringBuilder _fastPool;private readonly StringBuilder[] _slowPool;private readonly int _maxBuilderCapacity;/// <summary>/// Constructor/// </summary>/// <param name="poolCapacity">Max number of items</param>/// <param name="initialBuilderCapacity">Initial StringBuilder Size</param>/// <param name="maxBuilderCapacity">Max StringBuilder Size</param>public StringBuilderPool(int poolCapacity, int initialBuilderCapacity = 1024, int maxBuilderCapacity = 512 * 1024){//一個快速池子,一直保持_fastPool = new StringBuilder(10 * initialBuilderCapacity);// 一個慢速池子,如果快速池子被占用,則從慢速池子取一個_slowPool = new StringBuilder[poolCapacity];for (int i = 0; i < _slowPool.Length; ++i){_slowPool[i] = new StringBuilder(initialBuilderCapacity);}_maxBuilderCapacity = maxBuilderCapacity;}/// <summary>/// Takes StringBuilder from pool/// </summary>/// <returns>Allow return to pool</returns>public ItemHolder Acquire(){//1.come on baby,先嘗試分配快速池子StringBuilder item = _fastPool;//2.如果你幸運的拿到快速池子,則盡快把快速池子置空,不要讓別人搶到了,否則,你就必須進慢速池子取了if (item == null || item != Interlocked.CompareExchange(ref _fastPool, null, item)){//3.你好,兄弟,你已經進入慢車道,來,一個個給你試試能用不for (int i = 0; i < _slowPool.Length; i++){item = _slowPool[i];if (item != null && item == Interlocked.CompareExchange(ref _slowPool[i], null, item)){//4.啊哈,發現一個慢速的,來,給你,別嫌棄return new ItemHolder(item, this, i);}}//這么倒霉,一個都沒有,算了,再給你分配一個新的吧,記住,你的編號~~~咦, 第0個吧~~~return new ItemHolder(new StringBuilder(), null, 0);}else{//你是幸運之星,快速跑~~~~return new ItemHolder(item, this, -1);}}/// <summary>/// Releases StringBuilder back to pool at its right place/// </summary>private void Release(StringBuilder stringBuilder, int poolIndex){//來,兄弟,好借好還,再借不難//什么,這么大,我記得借你時沒這么大啊,讓我再檢查檢查if (stringBuilder.Length > _maxBuilderCapacity){//我靠,你是快車道的,那就給你10倍大小如何,這都超了,那對不起,減半了~~// Avoid high memory usage by not keeping huge StringBuilders alive (Except one StringBuilder)int maxBuilderCapacity = poolIndex == -1 ? _maxBuilderCapacity * 10 : _maxBuilderCapacity;if (stringBuilder.Length > maxBuilderCapacity){stringBuilder = new StringBuilder(maxBuilderCapacity / 2);}}stringBuilder.Length = 0;//好,各就各位...if (poolIndex == -1){_fastPool = stringBuilder;}else{_slowPool[poolIndex] = stringBuilder;}}//下面是個池化對象類/// <summary>/// Keeps track of acquired pool item/// </summary>public struct ItemHolder : IDisposable{public readonly StringBuilder Item;readonly StringBuilderPool _owner;readonly int _poolIndex;public ItemHolder(StringBuilder stringBuilder, StringBuilderPool owner, int poolIndex){Item = stringBuilder;_owner = owner;_poolIndex = poolIndex;}/// <summary>/// Releases pool item back into pool/// </summary>public void Dispose(){if (_owner != null){_owner.Release(Item, _poolIndex);}}}}總結
以上是生活随笔為你收集整理的NLog源码解读——StringBuilderPool的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 无意间写了一段线程死锁的代码
- 下一篇: ABP Framework:移除 EF