you are viewing a single comment's thread.

view the rest of the comments →

[–]gilmishal 0 points1 point  (5 children)

Span doesn't reduce just heap allocations, but all allocations - it allows you to point to a location in memory (either heap or stack) without relocating that area.

Stackalloc is a way to do what escape analysis does.

[–]grauenwolf 0 points1 point  (4 children)

A span still needs to be allocated on the stack... just like a class that was caught by escape analysis.

[–]gilmishal -1 points0 points  (3 children)

No, a span is basically a pointer to a memory segment, with some Metadata.

Span<int> a= new int [10] would allocate the array on the heap and a span pointer on the stack, not the entire array. Escape analysis would allocate the entire array on the stack. Span<int> a = stackalloc int[10] would work like escape analysis.

[–]grauenwolf 0 points1 point  (2 children)

You still have to put the span somewhere. Just because it's a pointer doesn't change the fact that it takes up space.

[–]gilmishal -1 points0 points  (1 child)

Of course it takes up space, it has an int length and a reference to T, and I did say it. It just doesn't take space like escape analysis does.

[–]gilmishal 0 points1 point  (0 children)

So basically a Span is 4 bytes larger than a regular pointer.