-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Unnecessary Slow Aligned Memory Reallocation (Multiple of System Page Size) #110225
Comments
Tagging subscribers to this area: @dotnet/gc |
Tagging subscribers to this area: @dotnet/interop-contrib |
For the most part For example:
Its a little bit more flexible with the aligned variants, as C didn't standardize them until more recently:
Notably "changing" the alignment is technically undefined behavior for the C API and it is technically possible for us to ignore the input and use |
Thank you for the very detailed information. If I understand correctly:
If so, what is the maximum alignment that NativeMemory.Realloc would maintain? Would it be 64 bytes, system page size, or another value? Can we include this information in the NativeMemory.Realloc documentation? Is the only purpose of NativeMemory.AlignedRealloc for situations when you want to change alignment? |
From a general public contract point for .NET, the The reason this nuance exists is because in some scenarios, like if we used certain POSIX APIs or on Windows where we need to defer to While we currently use the underlying C runtime API on systems that provide it (currently all officially supported Linux systems), we don't surface that detail publicly and so there's no way to query it. If such a detail was surfaced (or you were willing to rely on a point in time implementation detail) then mixing
It depends on the underlying system. The C runtime doesn't guarantee a range of values that
Changing alignment isn't strictly guaranteed to work as some underlying One bit I was trying to say in my previous message was that the .NET team could, with a bit more discussion, simplify our own implementation and just call
I think there's a few clarifying remarks we can add to improve things here, yes. Particularly in terms of what may be undefined behavior across platforms. |
Is that documented somewhere? I do not see it mentioned in any documentation and it does not appear to be the case based on my ad-hoc testing. For example, this is going to reliable show that realloc does not preserve 64kB alignment on Ubuntu 24.04:
The exact conditions where |
Hmmm, I thought it had been in the C17 or C23 spec; but after having re-read the relevant portions it isn't explicitly called out. It would indeed be dependent on the underlying implementation given that, which may not preserve it in all cases. |
I've been experimenting with resizing allocated aligned memory. I believe that increasing or decreasing memory by multiples of the system page size should be almost instantaneous.
The system seems capable of this when tested with NativeMemory.Realloc, which completes in around 1ms. However, NativeMemory.Realloc doesn't guarantee alignment preservation.
When I perform the same test with NativeMemory.AlignedRealloc, it takes several seconds to complete. It should be as fast as NativeMemory.Realloc when the requested alignment remains unchanged and the memory is resized by multiples of the system page size.
I'm unsure whether this issue should be reported to the .NET team or the operating system developers. I'm using Ubuntu 24.04 with kernel 6.10.14.
The text was updated successfully, but these errors were encountered: