From: Ahson Khan Date: Mon, 9 Apr 2018 22:49:04 +0000 (-0700) Subject: Fix MemoryManager ctor and use internal span ctor to improve performance (#17452) X-Git-Tag: accepted/tizen/unified/20190422.045933~2371 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=09ea3a952e4c9df4b572f9c76e8e58f052f6f18f;p=platform%2Fupstream%2Fcoreclr.git Fix MemoryManager ctor and use internal span ctor to improve performance (#17452) * Fix MemoryManager ctor, add unit and perf tests, and use internal span ctor. * Address PR feedback (remove use of Unsafe.As and Dangerous Span Ctor) --- diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index 6eb5af6..3ccb76b 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -120,7 +120,9 @@ namespace System /// The memory manager. /// The index at which to begin the memory. /// The number of items in the memory. - /// Returns default when is null. + /// + /// Thrown when is null. + /// /// /// Thrown when the specified or end index is not in the range (<0 or >=Length). /// @@ -128,12 +130,7 @@ namespace System public Memory(MemoryManager manager, int start, int length) { if (manager == null) - { - if (start != 0 || length != 0) - ThrowHelper.ThrowArgumentOutOfRangeException(); - this = default; - return; // returns default - } + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.manager); if ((uint)start > (uint)manager.Length || (uint)length > (uint)(manager.Length - start)) ThrowHelper.ThrowArgumentOutOfRangeException(); @@ -276,6 +273,7 @@ namespace System if (_index < 0) { Debug.Assert(_length >= 0); + Debug.Assert(_object != null); return ((MemoryManager)_object).GetSpan().Slice(_index & RemoveFlagsBitMask, _length); } else if (typeof(T) == typeof(char) && _object is string s) @@ -335,6 +333,7 @@ namespace System { if (_index < 0) { + Debug.Assert(_object != null); return ((MemoryManager)_object).Pin((_index & RemoveFlagsBitMask)); } else if (typeof(T) == typeof(char) && _object is string s) diff --git a/src/mscorlib/shared/System/ReadOnlyMemory.cs b/src/mscorlib/shared/System/ReadOnlyMemory.cs index ca0e7d8..3e78845 100644 --- a/src/mscorlib/shared/System/ReadOnlyMemory.cs +++ b/src/mscorlib/shared/System/ReadOnlyMemory.cs @@ -187,6 +187,7 @@ namespace System if (_index < 0) { Debug.Assert(_length >= 0); + Debug.Assert(_object != null); return ((MemoryManager)_object).GetSpan().Slice(_index & RemoveFlagsBitMask, _length); } else if (typeof(T) == typeof(char) && _object is string s) @@ -241,6 +242,7 @@ namespace System { if (_index < 0) { + Debug.Assert(_object != null); return ((MemoryManager)_object).Pin((_index & RemoveFlagsBitMask)); } else if (typeof(T) == typeof(char) && _object is string s) diff --git a/src/mscorlib/src/System/ThrowHelper.cs b/src/mscorlib/src/System/ThrowHelper.cs index eb6a638..b1a2fec 100644 --- a/src/mscorlib/src/System/ThrowHelper.cs +++ b/src/mscorlib/src/System/ThrowHelper.cs @@ -479,7 +479,8 @@ namespace System comparable, source, state, - comparisonType + comparisonType, + manager } //