--- lang: ja-jp breaks: true --- # C# スレッド単位 `[ThreadStatic]` で静的な `StringBuilder` のキャッシュ機構 2022-07-24 ## Dapper > DapperLib/Dapper > https://github.com/DapperLib/Dapper > https://github.com/DapperLib/Dapper/blob/ca00feeb5fafe5262166689c0bec2b80b53add4e/Dapper/SqlMapper.cs#L3796 ```csharp= // one per thread [ThreadStatic] private static StringBuilder perThreadStringBuilderCache; private static StringBuilder GetStringBuilder() { var tmp = perThreadStringBuilderCache; if (tmp != null) { perThreadStringBuilderCache = null; tmp.Length = 0; return tmp; } return new StringBuilder(); } private static string ToStringRecycle(this StringBuilder obj) { if (obj == null) return ""; var s = obj.ToString(); perThreadStringBuilderCache ??= obj; return s; } ``` ## .NET Core > Reducing allocations by caching with StringBuilderCache > https://andrewlock.net/a-deep-dive-on-stringbuilder-part-5-reducing-allocations-by-caching-stringbuilders-with-stringbuildercache/ > StringBuilderCache.cs > https://github.com/dotnet/runtime/blob/main/src/libraries/Common/src/System/Text/StringBuilderCache.cs ```csharp= // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. namespace System.Text { /// <summary>Provide a cached reusable instance of stringbuilder per thread.</summary> internal static class StringBuilderCache { // The value 360 was chosen in discussion with performance experts as a compromise between using // as little memory per thread as possible and still covering a large part of short-lived // StringBuilder creations on the startup path of VS designers. internal const int MaxBuilderSize = 360; private const int DefaultCapacity = 16; // == StringBuilder.DefaultCapacity [ThreadStatic] private static StringBuilder? t_cachedInstance; /// <summary>Get a StringBuilder for the specified capacity.</summary> /// <remarks>If a StringBuilder of an appropriate size is cached, it will be returned and the cache emptied.</remarks> public static StringBuilder Acquire(int capacity = DefaultCapacity) { if (capacity <= MaxBuilderSize) { StringBuilder? sb = t_cachedInstance; if (sb != null) { // Avoid stringbuilder block fragmentation by getting a new StringBuilder // when the requested size is larger than the current capacity if (capacity <= sb.Capacity) { t_cachedInstance = null; sb.Clear(); return sb; } } } return new StringBuilder(capacity); } /// <summary>Place the specified builder in the cache if it is not too big.</summary> public static void Release(StringBuilder sb) { if (sb.Capacity <= MaxBuilderSize) { t_cachedInstance = sb; } } /// <summary>ToString() the stringbuilder, Release it to the cache, and return the resulting string.</summary> public static string GetStringAndRelease(StringBuilder sb) { string result = sb.ToString(); Release(sb); return result; } } } ``` ###### tags: `C#` `ThreadStatic` `StringBuilder` `キャッシュ` `ThreadStatic`