Repository navigation
Misc Span/Memory changes - #15941
Misc Span/Memory changes#15941
Conversation
| /// <summary> | ||
| /// Returns a <see cref="String"/> with the name of the type and the number of elements | ||
| /// </summary> | ||
| /// <returns>A <see cref="String"/> with the name of the type and the number of elements</returns> |
There was a problem hiding this comment.
nit: Is the returns xml tag necessary? It seems redundant with whatever has been stated in the summary anyway.
There was a problem hiding this comment.
It's definitely redundant. I added it because I thought that intellisense may do something useful with the returns tag, but I couldn't actually find a case where it's used without the summary. I'll remove it.
| /// Thrown if the Length property of the new Span would exceed Int32.MaxValue. | ||
| /// </exception> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static ReadOnlySpan<TTo> Cast<TFrom, TTo>(ReadOnlySpan<TFrom> source) |
There was a problem hiding this comment.
Thanks, this should be casting Span
| /// Thrown if the Length property of the new Span would exceed Int32.MaxValue. | ||
| /// </exception> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static ReadOnlySpan<TTo> Cast<TFrom, TTo>(ReadOnlySpan<TFrom> source) |
There was a problem hiding this comment.
Same here. This looks like a duplicate.
| ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(TTo)); | ||
|
|
||
| return new ReadOnlySpan<TTo>( | ||
| ref Unsafe.As<TFrom, TTo>(ref MemoryMarshal.GetReference(source)), |
There was a problem hiding this comment.
This can use _pointer.Value directly. No need to go through GetReference wrapper.
|
It was not clear to me from the discussions that we are adding Otherwise LGTM - once you make the CI green. |
From our API review today: [starts at 13:30] https://youtu.be/Qcqn-yv1X0o?t=13m30s |
| /// <param name="length">The number of <typeparamref name="T"/> elements the memory contains.</param> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public static ReadOnlySpan<T> DangerousCreate(object obj, ref T objectData, int length) => new ReadOnlySpan<T>(ref objectData, length); |
There was a problem hiding this comment.
At least from my experience, it's easier to stage changes like this across coreclr/corefx if when changing a method's name/location, you first add the new method in coreclr, let that propagate to corefx and fix everything up, and then go back and delete the old method. Otherwise, there's a period of time where coreclr and corefx don't work together, and if there's any other issue happening at the same time, you may not be able to fix things quickly.
There was a problem hiding this comment.
Great idea! I wasn't too pleased with the broken in-between phase. I'll do that.
| /// Returns a <see cref="String"/> with the name of the type and the number of elements | ||
| /// </summary> | ||
| /// <returns>A <see cref="String"/> with the name of the type and the number of elements</returns> | ||
| public override string ToString() => $"System.Span[{Length}]"; |
There was a problem hiding this comment.
Performance here isn't important, but FYI this is more expensive than just:
"System.Span[" + Length.ToString() + "]";The string interpolation version will box Length, will create an array to store that boxed length, and then will end up calling string.Format, whereas the above allocates the string for length and then calls String.Concat(string, string, string).
| return new ReadOnlySpan<TTo>(Unsafe.As<Pinnable<TTo>>(source.Pinnable), source.ByteOffset, newLength); | ||
| } | ||
|
|
||
| #else |
There was a problem hiding this comment.
Why do we have both the portable and fast versions in the same file? Can we split this into separate files like we do elsewhere?
There was a problem hiding this comment.
Sure. I'll have to temporarily have four files to prevent corefx from breaking before the accompanying PR there is merged:
- MemoryMarshal.Fast.cs
- MemoryMarshal.Portable.cs
- MemoryMarshal.Shared.cs (only the few functions that are the same for both fast/portable)
- MemoryMarshal.cs (old file without changes)
There was a problem hiding this comment.
Why do you need to have MemoryMarshal.Shared.cs file? I think these can be just be added to the existing MemoryMarshal.cs file.
There was a problem hiding this comment.
The purpose of the Shared file is to make it so that merging the coreclr PR won't cause CoreFX to break. To do that, we need to mostly leave the existing MemoryMarshal file intact, which means we can't include it in coreclr if we also have a MemoryMarshal.Fast that contains some of the signatures that are currently living in MemoryMarshal.
There was a problem hiding this comment.
There are two cases:
- You are adding new APIs (including adding overloads of existing APIs with new signatures). In this case, you can just add it to MemoryMarshal.cs
- You add modifying existing APIs. In this case, you will need to augment the mirror PR in CoreFX to get it through.
MemoryMarshal.Shared.csfile is not going to help you with that.
What I am missing?
There was a problem hiding this comment.
You add modifying existing APIs. In this case, you will need to augment the mirror PR in CoreFX to get it through. MemoryMarshal.Shared.cs file is not going to help you with that.
The way I have it currently set up, I'm not actually modifying any existing APIs, I'm only adding new API, so the mirror PR won't be broken.
You are adding new APIs (including adding overloads of existing APIs with new signatures). In this case, you can just add it to MemoryMarshal.cs
If I add the new API to MemoryMarshal, then coreclr needs to include MemoryMarshal, which means I'll need to remove the Fast-Specific section from MemoryMarshal to avoid conflicts with MemoryMarshal.Fast.cs, which means that the mirror PR to corefx will be broken for the non-portable builds unless I modify the mirror PR to include the MemoryMarshal.Fast file.
I was trying to avoid having to make fixes to the mirror PR, but if you're strongly against the Shared suffix, then I can. I'll just fix the mirror PR to include the new MM.Fast and MM.Portable files to get the build green then keep my feature CoreFX PR the same minus the new file inclusions.
There was a problem hiding this comment.
you're strongly against the Shared suffix
Yes, I am strongly against adding temporary files with .Shared suffix. It just means more round trips between repos to clean things up.
mirror PR to corefx will be broken for the non-portable builds
We should expect that you need to patch up the mirror PR for changes like these.
| /// <typeparam name="T">The element type of the <paramref name="memory" />.</typeparam> | ||
| /// <param name="memory">The Memory to view as an <see cref="IEnumerable{T}"/></param> | ||
| /// <returns>An <see cref="IEnumerable{T}"/> view of the given <paramref name="memory" /></returns> | ||
| public static IEnumerable<T> ToEnumerable<T>(Memory<T> memory) |
There was a problem hiding this comment.
Does this end up having the desired optimization? I would hope so, as Environment.CurrentManagedThreadId is available to it, but the Environment it has access to is also internal, so we should verify that's not throwing off the compiler.
There was a problem hiding this comment.
@stephentoub Environment.CurrentManagedThreadId is not used here. Was this meant for something else?
There was a problem hiding this comment.
It's used by the compiler's implementation of iterators, and the compiler keys off its presence to determine whether to take the optimization that reuses the enumerable as the enumerator.
There was a problem hiding this comment.
(I expect it's fine though, as corelib already has other iterators in it, and they look to compile "correctly".)
There was a problem hiding this comment.
I checked the IL and confirmed that it is getting compiled correctly with the use of System.Environment::get_CurrentManagedThreadId()
| /// <typeparam name="T">The element type of the <paramref name="memory" />.</typeparam> | ||
| /// <param name="memory">The Memory to view as an <see cref="IEnumerable{T}"/></param> | ||
| /// <returns>An <see cref="IEnumerable{T}"/> view of the given <paramref name="memory" /></returns> | ||
| public static IEnumerable<T> ToEnumerable<T>(Memory<T> memory) |
There was a problem hiding this comment.
Should this be for ReadOnlyMemory<T> instead of for Memory<T>?
There was a problem hiding this comment.
Seems like it might as well be, yeah.
- Add MemoryMarshal.CreateSpan to replace DangerousCreate - Add MemoryMarshal.CreateReadOnlySpan to replace DangerousCreate - Add MemoryMarshal.Cast to replace NonPortableCast - Add ToString override to Span and ReadOnlySpan - Add ToEnumerable function to MemoryMarshal that takes a ReadOnlyMemory
|
Thanks everyone for the feedback. I force pushed a new commit that addresses of all it and also leaves the existing functions in place to ease the intermediate stage per @stephentoub 's suggestion. |
| <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MarshalDirectiveException.cs" /> | ||
| <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MemoryMarshal.cs" /> | ||
| <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MemoryMarshal.Fast.cs" /> | ||
| <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MemoryMarshal.Shared.cs" /> |
There was a problem hiding this comment.
This should stay MemoryMarshal.cs to follow our naming conventions.
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
There was a problem hiding this comment.
MemoryMarshal.Portable.cs does not need to be in the shared CoreLib subtree. It can stay in the CoreFX-specific subtree.
There was a problem hiding this comment.
There was a problem hiding this comment.
Good point, removed. I'll add it back to CoreFX in the System.Memory project.
* Misc Span/Memory additions - Add MemoryMarshal.CreateSpan to replace DangerousCreate - Add MemoryMarshal.CreateReadOnlySpan to replace DangerousCreate - Add MemoryMarshal.Cast to replace NonPortableCast - Add ToString override to Span and ReadOnlySpan - Add ToEnumerable function to MemoryMarshal that takes a ReadOnlyMemory Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
* Misc Span/Memory additions - Add MemoryMarshal.CreateSpan to replace DangerousCreate - Add MemoryMarshal.CreateReadOnlySpan to replace DangerousCreate - Add MemoryMarshal.Cast to replace NonPortableCast - Add ToString override to Span and ReadOnlySpan - Add ToEnumerable function to MemoryMarshal that takes a ReadOnlyMemory Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
* Misc Span/Memory additions - Add MemoryMarshal.CreateSpan to replace DangerousCreate - Add MemoryMarshal.CreateReadOnlySpan to replace DangerousCreate - Add MemoryMarshal.Cast to replace NonPortableCast - Add ToString override to Span and ReadOnlySpan - Add ToEnumerable function to MemoryMarshal that takes a ReadOnlyMemory Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
* Misc Span/Memory additions - Add MemoryMarshal.CreateSpan to replace DangerousCreate - Add MemoryMarshal.CreateReadOnlySpan to replace DangerousCreate - Add MemoryMarshal.Cast to replace NonPortableCast - Add ToString override to Span and ReadOnlySpan - Add ToEnumerable function to MemoryMarshal that takes a ReadOnlyMemory Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
progress towards https://github.com/dotnet/corefx/issues/26368, https://github.com/dotnet/corefx/issues/24854, https://github.com/dotnet/corefx/issues/26295, https://github.com/dotnet/corefx/issues/26139
Note that this includes breaking changes to any upstream projects using these functions. This means it will break CoreFX builds dependent on any new coreclr produced until an accompanying PR in dotnet/corefx/ is merged.