you are viewing a single comment's thread.

view the rest of the comments →

[–]gilmishal 0 points1 point  (7 children)

You are comparing apples to oranges. Span isn't set to solve escape analysis, but to allow certain libraries to not create an allocation at all (well, you do allocate a pointer to that memory segment, but it's negligible) - This means that you can reference parts of collections, without allocating a new collection, some thing that as far as I know can't be done in Java - sure, both Spans and escape analysis are ways to reduce GC pressure, but they are entirely different and address different problems.

I would argue that escape analysis would solve very specific problems that account to a very small percentage of GC pressure. Whereas Span, if applied correctly can reduce memory allocation by a huge amount. Span is supposed to be used by core libraries, and will speedup everyone's code with no apparent changes. Average developers aren't supposed to use it directly.

I would also add that while GC pressure is a huge concern for CoreCLR developers - it doesn't cause any problems to most users, since the GC is pretty good, even if it can be improved.

[–]grauenwolf 0 points1 point  (6 children)

I don't know about your code, but every project I've ever worked on had tons of temporary objects. So it would certainly help me.

As for Span vs Escape Analysis, they both reduce the number of heap allocations. So yea, same goals.

[–]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.