1 people like it.

StringBuilderCache.fs

F# Impletementation of StringBuilderCache

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
open System
open System.Text

[<AbstractClass; Sealed>]
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.
    [<Literal>]
    static let _maxBuilderSize = 360

    // == StringBuilder.DefaultCapacity
    [<Literal>]
    static let _defaultCapacity = 16

    [<ThreadStatic; DefaultValue>]
    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
namespace System
namespace System.Text
Multiple items
type AbstractClassAttribute =
  inherit Attribute
  new : unit -> AbstractClassAttribute

--------------------
new : unit -> AbstractClassAttribute
Multiple items
type SealedAttribute =
  inherit Attribute
  new : unit -> SealedAttribute
  new : value:bool -> SealedAttribute
  member Value : bool

--------------------
new : unit -> SealedAttribute
new : value:bool -> SealedAttribute
Multiple items
type StringBuilderCache =
  private new : unit -> StringBuilderCache
  static val mutable private cachedInstance: StringBuilder
  static member Acquire : ?capacity:int -> StringBuilder
  static member GetString : sb:StringBuilder -> string

--------------------
private new : unit -> StringBuilderCache
Multiple items
type LiteralAttribute =
  inherit Attribute
  new : unit -> LiteralAttribute

--------------------
new : unit -> LiteralAttribute
val _maxBuilderSize : int
val _defaultCapacity : int
Multiple items
type ThreadStaticAttribute =
  inherit Attribute
  new : unit -> ThreadStaticAttribute

--------------------
ThreadStaticAttribute() : ThreadStaticAttribute
Multiple items
type DefaultValueAttribute =
  inherit Attribute
  new : unit -> DefaultValueAttribute
  new : check:bool -> DefaultValueAttribute
  member Check : bool

--------------------
new : unit -> DefaultValueAttribute
new : check:bool -> DefaultValueAttribute
StringBuilderCache.cachedInstance: StringBuilder
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 23 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 7 overloads
  member AppendJoin : separator:string * [<ParamArray>] values:obj[] -> StringBuilder + 5 overloads
  member AppendLine : unit -> StringBuilder + 1 overload
  member Capacity : int with get, set
  member Chars : int -> char with get, set
  member Clear : unit -> StringBuilder
  member CopyTo : sourceIndex:int * destination:Span<char> * count:int -> unit + 1 overload
  member EnsureCapacity : capacity:int -> int
  ...
  nested type ChunkEnumerator

--------------------
StringBuilder() : StringBuilder
StringBuilder(capacity: int) : StringBuilder
StringBuilder(value: string) : StringBuilder
StringBuilder(value: string, capacity: int) : StringBuilder
StringBuilder(capacity: int, maxCapacity: int) : StringBuilder
StringBuilder(value: string, startIndex: int, length: int, capacity: int) : StringBuilder
val capacity : int option
Multiple items
val int : value:'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
val capacity' : int
val defaultArg : arg:'T option -> defaultValue:'T -> 'T
val sb : StringBuilder
val not : value:bool -> bool
val isNull : value:'T -> bool (requires 'T : null)
val ignore : value:'T -> unit
val result : string
Raw view Test code New version

More information

Link:http://fssnip.net/87r
Posted:3 months ago
Author:Pim
Tags: string , string manipulation , stringbuilder