Skip to content

Make SmartFormatter.Format(...) methods thread-safe - #473

Merged
axunonb merged 1 commit into
axuno:mainfrom
axunonb:pr/smartformatter-threadsafe
Mar 17, 2025
Merged

axunonb merged 1 commit into
axuno:mainfrom
axunonb:pr/smartformatter-threadsafe

Conversation

@axunonb

@axunonb axunonb commented Mar 16, 2025

Copy link
Copy Markdown
Member
  • Removed ThreadStatic attribute from the Smart.Default instance of SmartFormatter.
  • Added Parallel unit tests ensuring thread-safe operations with shared SmartFormatter instances with different Smart.Extensions.
  • Updated documentation in Parser.cs, Smart.cs, and SmartFormatter.cs to clarify thread safety of methods.

- Removed `ThreadStatic` attribute from the `Smart.Default` instance of `SmartFormatter`.
- Added `Parallel` unit tests ensuring thread-safe operations with shared `SmartFormatter` instances with different `Smart.Extensions`.
- Updated documentation in `Parser.cs`, `Smart.cs`, and `SmartFormatter.cs` to clarify thread safety of methods.
@axunonb

axunonb commented Mar 16, 2025

Copy link
Copy Markdown
Member Author

@karljj1 After making the Parser.ParseFormat method thread-safe, the same was possible for the SmartFormatter.Format methods. Of course all extensions must be thread-safe as well. For ours this is the case.

The ThreadStatic attribute for the Smart.Default instance of SmartFormatter has never been popular on users's side. When used in a multi-threaded environment like AspNet Core this was unhandy and increased GC pressure.

Removing the ThreadStatic attribute may mostly be welcome, but it is also a change in the behavior.
So do you think mentioning this change in the release notes would be sufficient?

@karljj1

karljj1 commented Mar 17, 2025

Copy link
Copy Markdown
Collaborator

Yeah I think mentioning it should be fine. Do you know what difference this makes to performance? When I first started using the library I noticed removing the multi-threading support improved the performance. Would it be possible to put the thread support behind an ifdef so it can be toggled on/off?

@axunonb

axunonb commented Mar 17, 2025

Copy link
Copy Markdown
Member Author

Thanks for your quick reply. So then the PR is ready for review.

Making the Parser thread-safe had almost no impact on performance, while at the same time we need less Parser instances.

Regarding SmartFormatter thread-safety for the Format method overloads, this was already built-in for quite a while. The only precondition for multi-threading is to set SmartSettings.IsThreadSafeMode = true, which is the default.

And yes, SmartSettings.IsThreadSafeMode = false while working on a single thread brings a nice improvement for speed but also for GC pressure. Now, that Parser.ParseFormat and Formatter.Formats are thread-safe, the penalty for thread-safety gets compensated by less Parser and SmartFormatter instances needed.

@axunonb
axunonb marked this pull request as ready for review March 17, 2025 11:56
@axunonb
axunonb requested a review from karljj1 March 17, 2025 11:56

@karljj1 karljj1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Sounds great :)

@axunonb
axunonb merged commit 9c43562 into axuno:main Mar 17, 2025
@axunonb
axunonb deleted the pr/smartformatter-threadsafe branch March 17, 2025 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants