5 ms·
> C# can be written in a way that stack allocates almost everything (much like Rust) I'd be surprised if this works in practice because the String processing i
by tialaramex 5y ago
> C# can be written in a way that stack allocates almost everything (much like Rust)
I'd be surprised if this works in practice because the String processing in C# is surely going to keep making Heap objects ?
You're immediately in a better place here in Rust because people (sensibly) keep developing standards encoded in UTF-8 and so the bytes on the wire are Rust's built-in str type, whereas C# needs to turn them into UTF-16 and store that somewhere as a String.
- briHass 5y ago> I'd be surprised if this works in practice because the String processing in C# is surely going to keep making Heap objects ? The new Span<T>, specifically Span<char>, stuff allows you to slice and read strings without creating new objects, and string has both a c-tor for a (readonly) span and a Create method that takes a delegate that allows you to fill the string already allocated on the heap.
- tialaramex 5y agoI didn't know about Span<T>, but it makes sense to give C# slices somehow. However so far as I've seen while writing C# popular APIs want an actual String and they won't take these slices as a substitute? The ergonomics seem poor. Maybe I'm mistaken and magically my C# code which says "string" everywhere is actually converted at runtime to use Span<char> and go real fast? But it doesn't look like it. The Rust APIs really do take the slice reference &str, it's just normal in Rust to say &str unless you very specifically want a mutable String for some reason (e.g. you're going to insert stuff into it)
- dehrmann 5y agoI'm not sure if .NET can do this, but Java is similar, and it can do escape analysis to keep more things on the stack.