Changes since v.3.0.0-alpha.4
1. Object Pools
After implementing Object Pools for all classes which are frequently instantiated, GC and memory allocation again went down significantly.
In order to return "smart" objects back to the object pool, its important to use one of the following patterns.
Examples:
a) Single thread context (no need to care about object pooling)
var resultString = Smart.Format("format string", args);
b) Recommended: Auto-dispose Format
(e.g.: caching, multi treading context)
var smart = Smart.CreateDefaultSmartFormat();
// Note "using" for auto-disposing the parsedFormat
using var parsedFormat = new Parser().ParseFormat("format string", args);
var resultString = smart.Format(parsedFormat);
c) Call Format.Dispose()
(e.g.: caching, multi treading context)
var smart = Smart.CreateDefaultSmartFormat();
var parsedFormat = new Parser().ParseFormat("format string", args);
var resultString = smart.Format(parsedFormat);
// Don't use (or reference) "parsedFormat" after disposing
parsedFormat.Dispose();
2. Thread Safety
SmartFormat makes heavy use of caching and object pooling for expensive operations, which both require static
containers.
a) Instantiating SmartFormatter
s from different threads:
`SmartSettings.IsThreadSafeMode=true` **must** be set, so that thread safe containers are used. This brings an inherent performance penalty.
**Note:** The simplified `Smart.Format(...)` API overloads use a static `SmartFormatter` instance which is **not** thread safe. Call `Smart.CreateDefaultSmartFormat()` to create a default `Formatter`.
a) Instantiating SmartFormatter
s from a single thread:
`SmartSettings.IsThreadSafeMode=false` **should** be set for avoiding the multithreading overhead and thus for best performance.
The simplified `Smart.Format(...)` API overloads are allowed here.