Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Consolidate System.Memory code to shared folder - #26393

Merged
tarekgh merged 5 commits into
dotnet:masterfrom
tarekgh:ConsolidateSystemMemoryCodeToSharedFolder
Jan 18, 2018
Merged

Consolidate System.Memory code to shared folder#26393
tarekgh merged 5 commits into
dotnet:masterfrom
tarekgh:ConsolidateSystemMemoryCodeToSharedFolder

Conversation

@tarekgh

@tarekgh tarekgh commented Jan 17, 2018

Copy link
Copy Markdown
Member

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.

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.
@tarekgh

tarekgh commented Jan 17, 2018

Copy link
Copy Markdown
Member Author

@ahsonkhan @jkotas could you please have a look?

CC @joshfree @KrzysztofCwalina @GrabYourPitchforks @atsushikan

@tarekgh

tarekgh commented Jan 17, 2018

Copy link
Copy Markdown
Member Author

@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.

@safern

safern commented Jan 17, 2018

Copy link
Copy Markdown
Member

Ok. I will keep an eye.

}
else if (typeof(T) == typeof(char) && _object is string s)
{
#if CORECLR || CORERT

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/Common/src/CoreLib/System/Memory.cs Outdated
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.ownedMemory);
if (index < 0 || length < 0)
ThrowHelper.ThrowArgumentOutOfRangeException();
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not have argument called start.

Comment thread src/Common/src/CoreLib/System/Memory.cs Outdated
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
if (default(T) == null && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();
ThrowHelper.ThrowArrayTypeMismatchException_ArrayTypeMustBeExactMatch(typeof(T));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing in typeof(T) has non-trivial overhead here. I do not think it is worth doing for this rare exception.

Comment thread src/Common/src/CoreLib/System/Memory.cs Outdated
namespace System
{
/// <summary>
/// Memory represents a contiguous region of arbitrary memory similar to Span.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add xml comments here as well.

@ahsonkhan

Copy link
Copy Markdown

Would this using directive work for portable span or does it need to be within ifdef?
https://github.com/dotnet/corefx/pull/26393/files#diff-85c2810af721b319d724938d5506641dR11

 using Internal.Runtime.CompilerServices;

@jkotas

jkotas commented Jan 17, 2018

Copy link
Copy Markdown
Member

using Internal.Runtime.CompilerServices; needs to be ifdefed.

<Compile Include="..\Common\src\System\MutableDecimal.cs" />
</ItemGroup>
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true'">
<Compile Include="$(CommonPath)\CoreLib\System\System\Memory.cs" />

@ahsonkhan ahsonkhan Jan 17, 2018

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra \System within the paths

here and elsewhere

should be:
"$(CommonPath)\CoreLib\System\Memory.cs"

@ahsonkhan ahsonkhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise, lgtm

@tarekgh

tarekgh commented Jan 17, 2018

Copy link
Copy Markdown
Member Author

Thanks @ahsonkhan for your review.

@jkotas I have addressed all your comments. please let me know if you have more comments. Thanks.

Comment thread src/Common/src/CoreLib/System/Memory.cs Outdated
if (index < 0 || length < 0)
ThrowHelper.ThrowArgumentOutOfRangeException();
if (index < 0)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ahsonkhan ahsonkhan Jan 17, 2018

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there needs to be a condition for this constant (either IsPartialFacadeAssembly != true, or use the existing netstandard).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/Common/src/CoreLib/System/Memory.cs Outdated
if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
{
ThrowHelper.ThrowArgumentOutOfRangeException();
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the code I picked from the System.Memory in corefx but I agree with you. I'll fix that.

@tarekgh

tarekgh commented Jan 17, 2018

Copy link
Copy Markdown
Member Author

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" />

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we introduce a variable for the shared folder? e.g. $(SharedPath) or $(SharedCommonPath)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it is worth it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It's good that we talked about it.

@tarekgh

tarekgh commented Jan 17, 2018

Copy link
Copy Markdown
Member Author

@jkotas any more comments?

Comment thread src/Common/src/CoreLib/System/Memory.cs Outdated
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.ownedMemory);
if (index < 0 || length < 0)
ThrowHelper.ThrowArgumentOutOfRangeException();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Extra line

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo nit

@tarekgh

tarekgh commented Jan 17, 2018

Copy link
Copy Markdown
Member Author

#25188

@tarekgh
tarekgh merged commit 9340e12 into dotnet:master Jan 18, 2018
@tarekgh
tarekgh deleted the ConsolidateSystemMemoryCodeToSharedFolder branch January 18, 2018 00:40
dotnet-bot pushed a commit to dotnet/coreclr that referenced this pull request Jan 18, 2018
* 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>
dotnet-bot added a commit to dotnet/corert that referenced this pull request Jan 18, 2018
* 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>
tarekgh pushed a commit to dotnet/coreclr that referenced this pull request Jan 18, 2018
* 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
jkotas pushed a commit to dotnet/corert that referenced this pull request Jan 19, 2018
* 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>
@karelz karelz added this to the 2.1.0 milestone Jan 20, 2018
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants