Download Latest Version v3.6.0 source code.tar.gz (305.2 kB)
Email in envelope

Get an email when there's a new version of SmartFormat

Home / v3.6.0
Name Modified Size InfoDownloads / Week
Parent folder
README.md 2025-03-17 3.6 kB
v3.6.0 source code.tar.gz 2025-03-17 305.2 kB
v3.6.0 source code.zip 2025-03-17 463.0 kB
Totals: 3 Items   771.7 kB 0

SmartFormat 3.6.0 Release Notes

Thread Safety Enhancements

  1. Parser:
  2. The parsing logic in Parser has been refactored by replacing stateful instance variables.
  3. The Parser.ParseFormat(...) method is now thread-safe, ensuring safe operations in multi-threaded environments.

  4. SmartFormatter:

  5. All SmartFormatter.Format... methods are thread-safe.
  6. Removed the ThreadStatic attribute from the Smart.Default instance of SmartFormatter.
  7. Added parallel unit tests to ensure thread-safe operations with shared SmartFormatter instances using different Smart.Extensions.
  8. Updated documentation in Parser, Smart, and SmartFormatter classes to clarify the thread safety of methods.

Also see further remarks regarding thread-safety in the Wiki

Heads Up

  • Removal of ThreadStatic Attribute for Smart.Default:
  • The ThreadStatic attribute for the Smart.Default instance of SmartFormatter has been removed.
  • This change addresses user feedback regarding their lack of favor and the increased GC pressure it caused in multi-threaded environments like ASP.NET Core.
  • The removal is intended to enhance usability and performance in multi-threaded environments. It may, however, break existing code.

Sample Usage: Using One SmartFormatter Instance with Several Threads in Parallel

The following example demonstrates how to use a single SmartFormatter instance with multiple threads in parallel. This ensures thread-safe operations and efficient resource utilization.

:::csharp
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SmartFormat;

public class Program
{
    public static void Main()
    {
        // Create a single instance of SmartFormatter
        var smartFormatter = Smart.CreateDefaultSmartFormat();

        // Concurrent dictionary to store results
        var results = new ConcurrentDictionary<long, string>();
        var options = new ParallelOptions { MaxDegreeOfParallelism = 100 };

        // Run parallel tasks
        Parallel.For(0L, 1000, options, i =>
        {
            // Re-use the same SmartFormatter instance, where the Format method is thread-safe.
            // The static Smart.Format can be used as well, producing the same results
            results.TryAdd(i, smartFormatter.Format("{0:D3}", i));
        });

        // Output results
        var sortedResult = results.OrderBy(r => r.Value).ToList();
        foreach (var result in sortedResult)
        {
            Console.WriteLine(result.Value);
        }
    }
}

What's Changed in Detail

Full Changelog: https://github.com/axuno/SmartFormat/compare/v3.5.3...v3.6.0

Source: README.md, updated 2025-03-17