Skip to content

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

License

Notifications You must be signed in to change notification settings

NickStrupat/CacheLineSize.NET

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

using System;
using NickStrupat;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(CacheLine.Size); // print the cache line size in bytes
        
        var array = new CacheLineAlignedArray<string>(10);
        Interlocked.Exchange(ref array[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.
    public struct CacheLineAlignedArray<T> where T : class {
        private readonly T[] buffer;
        public CacheLineAlignedArray(Int32 size) => buffer = new T[Multiplier * size];
        public Int32 Length => buffer.Length / Multiplier;
        public ref T this[Int32 index] => ref buffer[Multiplier * index];
        private static readonly Int32 Multiplier = 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

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages