-
-
Notifications
You must be signed in to change notification settings - Fork 614
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
Allow scope new
for arrays for dynamic stack allocation
#20770
Comments
I think |
void foo(int bar) @nogc {
scope int[] a = new int[](bar);
a ~= 42;
} Error? Allowed? void foo(int bar) @nogc {
scope int[] a = new int[](bar);
scope int[] b = new int[](bar);
a ~= 42;
} These could be complicated, but they seem possible. |
That won't work, because To do this with solely stack memory would require value range propagation which is a form of data flow analysis, which unfortunately would be specific to this use case. |
So is
I don't know what that is, but I'm assuming that it means this won't ever get implemented? |
When So no, not GC. It would be a stack allocation. void main() @nogc {
int[] heap = new int(2); // Error
scope int[] stack = new int(2); // Ok
}
Too low of value, and too high of a cost. So yeah that's a big no. |
scope new
for arrays for dynamic stack allocation
|
We can deprecate the function, internally it could be kept. It is also |
Two key properties of class instances that make stack allocation viable:
What if I allocate something that exceeds the stack size? void main() @nogc
{
scope int[] arr = new int[32_000_000];
} |
Regarding appending: It should behave as appending to a slice of a static array. Which currently leads to reallocation. I don't think we'd need to do something for it to work, since the GC will not just be able to find a block for it, as currently happens for static array.
Can always happen, can it ? E.g. you can have |
I thought D was moving away from DIP1000/escape analysis, but if it's not, this is a pretty logical feature to build on top of that capability. Obviously if |
Sounds like a bug in fibers, either they should probe guard pages, or be
I'm not talking about executable size, the problem is that it's normal to allocate multiple megabytes with |
You probably shouldn't change those to stack allocations, then. void main() {
int[32_000_000] foo; // segfault ahoy
} This is already legal and crashes just as spectacularly as you'd expect. |
Exactly. So my question is, how (in this proposal) does the compiler decide whether |
As it currently stands it won't compile in But what to do about the other functions? That may need to wait for an edition. |
What other functions? D doesn't have editions, so waiting for that seems like a bad idea. |
I was referring to functions not marked
We can remove or replace the check for No need to solve everything all at once. |
alloca
is typically implemented as an intrinsic.But as a function prototype it is inherently unsafe without an additional attribute to inform the caller that it must not escape up or out of the call stack.
However we have a way to deal with this, by using
new
and putting it into ascope
variable:Currently this does not work for arrays, but does for classes.
Expanding
new
to work for other types, would be a memory safe method that does not need this unsafe function prototype that we could then remove at a later date.The text was updated successfully, but these errors were encountered: