-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
ARC now capable of custom extra alignment. Ref, closure and seq support. #15697
Conversation
ready for review, test failures seem unrelated |
found the bug with useMalloc. Investigating |
Ready for review. Increased max alignment to 32768. |
Does this mean that we also overallocate with our default native Nim allocator? When exactly do we overallocate? |
Here are my findings. Functions like posix_memalign, aligned_alloc are always overallocating because they need to store alignment offset somewhere hence using them in all cases is not an option as Nim's memory footprint would increase significantly. There is no standard way to reallocate aligned memory while this op is used by seqv2 implementation. Hence I implemented my own portable In order to keep overhead low I am checking if extra alignment is required at all. If not I revert back to standard allocation (either nim allocator or c_malloc depending on useMalloc). proc alignedDealloc(p: pointer, align: int) {.compilerproc.} =
if align <= MemAlign:
existingDealloc(p)
else:
let offset = cast[ptr uint16](p -! sizeof(uint16))[]
existingDealloc(p -! offset) Summary: |
Also it is a good time to reduce default |
fixes #7865 |
result = alloc(size) | ||
else: | ||
when compileOption("threads"): | ||
let base = allocShared(size + align - 1 + sizeof(uint16)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Write a comment that explains this logic. And also why it doesn't produce unaligned stores (which can be performance desasters on non-x86 CPUs).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added the comments. I have carefully reviewed the code for unaligned reads or stores. I dont' see any.
ready for review |
@cooldome Do you plan to make doc comments, export aligned* and add changelog entry in a follow up or it wasn't exported on purpose? |
Max Alignment is 256 bytes. Let me know if you want me to increase it.Max Alignment is 32768 bytes.
For
ref T
I had to do the following trick, suppose the alignment is 64 bytes and we are overallocating then memory layout is: padding first thenRefHeader
then the object. Hence there is a padding at start such thathead(myref)
stil works and it doescast[int](myref) - sizeof(RefHeader)
.New functions
alignedAlloc, alignedRealloc, alignedDealloc
mimic what the functionsposix_memalign
,aligned_alloc
do but in the platform independent way. These functions are overallocating on windows and linux.