Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Memoize Implementation #898

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions MoreLinq.Test/CartesianTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,8 @@ public void TestCartesianProductCombinations()
[Test]
public void TestEmptyCartesianEvaluation()
{
using var sequence = Enumerable.Range(0, 5).AsTestingSequence();

var resultA = sequence.Cartesian(Enumerable.Empty<int>(), (a, b) => new { A = a, B = b });
var resultB = Enumerable.Empty<int>().Cartesian(sequence, (a, b) => new { A = a, B = b });
var resultA = Enumerable.Range(0, 5).AsTestingSequence().Cartesian(Enumerable.Empty<int>(), (a, b) => new { A = a, B = b });
var resultB = Enumerable.Empty<int>().Cartesian(Enumerable.Range(0, 5).AsTestingSequence(), (a, b) => new { A = a, B = b });
var resultC = Enumerable.Empty<int>().Cartesian(Enumerable.Empty<int>(), (a, b) => new { A = a, B = b });

Assert.AreEqual(0, resultA.Count());
Expand Down
89 changes: 2 additions & 87 deletions MoreLinq.Test/MemoizeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,33 +183,7 @@ public static void MemoizeIsThreadSafe()
}

[Test]
public static void MemoizeRestartsAfterDisposal()
{
var starts = 0;

IEnumerable<int> TestSequence()
{
starts++;
yield return 1;
yield return 2;
}

var memoized = TestSequence().Memoize();

void Run()
{
using ((IDisposable) memoized)
memoized.Take(1).Consume();
}

Run();
Assert.That(starts, Is.EqualTo(1));
Run();
Assert.That(starts, Is.EqualTo(2));
}

[Test]
public static void MemoizeIteratorThrowsWhenCacheDisposedDuringIteration()
public static void MemoizeIteratorThrowsWhenDisposedDuringIteration()
{
var sequence = Enumerable.Range(1, 10);
var memoized = sequence.Memoize();
Expand All @@ -221,7 +195,7 @@ public static void MemoizeIteratorThrowsWhenCacheDisposedDuringIteration()
disposable.Dispose();

var e = Assert.Throws<ObjectDisposedException>(() => reader.Read());
Assert.That(e.ObjectName, Is.EqualTo("MemoizedEnumerable"));
Assert.That(e.ObjectName, Is.EqualTo("MemoizedEnumerable`1"));
}

[Test]
Expand Down Expand Up @@ -259,67 +233,8 @@ IEnumerable<int> TestSequence()
var e2 = Assert.Throws<Exception>(() => r2.Read());
Assert.That(e2, Is.SameAs(error));
}
using (var r1 = memoized.Read())
Assert.That(r1.Read(), Is.EqualTo(123));
}

[Test]
public void MemoizeRethrowsErrorDuringIterationStartToAllIteratorsUntilDisposed()
{
var error = new Exception("This is a test exception.");

var i = 0;
IEnumerable<int> TestSequence()
{
if (0 == i++) // throw at start for first iteration only
throw error;
yield return 42;
}

var xs = new DisposalTrackingSequence<int>(TestSequence());
var memoized = xs.Memoize();
using ((IDisposable) memoized)
using (var r1 = memoized.Read())
using (var r2 = memoized.Read())
{
var e1 = Assert.Throws<Exception>(() => r1.Read());
Assert.That(e1, Is.SameAs(error));

Assert.That(xs.IsDisposed, Is.True);

var e2 = Assert.Throws<Exception>(() => r2.Read());
Assert.That(e2, Is.SameAs(error));
}

using (var r1 = memoized.Read())
using (var r2 = memoized.Read())
Assert.That(r1.Read(), Is.EqualTo(r2.Read()));
}

[Test]
public void MemoizeRethrowsErrorDuringFirstIterationStartToAllIterationsUntilDisposed()
{
var error = new Exception("An error on the first call!");
var obj = new object();
var calls = 0;
var source = Delegate.Enumerable(() => 0 == calls++
? throw error
: Enumerable.Repeat(obj, 1).GetEnumerator());

var memo = source.Memoize();

for (var i = 0; i < 2; i++)
{
var e = Assert.Throws<Exception>(() => memo.First());
Assert.That(e, Is.SameAs(error));
}

((IDisposable) memo).Dispose();
Assert.That(memo.Single(), Is.EqualTo(obj));
}

// TODO Consolidate with MoreLinq.Test.TestingSequence<T>?

sealed class DisposalTrackingSequence<T> : IEnumerable<T>, IDisposable
{
readonly IEnumerable<T> _sequence;
Expand Down
5 changes: 1 addition & 4 deletions MoreLinq.Test/TestingSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,11 @@ public IEnumerator<T> GetEnumerator()
_disposed = false;
enumerator.Disposed += delegate
{
Assert.That(_disposed, Is.False, "LINQ operators should not dispose a sequence more than once.");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per the specification, it is not a failure to call .Dispose() multiple times. The .Dispose() method is expected to be idempotent, such that it is callable multiple times without throwing an exception.

_disposed = true;
};
var ended = false;
enumerator.MoveNextCalled += (_, moved) =>
{
Assert.That(ended, Is.False, "LINQ operators should not continue iterating a sequence that has terminated.");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per the specification, it is not a failure to call .MoveNext() after receiving a false response. The enumerator is simply expected to continue to return false for each following call.

ended = !moved;
Assert.That(_disposed, Is.False, "LINQ operators should not call MoveNext() on a disposed sequence.");
MoveNextCallCount++;
};
_sequence = null;
Expand Down
102 changes: 38 additions & 64 deletions MoreLinq/Experimental/Memoize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,106 +56,80 @@ public static IEnumerable<T> Memoize<T>(this IEnumerable<T> source)
switch (source)
{
case null: throw new ArgumentNullException(nameof(source));
case ICollection<T> : // ...
case ICollection<T>: // ...
case IReadOnlyCollection<T>: // ...
case MemoizedEnumerable<T> : return source;
case MemoizedEnumerable<T>: return source;
default: return new MemoizedEnumerable<T>(source);
}
}
}

sealed class MemoizedEnumerable<T> : IEnumerable<T>, IDisposable
{
List<T>? _cache;
readonly List<T> _cache = new();
readonly object _locker;
readonly IEnumerable<T> _source;
IEnumerator<T>? _sourceEnumerator;

readonly Lazy<IEnumerator<T>> _sourceEnumerator;
bool _running = true;
bool _isDisposed = false;

int? _errorIndex;
ExceptionDispatchInfo? _error;

public MemoizedEnumerable(IEnumerable<T> sequence)
{
_source = sequence ?? throw new ArgumentNullException(nameof(sequence));
if (sequence == null) throw new ArgumentNullException(nameof(sequence));
_sourceEnumerator = new Lazy<IEnumerator<T>>(
sequence.GetEnumerator,
System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
_locker = new object();
}

public IEnumerator<T> GetEnumerator()
{
if (_cache == null)
var index = 0;

while (true)
{
if (_isDisposed)
throw new ObjectDisposedException(GetType().Name);

T current;
lock (_locker)
{
if (_cache == null)
if (index >= _cache.Count)
{
_error?.Throw();
if (index == _errorIndex)
Assume.NotNull(_error).Throw();

bool moved;
try
{
var cache = new List<T>(); // for exception safety, allocate then...
_sourceEnumerator = _source.GetEnumerator(); // (because this can fail)
_cache = cache; // ...commit to state
moved = _running && _sourceEnumerator.Value.MoveNext();
}
catch (Exception ex)
{
_error = ExceptionDispatchInfo.Capture(ex);
_errorIndex = index;
_sourceEnumerator.Value.Dispose();
throw;
}
}
}
}

return _(); IEnumerator<T> _()
{
var index = 0;

while (true)
{
T current;
lock (_locker)
{
if (_cache == null) // Cache disposed during iteration?
throw new ObjectDisposedException(nameof(MemoizedEnumerable<T>));

if (index >= _cache.Count)
if (!moved)
{
if (index == _errorIndex)
Assume.NotNull(_error).Throw();

if (_sourceEnumerator == null)
break;

bool moved;
try
{
moved = _sourceEnumerator.MoveNext();
}
catch (Exception ex)
{
_error = ExceptionDispatchInfo.Capture(ex);
_errorIndex = index;
_sourceEnumerator.Dispose();
_sourceEnumerator = null;
throw;
}

if (moved)
{
_cache.Add(_sourceEnumerator.Current);
}
else
{
_sourceEnumerator.Dispose();
_sourceEnumerator = null;
break;
}
_running = false;
_sourceEnumerator.Value.Dispose();
yield break;
}

current = _cache[index];
_cache.Add(_sourceEnumerator.Value.Current);
}

yield return current;
index++;
current = _cache[index];
}

yield return current;
index++;
}
}

Expand All @@ -166,10 +140,10 @@ public void Dispose()
lock (_locker)
{
_error = null;
_cache = null;
_errorIndex = null;
_sourceEnumerator?.Dispose();
_sourceEnumerator = null;
_sourceEnumerator.Value.Dispose();

_isDisposed = true;
}
}
}
Expand Down