Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: Add some Memory API to do tricky mappings #110984

Open
Stoorx opened this issue Dec 29, 2024 · 3 comments
Open

[API Proposal]: Add some Memory API to do tricky mappings #110984

Stoorx opened this issue Dec 29, 2024 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime untriaged New issue has not been triaged by the area owner

Comments

@Stoorx
Copy link

Stoorx commented Dec 29, 2024

Background and motivation

Once upon a time I decided to implement the fastest gun circular buffer on the wild West. I have found a tricky solution which maps two virtual pages to the same physical page. This thick virtually 'unwraps' the circular buffer over the page bounds. This leads to three and a half benefits:

  1. There is no need to perform modulo operations to wrap elements. It is made by MMU hardware, since the second page are mapped into the same as first.
  2. Since the elements are virtually unwrapped now, we can perform bulk operations in single copy operation.
  3. That buffer is inherently a bipartite buffer, which can be used in APIs that expect only contigous memory region.
    3.5. This is more SIMD friendly (I am not sure)

I can easily implement that trick in C++ using mmap() or VirtualAlloc(). But it still seems impossible to me to implement it in C# without using P/Invoke or another kludges. I had an idea to use a memory mapped file and somehow pin it twice with (some help of unsafe sections), but it looks really clumsy.

Maybe it is possible to implement in more elegant way, but I did not find any good ideas. (Or it is just googling skill issue)
I am open for discussion and hope this API would be helpful not only for circular buffers. I think I can implement this API by myself after we discuss the design of it.

API Proposal

I still have no idea of the best API design. But maybe something like that...

public class Memory
{
    public static unsafe void VirtualMap(IntPtr from, IntPtr to);
    public static unsafe void VirtualUnmap(IntPtr ptr);
}

API Usage

Something like that

public class RingBuffer: IDisposable
{
    private IntPtr _buffer;
    private int _size;
    ...
    public RingBuffer(int size)
    {
        ...
        unsafe {
            _buffer = GCHandle.Alloc(size * 2);
            Memory.VirtualMap(_buffer + size, _buffer); // Map second half of the buffer to the same page
        }
        ...
    }
    public void Dispose(){
        Memory.VirtualUnmap(_buffer + _size);
        GCHandle.Free(_buffer);
    }
}

Alternative Designs

No response

Risks

It is difficult to me to predict some risks at this point. But I think there is a possible issues with JIT, GC or dotnet memory model.

@Stoorx Stoorx added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 29, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 29, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 29, 2024
@DaZombieKiller
Copy link
Contributor

Related to #40892

@huoyaoyuan huoyaoyuan added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 30, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@KalleOlaviNiemitalo
Copy link

Aliased mapping of a circular buffer was mentioned in #63474 (comment). According to #75790 (comment), writes cause undefined behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

4 participants