Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1572,18 +1572,14 @@ internal Enumerator(IBinaryTree<T> root, Builder builder = null, int startIndex
this.reversed = reversed;
this.enumeratingBuilderVersion = builder != null ? builder.Version : -1;
this.poolUserId = Guid.NewGuid();
this.stack = null;
if (this.count > 0)
{
this.stack = null;
if (!EnumeratingStacks.TryTake(this, out this.stack))
{
this.stack = EnumeratingStacks.PrepNew(this, new Stack<RefAsValueType<IBinaryTree<T>>>(root.Height));
}
}
else
{
this.stack = null;
}

this.Reset();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,12 @@ internal Enumerator(IBinaryTree<KeyValuePair<TKey, TValue>> root, Builder builde
this.enumeratingBuilderVersion = builder != null ? builder.Version : -1;
this.poolUserId = Guid.NewGuid();
this.stack = null;
if (!enumeratingStacks.TryTake(this, out this.stack))
if (!this.root.IsEmpty)
{
this.stack = enumeratingStacks.PrepNew(this, new Stack<RefAsValueType<IBinaryTree<KeyValuePair<TKey, TValue>>>>(root.Height));
if (!enumeratingStacks.TryTake(this, out this.stack))
{
this.stack = enumeratingStacks.PrepNew(this, new Stack<RefAsValueType<IBinaryTree<KeyValuePair<TKey, TValue>>>>(root.Height));
}
}

this.Reset();
Expand Down Expand Up @@ -1062,21 +1065,22 @@ public bool MoveNext()
this.ThrowIfDisposed();
this.ThrowIfChanged();

using (var stack = this.stack.Use(this))
if (this.stack != null)
{
if (stack.Value.Count > 0)
{
IBinaryTree<KeyValuePair<TKey, TValue>> n = stack.Value.Pop().Value;
this.current = n;
this.PushLeft(n.Right);
return true;
}
else
using (var stack = this.stack.Use(this))
{
this.current = null;
return false;
if (stack.Value.Count > 0)
{
IBinaryTree<KeyValuePair<TKey, TValue>> n = stack.Value.Pop().Value;
this.current = n;
this.PushLeft(n.Right);
return true;
}
}
}

this.current = null;
return false;
}

/// <summary>
Expand All @@ -1088,12 +1092,15 @@ public void Reset()

this.enumeratingBuilderVersion = builder != null ? builder.Version : -1;
this.current = null;
using (var stack = this.stack.Use(this))
if (this.stack != null)
{
stack.Value.Clear();
}
using (var stack = this.stack.Use(this))
{
stack.Value.Clear();
}

this.PushLeft(this.root);
this.PushLeft(this.root);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,10 @@ public void GetValueOrDefaultOfConcreteType()
[Fact]
public void EnumeratorRecyclingMisuse()
{
var collection = ImmutableDictionary.Create<int, int>();
var collection = ImmutableDictionary.Create<int, int>().Add(5, 3);
var enumerator = collection.GetEnumerator();
var enumeratorCopy = enumerator;
Assert.True(enumerator.MoveNext());
Assert.False(enumerator.MoveNext());
enumerator.Dispose();
Assert.Throws<ObjectDisposedException>(() => enumerator.MoveNext());
Expand All @@ -315,6 +316,7 @@ public void EnumeratorRecyclingMisuse()
// We expect that acquiring a new enumerator will use the same underlying Stack<T> object,
// but that it will not throw exceptions for the new enumerator.
enumerator = collection.GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.False(enumerator.MoveNext());
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
enumerator.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ public void EnumeratorWithHashCollisionsTest()
[Fact]
public void EnumeratorRecyclingMisuse()
{
var collection = ImmutableHashSet.Create<int>();
var collection = ImmutableHashSet.Create<int>().Add(5);
var enumerator = collection.GetEnumerator();
var enumeratorCopy = enumerator;
Assert.True(enumerator.MoveNext());
Assert.False(enumerator.MoveNext());
enumerator.Dispose();
Assert.Throws<ObjectDisposedException>(() => enumerator.MoveNext());
Expand All @@ -90,6 +91,7 @@ public void EnumeratorRecyclingMisuse()
// We expect that acquiring a new enumerator will use the same underlying Stack<T> object,
// but that it will not throw exceptions for the new enumerator.
enumerator = collection.GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.False(enumerator.MoveNext());
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
enumerator.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
Expand Down Expand Up @@ -316,9 +318,10 @@ public void WithComparersEmptyCollection()
[Fact]
public void EnumeratorRecyclingMisuse()
{
var collection = ImmutableSortedDictionary.Create<int, int>();
var collection = ImmutableSortedDictionary.Create<int, int>().Add(3, 5);
var enumerator = collection.GetEnumerator();
var enumeratorCopy = enumerator;
Assert.True(enumerator.MoveNext());
Assert.False(enumerator.MoveNext());
enumerator.Dispose();
Assert.Throws<ObjectDisposedException>(() => enumerator.MoveNext());
Expand All @@ -334,11 +337,60 @@ public void EnumeratorRecyclingMisuse()
// We expect that acquiring a new enumerator will use the same underlying Stack<T> object,
// but that it will not throw exceptions for the new enumerator.
enumerator = collection.GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.False(enumerator.MoveNext());
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
enumerator.Dispose();
}

////[Fact] // not really a functional test -- but very useful to enable when collecting perf traces.
public void EnumerationPerformance()
{
var dictionary = Enumerable.Range(1, 1000).ToImmutableSortedDictionary(k => k, k => k);

var timing = new TimeSpan[3];
var sw = new Stopwatch();
for (int j = 0; j < timing.Length; j++)
{
sw.Start();
for (int i = 0; i < 10000; i++)
{
foreach (var entry in dictionary)
{
}
}

timing[j] = sw.Elapsed;
sw.Reset();
}

File.AppendAllText(Environment.ExpandEnvironmentVariables(@"%TEMP%\timing.txt"), string.Join(Environment.NewLine, timing));
}

////[Fact] // not really a functional test -- but very useful to enable when collecting perf traces.
public void EnumerationPerformance_Empty()
{
var dictionary = ImmutableSortedDictionary<int, int>.Empty;

var timing = new TimeSpan[3];
var sw = new Stopwatch();
for (int j = 0; j < timing.Length; j++)
{
sw.Start();
for (int i = 0; i < 10000; i++)
{
foreach (var entry in dictionary)
{
}
}

timing[j] = sw.Elapsed;
sw.Reset();
}

File.AppendAllText(Environment.ExpandEnvironmentVariables(@"%TEMP%\timing_empty.txt"), string.Join(Environment.NewLine, timing));
}

protected override IImmutableDictionary<TKey, TValue> Empty<TKey, TValue>()
{
return ImmutableSortedDictionaryTest.Empty<TKey, TValue>();
Expand Down