computablee / DotMP

A collection of powerful abstractions for parallel programming in .NET with an OpenMP-like API.
https://computablee.github.io/DotMP/
GNU Lesser General Public License v2.1
29 stars 7 forks source link

Improve `GetThreadNum` performance #111

Closed computablee closed 1 year ago

computablee commented 1 year ago

Which issue are you addressing?

Closes #110

How have you addressed the issue?

Using ThreadLocal<T>, the thread num is only calculated with ToInt32 once, and from thereon is cached as a thread-local variable.

How have you tested your patch?

A new Misc benchmark was added. Omitting the time to call InParallel(), the new GetThreadNum() runs in about 11ns, whereas the old took about 24ns. Negligible performance improvement for most code, but could be beneficial if GetThreadNum() is called millions or billions of times in a tight loop over the course of an application.

Of course, all unit tests still pass.

codecov[bot] commented 1 year ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (c7eec2c) 99.19% compared to head (5ea0102) 99.11%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #111 +/- ## ========================================== - Coverage 99.19% 99.11% -0.09% ========================================== Files 12 12 Lines 1237 1238 +1 Branches 126 126 ========================================== Hits 1227 1227 Misses 5 5 - Partials 5 6 +1 ``` | [Files](https://app.codecov.io/gh/computablee/DotMP/pull/111?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Phillip+Allen+Lane) | Coverage Δ | | |---|---|---| | [DotMP/Parallel.cs](https://app.codecov.io/gh/computablee/DotMP/pull/111?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Phillip+Allen+Lane#diff-RG90TVAvUGFyYWxsZWwuY3M=) | `98.77% <100.00%> (-0.21%)` | :arrow_down: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.