github axuno/SmartFormat v3.6.0

one day ago

Release Notes

Thread Safety Enhancements

  1. Parser:

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

    • All SmartFormatter.Format... methods are thread-safe.
    • Removed the ThreadStatic attribute from the Smart.Default instance of SmartFormatter.
    • Added parallel unit tests to ensure thread-safe operations with shared SmartFormatter instances using different Smart.Extensions.
    • 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.

User Feedback

image

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.

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

  • Refactor parsing for thread safety in #467
  • Update appveyor api key for nuget in #468
  • Shift CI Publishing Workflow to GitHub Actions in #469
  • Move code quality CI to Github Action CodeQuality.yml in #470
  • Make SmartFormatter.Format(...) methods thread-safe in #473
  • Bump version to v3.6.0 in #474

Full Changelog: v3.5.3...v3.6.0

Don't miss a new SmartFormat release

NewReleases is sending notifications on new releases.