Skip to content

Repository files navigation

CacheLineSize.NET

A cross-platform .NET Standard 1.5 library to get the cache line size of the processor, in bytes. Windows, Linux, and macOS supported.

NuGet Status

Usage

usingSystem;usingNickStrupat;classProgram{staticvoidMain(string[]args){Console.WriteLine(CacheLine.Size);// print the cache line size in bytesvararray=newCacheLineAlignedArray<string>(10);Interlocked.Exchange(refarray[0],"Hello");// all threads can now see the latest value at `array[0]` without risk of ruining performance with false-sharing// This can be used to build collections which share elements across threads at the fastest possible synchronization.}// An array-like type where each element is on it's own cache-line. This is a building block for avoiding false-sharing.publicstructCacheLineAlignedArray<T>whereT:class{privatereadonlyT[]buffer;publicCacheLineAlignedArray(Int32size)=>buffer=newT[Multiplier*size];publicInt32Length=>buffer.Length/Multiplier;publicrefTthis[Int32index]=>refbuffer[Multiplier*index];privatestaticreadonlyInt32Multiplier=CacheLine.Size/IntPtr.Size;}}

See also

Contributing

  1. Create an issue
  2. Let's find some point of agreement on your suggestion.
  3. Fork it!
  4. Create your feature branch: git checkout -b my-new-feature
  5. Commit your changes: git commit -am 'Add some feature'
  6. Push to the branch: git push origin my-new-feature
  7. Submit a pull request :D

History

Commit history

License

MIT License

About

A cross-platform .NET Standard library to get the cache line size (in bytes) of the processor.

Resources

Stars

17 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages