open System open System.Text [] type StringBuilderCache private () = // The value 360 was chosen in discussion with performance experts as a compromise between using // as litle memory (per thread) as possible and still covering a large part of short-lived // StringBuilder creations on the startup path of VS designers. [] static let _maxBuilderSize = 360 // == StringBuilder.DefaultCapacity [] static let _defaultCapacity = 16 [] static val mutable private cachedInstance : StringBuilder static member Acquire (?capacity : int) = let capacity' = defaultArg capacity _defaultCapacity let sb = StringBuilderCache.cachedInstance // Avoid stringbuilder block fragmentation by getting a new StringBuilder // when the requested size is larger than the current capacity if capacity' <= _maxBuilderSize && not(isNull sb) && capacity' <= sb.Capacity then StringBuilderCache.cachedInstance <- null sb.Clear() |> ignore sb else new StringBuilder(capacity') static member GetString (sb : StringBuilder) = let result = sb.ToString() if sb.Capacity <= _maxBuilderSize then StringBuilderCache.cachedInstance <- sb result