Skip to content

Conversation

@axunonb
Copy link
Member

@axunonb axunonb commented Mar 17, 2025

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.

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);
        }
    }
}

@sonarqubecloud
Copy link

@codecov
Copy link

codecov bot commented Mar 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97%. Comparing base (9c43562) to head (aa764e6).
Report is 1 commits behind head on main.

Additional details and impacted files
@@         Coverage Diff         @@
##           main   #474   +/-   ##
===================================
  Coverage    97%    97%           
===================================
  Files        99     99           
  Lines      3401   3401           
===================================
  Hits       3296   3296           
  Misses      105    105           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@axunonb axunonb marked this pull request as ready for review March 17, 2025 16:35
@axunonb axunonb merged commit daaa16d into main Mar 17, 2025
7 checks passed
@axunonb axunonb deleted the pr/v3.6.0 branch March 17, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant