Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1579 from stephentoub/more_search_replace
Browse files Browse the repository at this point in the history
Cleanup a few more empty array allocations and Contract.Asserts
  • Loading branch information
ellismg committed Apr 30, 2015
2 parents 131b26f + f70d30a commit cd67f03
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 30 deletions.
12 changes: 5 additions & 7 deletions src/Common/src/System/Collections/Generic/LowLevelList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ internal class LowLevelList<T>
protected int _size;
protected int _version;

private static readonly T[] s_emptyArray = new T[0];

// Constructs a List. The list is initially empty and has a capacity
// of zero. Upon adding the first element to the list the capacity is
// increased to 16, and then increased in multiples of two as required.
public LowLevelList()
{
_items = s_emptyArray;
_items = Array.Empty<T>();
}

// Constructs a List with a given initial capacity. The list is
Expand All @@ -69,7 +67,7 @@ public LowLevelList(int capacity)
Contract.EndContractBlock();

if (capacity == 0)
_items = s_emptyArray;
_items = Array.Empty<T>();
else
_items = new T[capacity];
}
Expand All @@ -90,7 +88,7 @@ public LowLevelList(IEnumerable<T> collection)
int count = c.Count;
if (count == 0)
{
_items = s_emptyArray;
_items = Array.Empty<T>();
}
else
{
Expand All @@ -102,7 +100,7 @@ public LowLevelList(IEnumerable<T> collection)
else
{
_size = 0;
_items = s_emptyArray;
_items = Array.Empty<T>();
// This enumerable could be empty. Let Add allocate a new array, if needed.
// Note it will also go to _defaultCapacity first, not 1, then 2, etc.

Expand Down Expand Up @@ -150,7 +148,7 @@ public int Capacity
}
else
{
_items = s_emptyArray;
_items = Array.Empty<T>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ internal sealed class ReadOnlyCollectionBuilder<T> : IList<T>, System.Collection

private Object _syncRoot;

private static readonly T[] s_emptyArray = new T[0];

/// <summary>
/// Constructs a ReadOnlyCollectionBuilder.
/// </summary>
public ReadOnlyCollectionBuilder()
{
_items = s_emptyArray;
_items = Array.Empty<T>();
}

/// <summary>
Expand Down Expand Up @@ -97,7 +95,7 @@ public int Capacity
}
else
{
_items = s_emptyArray;
_items = Array.Empty<T>();
}
}
}
Expand Down Expand Up @@ -471,7 +469,7 @@ public ReadOnlyCollection<T> ToReadOnlyCollection()
{
items = ToArray();
}
_items = s_emptyArray;
_items = Array.Empty<T>();
_size = 0;
_version++;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static class QueryAggregationOptionsExtensions
{
// This helper is a workaround for the fact that Enum.Defined() does not work on non-public enums.
// There is a custom attribute in System.Reflection.Metadata.Controls that would make it work
// but we don't want to introduce a dependency on that contract just to support two Contract.Asserts.
// but we don't want to introduce a dependency on that contract just to support two asserts.
public static bool IsValidQueryAggregationOption(this QueryAggregationOptions value)
{
return value == QueryAggregationOptions.None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
Expand Down Expand Up @@ -385,7 +384,7 @@ internal Segment(long index, LowLevelConcurrentQueue<T> source)
m_array = new T[SEGMENT_SIZE];
m_state = new VolatileBool[SEGMENT_SIZE]; //all initialized to false
_high = -1;
Contract.Assert(index >= 0);
Debug.Assert(index >= 0);
m_index = index;
_source = source;
}
Expand Down Expand Up @@ -417,7 +416,7 @@ internal bool IsEmpty
/// <param name="value"></param>
internal void UnsafeAdd(T value)
{
Contract.Assert(_high < SEGMENT_SIZE - 1);
Debug.Assert(_high < SEGMENT_SIZE - 1);
_high++;
m_array[_high] = value;
m_state[_high].m_value = true;
Expand All @@ -433,7 +432,7 @@ internal void UnsafeAdd(T value)
/// <returns>the reference to the new Segment</returns>
internal Segment UnsafeGrow()
{
Contract.Assert(_high >= SEGMENT_SIZE - 1);
Debug.Assert(_high >= SEGMENT_SIZE - 1);
Segment newSegment = new Segment(m_index + 1, _source); //m_index is Int64, we don't need to worry about overflow
_next = newSegment;
return newSegment;
Expand All @@ -449,7 +448,7 @@ internal void Grow()
//no CAS is needed, since there is no contention (other threads are blocked, busy waiting)
Segment newSegment = new Segment(m_index + 1, _source); //m_index is Int64, we don't need to worry about overflow
_next = newSegment;
Contract.Assert(_source._tail == this);
Debug.Assert(_source._tail == this);
_source._tail = _next;
}

Expand Down Expand Up @@ -556,7 +555,7 @@ internal bool TryRemove(out T result)
{
spinLocal.SpinOnce();
}
Contract.Assert(_source._head == this);
Debug.Assert(_source._head == this);
_source._head = _next;
}
return true;
Expand Down
3 changes: 1 addition & 2 deletions src/System.Threading/src/System/Progress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Threading;
using System.Diagnostics;
using System.Diagnostics.Contracts;

namespace System
{
Expand Down Expand Up @@ -34,7 +33,7 @@ public Progress()
// Capture the current synchronization context.
// If there is no current context, we use a default instance targeting the ThreadPool.
_synchronizationContext = SynchronizationContext.Current ?? ProgressStatics.DefaultContext;
Contract.Assert(_synchronizationContext != null);
Debug.Assert(_synchronizationContext != null);
_invokeHandlers = new SendOrPostCallback(InvokeHandlers);
}

Expand Down
7 changes: 3 additions & 4 deletions src/System.Threading/src/System/Threading/CountdownEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics.Contracts;

namespace System.Threading
{
Expand Down Expand Up @@ -180,7 +179,7 @@ protected virtual void Dispose(bool disposing)
public bool Signal()
{
ThrowIfDisposed();
Contract.Assert(_event != null);
Debug.Assert(_event != null);

if (_currentCount <= 0)
{
Expand Down Expand Up @@ -227,7 +226,7 @@ public bool Signal(int signalCount)
}

ThrowIfDisposed();
Contract.Assert(_event != null);
Debug.Assert(_event != null);

int observedCount;
SpinWait spin = new SpinWait();
Expand Down Expand Up @@ -261,7 +260,7 @@ public bool Signal(int signalCount)
return true;
}

Contract.Assert(_currentCount >= 0, "latch was decremented below zero");
Debug.Assert(_currentCount >= 0, "latch was decremented below zero");
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/System.Threading/src/System/Threading/TimeoutHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.Contracts;
using System.Diagnostics;

namespace System.Threading
{
Expand Down Expand Up @@ -30,7 +30,7 @@ public static uint GetTime()
public static int UpdateTimeOut(uint startTime, int originalWaitMillisecondsTimeout)
{
// The function must be called in case the time out is not infinite
Contract.Assert(originalWaitMillisecondsTimeout != Timeout.Infinite);
Debug.Assert(originalWaitMillisecondsTimeout != Timeout.Infinite);

uint elapsedMilliseconds = (GetTime() - startTime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public override int Count
}
}

private static readonly object[] s_nullparams = { };

private XmlNode GetNode(XPathNavigator n)
{
return (XmlNode)n.UnderlyingObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ void CheckNodeSet(XPathResultType t)
}

// ----------------------------------------------------------------
private static readonly XPathResultType[] s_temparray1 = { };
private static readonly XPathResultType[] s_temparray1 = Array.Empty<XPathResultType>();
private static readonly XPathResultType[] s_temparray2 = { XPathResultType.NodeSet };
private static readonly XPathResultType[] s_temparray3 = { XPathResultType.Any };
private static readonly XPathResultType[] s_temparray4 = { XPathResultType.String };
Expand Down

0 comments on commit cd67f03

Please sign in to comment.