-
-
Couldn't load subscription status.
- Fork 106
Thread Safety
SmartFormat extensively uses caching and object pooling for expensive operations, utilizing static containers for optimal efficiency. These containers are shared across all instances of SmartFormatter.
The type of container used depends on the SmartSettings.IsThreadSafeMode setting. If set to true, thread-safe containers are used. If set to false, faster, non-thread-safe containers are used, which are suitable only for single-threaded contexts.
When using a SmartFormatter instance across multiple threads, SmartSettings.IsThreadSafeMode must be set to true (the default since version 3.1.0) to ensure thread-safe containers are used. This setting incurs a performance penalty.
From version 3.6.0 onwards, multi-threading has been optimized:
- The
SmartFormatter'sFormat...methods are thread-safe. - The static
SmartFormatterinstanceSmart.Defaultis thread-safe, provided noSmartSettingsor instance properties are modified during formatting. - The
ParsermethodParseFormatis thread-safe, as long as noParserSettingsor instance properties are changed during parsing.
Custom ISource or IFormatter implementations must be thread-safe or used only in single-threaded contexts.
Note:
Versions prior to 3.6.0 used the ThreadStatic attribute for the SmartFormatter instance returned by Smart.Default, creating a new, isolated instance for each thread. Changes to this instance affected only the thread in which they were made.
For single-threaded use, you should set SmartSettings.IsThreadSafeMode to false to avoid the overhead of multi-threading and achieve the best performance. However, the default setting is SmartSettings.IsThreadSafeMode=true.
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License
3.6