Name | Modified | Size | Downloads / 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
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. -
SmartFormatter
: - All
SmartFormatter.Format...
methods are thread-safe. - Removed the
ThreadStatic
attribute from theSmart.Default
instance ofSmartFormatter
. - Added parallel unit tests to ensure thread-safe operations with shared
SmartFormatter
instances using differentSmart.Extensions
. - Updated documentation in
Parser
,Smart
, andSmartFormatter
classes to clarify the thread safety of methods.
Also see further remarks regarding thread-safety in the Wiki
Heads Up
- Removal of
ThreadStatic
Attribute forSmart.Default
: - The
ThreadStatic
attribute for theSmart.Default
instance ofSmartFormatter
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
- Refactor parsing for thread safety in https://github.com/axuno/SmartFormat/pull/467
- Update appveyor api key for nuget in https://github.com/axuno/SmartFormat/pull/468
- Shift CI Publishing Workflow to GitHub Actions in https://github.com/axuno/SmartFormat/pull/469
- Move code quality CI to Github Action CodeQuality.yml in https://github.com/axuno/SmartFormat/pull/470
- Make
SmartFormatter.Format(...)
methods thread-safe in https://github.com/axuno/SmartFormat/pull/473 - Bump version to v3.6.0 in https://github.com/axuno/SmartFormat/pull/474
Full Changelog: https://github.com/axuno/SmartFormat/compare/v3.5.3...v3.6.0