UTF8 string literals as ReadonlyMemory<byte> #9493
Replies: 2 comments 15 replies
-
If a same sequence is likely to be produced in common paths, then a |
Beta Was this translation helpful? Give feedback.
-
Problem: TL;DR: Turning RVA into a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Allow no-allocation
ReadonlyMemory<byte>
typed utf8 string literals.Motivation:
While utf8 string literals as ReadonlySpan are great in data-consuming scenarios s.a. parsing that usually happens within the context of a single non-async method, they are not usable when it comes to producing data. At least not in network services environment, where everything is async.
The issue isn't new. It is explicitly mentioned in the lang specification for this feature / proposal but its solution is less than ideal.
The usage penalty is IMHO not the problem. The problem is that even though there's no need for allocations - as the backing byte array has forever lifetime...
...due to he restrictions of ReadOnlySpan type, it cannot be stored in normal classes, it cannot participate in async functions etc. And .ToArray() even though it wouldn't need to, allocates. And from my testing JIT in .NET 9 doesn't seem to optimize that copy/allocaiton away.
^^ this allocates
Solution
I don't have any clear solution. I'm not sure if it's possible to add new natural type ReadonlyMemory in this case for utf8 literals, and have it choose based on the target type. But if it was, it'd be one way (I understand ReadonlySpan would need to remain default not to break stuff).
Beta Was this translation helpful? Give feedback.
All reactions