-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
optimize behavior of shrink in standard library data structures #2009
Comments
Here's the plan to solve it #1306 (comment) |
Before, allocator implementations had to provide `allocFn`, `reallocFn`, and `freeFn`. Now, they must provide only `reallocFn` and `shrinkFn`. Reallocating from a zero length slice is allocation, and shrinking to a zero length slice is freeing. When the new memory size is less than or equal to the previous allocation size, `reallocFn` now has the option to return `error.OutOfMemory` to indicate that the allocator would not be able to take advantage of the new size. For more details see #1306. This commit closes #1306. This commit paves the way to solving #2009. This commit also introduces a memory leak to all coroutines. There is an issue where a coroutine calls the function and it frees its own stack frame, but then the return value of `shrinkFn` is a slice, which is implemented as an sret struct. Writing to the return pointer causes invalid memory write. We could work around it by having a global helper function which has a void return type and calling that instead. But instead this hack will suffice until I rework coroutines to be non-allocating. Basically coroutines are not supported right now until they are reworked as in #1194.
Have a look at the new Allocator interface API: Lines 11 to 71 in 9c13e9b
In particular, And here's Lines 144 to 150 in 9c13e9b
So what's left is to update the other data structures from the standard library to take advantage of this new interface. |
Related: #1306
As @schmee notes in #2008,
ArrayList.shrink
does not actually interact with the allocator.I would like it to call
shrink
in the allocator. However the allocator currently has no way to communicate back, "hey I actually can't reclaim this memory, so if you just want to hang on to it, that would actually be better". So I want to look into this before deciding how shrink will work for array lists.To resolve this issue, we need to enumerate the various use cases for the various data structures that can have the concept of shrink:
And we need to consider the various allocators that may be used as a backing allocator for these data structures:
And then make sure that the most optimal thing is happening in every case, for every data structure. If this isn't the case, then the Allocator API and/or data structure APIs need to be modified.
The text was updated successfully, but these errors were encountered: