Skip to content
axunonb edited this page Mar 17, 2025 · 9 revisions

Preface

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.

Multi-Threading

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's Format... methods are thread-safe.
  • The static SmartFormatter instance Smart.Default is thread-safe, provided no SmartSettings or instance properties are modified during formatting.
  • The Parser method ParseFormat is thread-safe, as long as no ParserSettings or 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.

Single-Threading

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.

Clone this wiki locally