Consolidate System.Memory code to shared folder - #26393
Conversation
This change is removing the duplicate codes from System.Memory and keep only one copy under the shared folder to be easier to edit such code in one place and get reflected on the other repos.
|
@ahsonkhan @jkotas could you please have a look? CC @joshfree @KrzysztofCwalina @GrabYourPitchforks @atsushikan |
|
@safern this change will break coreclr when get mirrored. so, please ping me when the mirroring PR get opened so I can fix the breaks before we merge it there. I expect this break on corert too. |
|
Ok. I will keep an eye. |
| } | ||
| else if (typeof(T) == typeof(char) && _object is string s) | ||
| { | ||
| #if CORECLR || CORERT |
There was a problem hiding this comment.
We should have a different ifdef for this so that this is just one condition.
It can be #if netstandard or #if FEATURE_PORTABLE_SPAN. We have prior art for both these.
| ThrowHelper.ThrowArgumentNullException(ExceptionArgument.ownedMemory); | ||
| if (index < 0 || length < 0) | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(); | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); |
There was a problem hiding this comment.
This does not have argument called start.
| ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); | ||
| if (default(T) == null && array.GetType() != typeof(T[])) | ||
| ThrowHelper.ThrowArrayTypeMismatchException(); | ||
| ThrowHelper.ThrowArrayTypeMismatchException_ArrayTypeMustBeExactMatch(typeof(T)); |
There was a problem hiding this comment.
Passing in typeof(T) has non-trivial overhead here. I do not think it is worth doing for this rare exception.
| namespace System | ||
| { | ||
| /// <summary> | ||
| /// Memory represents a contiguous region of arbitrary memory similar to Span. |
There was a problem hiding this comment.
nit: Use decorator tag cref when referring to Span, similar to the comment in ReadOnlyMemory.cs
/// <summary>
/// Represents a contiguous region of memory, similar to <see cref="Span{T}"/>.
/// Unlike <see cref="Span{T}"/>, it is not a byref-like type.
/// </summary>
|
|
||
|
|
||
| #if CORECLR || CORERT | ||
| public static ref T GetReference<T>(Span<T> span) => ref span._pointer.Value; |
|
Would this using directive work for portable span or does it need to be within ifdef? using Internal.Runtime.CompilerServices; |
|
|
| <Compile Include="..\Common\src\System\MutableDecimal.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true'"> | ||
| <Compile Include="$(CommonPath)\CoreLib\System\System\Memory.cs" /> |
There was a problem hiding this comment.
extra \System within the paths
here and elsewhere
should be:
"$(CommonPath)\CoreLib\System\Memory.cs"
|
Thanks @ahsonkhan for your review. @jkotas I have addressed all your comments. please let me know if you have more comments. Thanks. |
| if (index < 0 || length < 0) | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(); | ||
| if (index < 0) | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); |
There was a problem hiding this comment.
This internal constructor and the callers are validating the arguments already. Do we need to validate the arguments here again?
These constructors are hot paths, with AggresiveInline. Any unnecessary cruft here impacts microbencmarks; and has multiplicative effect on code size.
There was a problem hiding this comment.
Context for the checks being there, from @stephentoub:
https://github.com/dotnet/coreclr/pull/13583/files/f538e879ed80d82c155270796d2ab20bdd63dbe5#r135248195
There was a problem hiding this comment.
I am seeing there is no strong opinion to keep the checks even in the comment that @ahsonkhan pointed at. do you agree with removing the checks (for index and length)?
There was a problem hiding this comment.
It is odd to have a partial argument validation. I think we should do no validation here, and let the callers take care of it as necessary.
| <DefineConstants Condition="'$(IsPartialFacadeAssembly)' != 'true'">$(DefineConstants);netstandard</DefineConstants> | ||
| <DefineConstants Condition="'$(TargetGroup)'=='netcoreapp'">$(DefineConstants);netcoreapp</DefineConstants> | ||
| <DefineConstants Condition="'$(TargetGroup)'=='netstandard1.1'">$(DefineConstants);netstandard11</DefineConstants> | ||
| <DefineConstants>$(DefineConstants);FEATURE_PORTABLE_SPAN</DefineConstants> |
There was a problem hiding this comment.
I think there needs to be a condition for this constant (either IsPartialFacadeAssembly != true, or use the existing netstandard).
There was a problem hiding this comment.
I thought about that but found we include only the needed files when building the portable library. I can add the condition but I am not seeing this is necessary. I'll add the condition anyway.
| if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start)) | ||
| { | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(); | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); |
There was a problem hiding this comment.
The argument name is not accurate - it can be length that is wrong. I think we should not bother with supplying the argument name here.
There was a problem hiding this comment.
this is the code I picked from the System.Memory in corefx but I agree with you. I'll fix that.
|
The failures are known tracked issue https://github.com/dotnet/corefx/issues/26382 |
| <Compile Include="..\Common\src\System\MutableDecimal.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true'"> | ||
| <Compile Include="$(CommonPath)\CoreLib\System\Memory.cs" /> |
There was a problem hiding this comment.
should we introduce a variable for the shared folder? e.g. $(SharedPath) or $(SharedCommonPath)
There was a problem hiding this comment.
I am not sure if it is worth it.
There was a problem hiding this comment.
Replacing all existing paths starting with $(CommonPath)\CoreLib works if we ever change the place of the mirror. So I'm fine with hardcoding it.
There was a problem hiding this comment.
All these path variables just makes it harder for people to find the files. It's not like msbuild supports "hitting F12 to go to the definition of path variable."
Not a good tradeoff for the remote chance that the mirror location will change, especially since search-replace will work just fine in that case.
There was a problem hiding this comment.
I agree. It's good that we talked about it.
|
@jkotas any more comments? |
| ThrowHelper.ThrowArgumentNullException(ExceptionArgument.ownedMemory); | ||
| if (index < 0 || length < 0) | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(); | ||
|
|
There was a problem hiding this comment.
It may be useful to add // No validation performed; caller must provide any necessary validation. comment here - similar to what it is in the other ctor.
|
#25188 |
* Consolidate System.Memory code to shared folder This change is removing the duplicate codes from System.Memory and keep only one copy under the shared folder to be easier to edit such code in one place and get reflected on the other repos. * Address the review feedback * Addressing more feedback * More cleanup * remove empty line and added a comment Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
* Consolidate System.Memory code to shared folder (dotnet/corefx#26393) * Consolidate System.Memory code to shared folder This change is removing the duplicate codes from System.Memory and keep only one copy under the shared folder to be easier to edit such code in one place and get reflected on the other repos. * Address the review feedback * Addressing more feedback * More cleanup * remove empty line and added a comment Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com> * Add missing throw helper methods used in the code we got from corefx * Update the exception helper * fix the break Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
* Consolidate System.Memory code to shared folder (dotnet/corefx#26393) * Consolidate System.Memory code to shared folder This change is removing the duplicate codes from System.Memory and keep only one copy under the shared folder to be easier to edit such code in one place and get reflected on the other repos. * Address the review feedback * Addressing more feedback * More cleanup * remove empty line and added a comment Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com> * Add missing throw helper methods used in the code we got from corefx * Update the exception helper * fix the break
* Consolidate System.Memory code to shared folder (dotnet/corefx#26393) * Consolidate System.Memory code to shared folder This change is removing the duplicate codes from System.Memory and keep only one copy under the shared folder to be easier to edit such code in one place and get reflected on the other repos. * Address the review feedback * Addressing more feedback * More cleanup * remove empty line and added a comment Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com> * Add missing throw helper methods used in the code we got from corefx * Update the exception helper * fix the break Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
* Consolidate System.Memory code to shared folder This change is removing the duplicate codes from System.Memory and keep only one copy under the shared folder to be easier to edit such code in one place and get reflected on the other repos. * Address the review feedback * Addressing more feedback * More cleanup * remove empty line and added a comment Commit migrated from dotnet/corefx@9340e12
This change is removing the duplicate codes from System.Memory and keep only one copy under the shared folder to be easier to edit such code in one place and get reflected on the other repos.