You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Can we improve ROS<byte>.ToArray from RVA static? #68103
When doing new byte[] { const, const, const, ... }, the JIT generates code that calls the new array helper and then emits an unrolled loop to copy the data into the array. When doing ((ReadOnlySpan<byte>)new byte[] { const, const, const, ... }).ToArray(), the copy is handled via ToArray's call to Buffer.MemMove. Is it feasible to do better?
If understand this correctly you mean that:
If JIT sees that a span was constructed from an array (full length) it can replace Span.ToArray() to just return the initial array it was created from? JIT optimizations for spans are very hard to do - they're quite verbose in the IR 😞 E.g. for Explicit():
Yes, except its not actually an array, so not that it would return the original object but rather directly copy the rva static data as happens in the RuntimeHelpers.InitializeArray Implicit case.
I'm asking because if the natural type of the new u8 literal in C# becomes ReadOnlySpan<byte>, Explicit is the pattern you'll get for e.g. "abcd"u8.ToArray().
Yes, except its not actually an array, so not that it would return the original object but rather directly copy the rva static data as happens in the RuntimeHelpers.InitializeArray Implicit case.
I'm asking because if the natural type of the new u8 literal in C# becomes ReadOnlySpan<byte>, Explicit is the pattern you'll get for e.g. "abcd"u8.ToArray().
I see. Honestly speaking I'd rather optimize this pattern in Roslyn or ILLink substep 🙁 I don't see an easy way to do it in JIT, perhaps someone from @dotnet/jit-contrib does?
if the natural type of the new u8 literal in C# becomes ReadOnlySpan<byte>
Roslyn previewed this feature with ReadOnlySpan<byte>. If it is still being finalized, isReadOnlySpan<System.Text.Rune> option in the cards for u8 suffix? 🙂
Ah, u32. Yes there will be a lot of overhead. Way too expensive than multi-byte characters directly encoded to byte array. 😁
I was originally thinking ROS<char> to UTF16 string ("..") could be same as ROS<Rune> to UTF8 string (".."u8), i.e. both being strongly-typed. The compiler could emit new Rune(const).Utf8SequenceLength / new Rune(const).TryEncodeToUtf8() calls in the IL, the runtime could then extract UTF8 bytes (at least for literal strings) in the jitinterface and something similar in mono. (it would also improve codegen of constantRune.Utf8SequenceLength and constantRune.TryEncodeToUtf8() more generally)
I see. Honestly speaking I'd rather optimize this pattern in Roslyn or ILLink substep 🙁 I don't see an easy way to do it in JIT, perhaps someone from @dotnet/jit-contrib does?
It does look nice and optimizable after assertion prop:
N007 ( 18, 20) [000141] --CXG-------▌CALL void System.Buffer.Memmove $VN.VoidN004 ( 1, 1) [000124] ------------ arg0 in rcx ├──▌LCL_VAR byref V06 tmp5 u:2 (last use) $280N005 ( 2, 10) [000190] H----------- arg1 in rdx ├──▌CNS_INT(h) long 0x142d5bf20a0 static $140N006 ( 1, 1) [000191] ------------ arg2 in r8 └──▌CNS_INT long 16 $81
But doing a second pass of morph-like optimizations here would be new territory.
I see. Honestly speaking I'd rather optimize this pattern in Roslyn or ILLink substep 🙁 I don't see an easy way to do it in JIT, perhaps someone from @dotnet/jit-contrib does?
It does look nice and optimizable after assertion prop:
N007 ( 18, 20) [000141] --CXG-------▌CALL void System.Buffer.Memmove $VN.VoidN004 ( 1, 1) [000124] ------------ arg0 in rcx ├──▌LCL_VAR byref V06 tmp5 u:2 (last use) $280N005 ( 2, 10) [000190] H----------- arg1 in rdx ├──▌CNS_INT(h) long 0x142d5bf20a0 static $140N006 ( 1, 1) [000191] ------------ arg2 in r8 └──▌CNS_INT long 16 $81
But doing a second pass of morph-like optimizations here would be new territory.
Activity
EgorBo commentedon Apr 16, 2022
If understand this correctly you mean that:
If JIT sees that a span was constructed from an array (full length) it can replace Span.ToArray() to just return the initial array it was created from? JIT optimizations for spans are very hard to do - they're quite verbose in the IR 😞 E.g. for
Explicit():Trees after Morph - Global
stephentoub commentedon Apr 16, 2022
Yes, except its not actually an array, so not that it would return the original object but rather directly copy the rva static data as happens in the RuntimeHelpers.InitializeArray Implicit case.
I'm asking because if the natural type of the new u8 literal in C# becomes
ReadOnlySpan<byte>, Explicit is the pattern you'll get for e.g."abcd"u8.ToArray().EgorBo commentedon Apr 16, 2022
I see. Honestly speaking I'd rather optimize this pattern in Roslyn or ILLink substep 🙁 I don't see an easy way to do it in JIT, perhaps someone from @dotnet/jit-contrib does?
stephentoub commentedon Apr 16, 2022
That's fair.
am11 commentedon Apr 16, 2022
Roslyn previewed this feature with
ReadOnlySpan<byte>. If it is still being finalized, isReadOnlySpan<System.Text.Rune>option in the cards foru8suffix? 🙂stephentoub commentedon Apr 16, 2022
In the prototype the natural type is actually byte[].
Each rune is 32 bits and would negate many of the intended uses of having cheap, direct access to UTF-8 bytes.
Or did you mean as an additional target type rather than instead of?
am11 commentedon Apr 16, 2022
Ah, u32. Yes there will be a lot of overhead. Way too expensive than multi-byte characters directly encoded to byte array. 😁
I was originally thinking
ROS<char>to UTF16 string ("..") could be same asROS<Rune>to UTF8 string (".."u8), i.e. both being strongly-typed. The compiler could emitnew Rune(const).Utf8SequenceLength/new Rune(const).TryEncodeToUtf8()calls in the IL, the runtime could then extract UTF8 bytes (at least for literal strings) in the jitinterface and something similar in mono. (it would also improve codegen ofconstantRune.Utf8SequenceLengthandconstantRune.TryEncodeToUtf8()more generally)jakobbotsch commentedon Apr 16, 2022
It does look nice and optimizable after assertion prop:
But doing a second pass of morph-like optimizations here would be new territory.
EgorBo commentedon Apr 16, 2022
Ah, indeed, I didn't think of it this way, nice!
EgorBo commentedon Mar 26, 2023
closed via #83638