Skip to content

Async and Thread Safety

axunonb edited this page May 18, 2022 · 3 revisions

SmartFormat makes heavy use of caching and object pooling for expensive operations, which both require static containers.

SmartFormatters and Parsers require one instance per thread.

Multi-Threading, async context

When instantiating SmartFormatters from different threads, SmartSettings.IsThreadSafeMode=true must be set (which is the default since v3.1.0), so that thread-safe containers are used. This brings an inherent performance penalty.

Note:

Static Smart methods like Smart.Format(format, args) can be called in an async / multi-threaded context since v3.1.0.

The SmartFormatter instance returned by Smart.Default is flagged with the ThreadStatic attribute. This means, that a new, isolated SmartFormatter instance will be created for each thread. Any changes to this instance will only have effect within the thread (not for all threads).

Single-Threading

When instantiating SmartFormatters from a single thread, SmartSettings.IsThreadSafeMode=false should be set for avoiding the multithreading overhead and thus for best performance. The default, however, is SmartSettings.IsThreadSafeMode=true

Clone this wiki locally