by foldr · 1y ago · 0 comments
Why? It is the same as in C.
#include <stdio.h>
#include <stdlib.h>
struct slice {
int *data;
size_t len;
size_t cap;
};
struct slice readLogsFromPartition() {
int *data = malloc(2);
data[0] = 1;
data[1] = 2;
return (struct slice){ data, 2, 2 };
}
int main() {
struct slice s = readLogsFromPartition();
for (int i = 0; i < s.len; i++) {
printf("%d\n", s.data[i]);
}
free(s.data);
} func foo() {
x := []int { 1 }
//SNIP
}
Could translate to C either as: void foo() {
int* x = malloc(1 * sizeof(int));
x[0] = 1;
//...
}
Or as void foo() {
int data[1] = {1};
int *x = data;
//...
}
Depending on the content of //SNIP. However, some people think that the semantics can also match the semantics of the second version in C - when in fact the semantics of the Go code always match the first version, even when the actual implementation is the second version.* Of course, in reality, Java also does escape analysis to allocate on the stack, though it's less likely to happen because of the lack of value types.
int *foo(void) {
int x = 99;
return &x; // bad idea
}
vs. func foo() *int {
x := 99
return &x // fine
}
They think that Go, like C, will allocate x on the stack, and that returning a pointer to the value will therefore be invalid.(Pedants: I'm aware that the official distinction in C is between automatic and non-automatic storage.)
What you wrote is not the same in C and Go, because GC and escape analysis. But 9rx is also correct that what OP wrote is the same in C and Go.
So OP almost learned about escape analysis, but their example didn't actually do it. So double confusion on their side.
Assuming to everything allocates on the heap, will solve this specific confusion.
My understanding is that C will let you crash quite fast if the stack becomes too large, go will dynamically grow the stack as needed. So it's possible to think you're working on the heap, but you are actually threshing the runtime with expensive stack grow calls. Go certainly tries to be smart about it with various strategies, but a rapid stack grow rate will have it's cost.
Depends what you mean by “large”. As of 1.24 Go will put slices several KB into the stack frame:
make([]byte, 65536)
Goes on the stack if it does not escape (you can see Go request a large stack frame) make([]byte, 65537)
goes on the heap (Go calls runtime.makeslice).Interestingly arrays have a different limit: they respect MaxStackVarSize, which was lowered from 10MB to 128 KB in 1.24.
If you use indexed slice literals gc does not even check and you can create megabyte-sized slices on the stack.
// -smallframes
// ir.MaxStackVarSize = 64 * 1024
// ir.MaxImplicitStackVarSize = 16 * 1024
a := [64 * 1024 +1]byte{}
b := make([]byte, 0, 16 * 1024 +1)
// default
// MaxStackVarSize = int64(128 * 1024)
// MaxImplicitStackVarSize = int64(64 * 1024)
c := [128 * 1024 +1]byte{}
d := make([]byte, 0, 64 * 1024 +1)
Not sure how to verify this, but the assumption you can allocate megabytes on the stack seems wrong. The output of the escape analysis for arrays is different then the make statement: test/test.go:36:2: moved to heap: c
Maybe an overlook because it is a bit sneaky?The initial stack size seems to be 2kb, a more on a few systems. So far I understand you can allocate a large local i.e. 8kb, that doesn't escape and grow the stack immediately. (Of course that adds up if you have a chain of calls with smaller allocs). So recursion is certainly not the only concern.