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

Return empty array if length is zero#16529

Merged
ahsonkhan merged 7 commits into
masterfrom
unknown repository
Feb 24, 2018
Merged

Return empty array if length is zero#16529
ahsonkhan merged 7 commits into
masterfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Feb 23, 2018

Comment thread src/mscorlib/shared/System/Memory.cs Outdated
if (_length == 0)
{
#if FEATURE_PORTABLE_SPAN
arraySegment = new ArraySegment<T>(Array<T>.Empty);
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 do not think Array<T>.Empty is portable. It should be SpanHelpers.PerTypeValues<T>.EmptyArray

Comment thread src/mscorlib/shared/System/Memory.cs Outdated
arraySegment = ArraySegment<T>.Empty;
#endif // FEATURE_PORTABLE_SPAN
return true;
}
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.

Is doing this check here what we want? Seems like it should instead be done at the end, just before returning false, so that we still call through an OwnedMemory if there is one, and preserve array object identity if this wraps an array, in case that's needed in some situation.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Moved to bottom.

@stephentoub
Copy link
Copy Markdown
Member

Resolves

As part of resolving this, we did also add tests in corefx.

Also, do we want to make a similar change to MemoryMarshal.TryGetArray?

@stephentoub
Copy link
Copy Markdown
Member

stephentoub commented Feb 24, 2018

cc: @ahsonkhan , @KrzysztofCwalina , @davidfowl

Comment thread src/mscorlib/shared/System/Memory.cs Outdated
arraySegment = new ArraySegment<T>(arr, _index, _length);
return true;
}
else if (_length == 0)
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.

With "else", this won't kick in for an owned memory that doesn't wrap an array. Is that what's desired?

@ghost
Copy link
Copy Markdown
Author

ghost commented Feb 24, 2018

@stephentoub, should I also make this change in CoreFX or will it be automatically moved? I can close this PR and add it in CoreFX with tests in one PR, instead of having two.

@stephentoub
Copy link
Copy Markdown
Member

should I also make this change in CoreFX or will it be automatically moved?

It'll be automatically mirrored.

I can close this PR and add it in CoreFX with tests in one PR, instead of having two.

No, keep this one. You won't be able to merge tests in corefx until it's using a coreclr build with the change.

@ghost
Copy link
Copy Markdown
Author

ghost commented Feb 24, 2018

I have added the case in MemoryMarshal and tests in CoreFX dotnet/corefx#27430

Copy link
Copy Markdown

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for fixing this.

Can you please build coreclr locally, build corefx using your local coreclr build and run the corefx tests you just added here (dotnet/corefx#27430) to confirm they pass?

See https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/developer-guide.md#testing-with-private-coreclr-bits for more details. If you have questions on how to do that, please feel free to ask.

@MarcoRossignoli
Copy link
Copy Markdown
Member

MarcoRossignoli commented Feb 24, 2018

@kasper3 if you get in trouble with cross tests check this, i hope to help.

@ahsonkhan ahsonkhan assigned ghost Feb 24, 2018
@ahsonkhan ahsonkhan merged commit c2481cd into dotnet:master Feb 24, 2018
dotnet-bot pushed a commit to dotnet/corert that referenced this pull request Feb 24, 2018
* Return empty array if length is zero

* Return true

* Add support for portable system

* Use portable span helper instead of Array.Empty

* Move to end

* Remove else

* Return empty array if length is 0 in MemoryMarshal

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
dotnet-bot pushed a commit to dotnet/corefx that referenced this pull request Feb 24, 2018
* Return empty array if length is zero

* Return true

* Add support for portable system

* Use portable span helper instead of Array.Empty

* Move to end

* Remove else

* Return empty array if length is 0 in MemoryMarshal

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
jkotas pushed a commit to dotnet/corert that referenced this pull request Feb 25, 2018
* Return empty array if length is zero

* Return true

* Add support for portable system

* Use portable span helper instead of Array.Empty

* Move to end

* Remove else

* Return empty array if length is 0 in MemoryMarshal

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
jkotas pushed a commit to dotnet/corefx that referenced this pull request Feb 25, 2018
* Return empty array if length is zero

* Return true

* Add support for portable system

* Use portable span helper instead of Array.Empty

* Move to end

* Remove else

* Return empty array if length is 0 in MemoryMarshal

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
kbaladurin pushed a commit to kbaladurin/corert that referenced this pull request Mar 15, 2018
* Return empty array if length is zero

* Return true

* Add support for portable system

* Use portable span helper instead of Array.Empty

* Move to end

* Remove else

* Return empty array if length is 0 in MemoryMarshal

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants