Repository navigation
optimize BlockingCollection<T>.GetConsumingEnumerable iterator #69631
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
Changes from all commits
9fe9df7
94037cf
fb26b8f
7605f77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -603,6 +603,14 @@ public bool TryTake([MaybeNullWhen(false)] out T item) | |
| public bool TryTake([MaybeNullWhen(false)] out T item, TimeSpan timeout) | ||
| { | ||
| ValidateTimeout(timeout); | ||
|
|
||
| // If the collection is completed then there is no need remove an item. | ||
| if (IsCompleted) | ||
| { | ||
| item = default(T); | ||
| return false; | ||
| } | ||
|
|
||
| return TryTakeWithNoTimeValidation(out item, (int)timeout.TotalMilliseconds, CancellationToken.None, null); | ||
| } | ||
|
|
||
|
|
@@ -624,6 +632,14 @@ public bool TryTake([MaybeNullWhen(false)] out T item, TimeSpan timeout) | |
| public bool TryTake([MaybeNullWhen(false)] out T item, int millisecondsTimeout) | ||
| { | ||
| ValidateMillisecondsTimeout(millisecondsTimeout); | ||
|
|
||
| // If the collection is completed then there is no need remove an item. | ||
| if (IsCompleted) | ||
| { | ||
| item = default(T); | ||
| return false; | ||
| } | ||
|
|
||
| return TryTakeWithNoTimeValidation(out item, millisecondsTimeout, CancellationToken.None, null); | ||
| } | ||
|
|
||
|
|
@@ -649,6 +665,15 @@ public bool TryTake([MaybeNullWhen(false)] out T item, int millisecondsTimeout) | |
| public bool TryTake([MaybeNullWhen(false)] out T item, int millisecondsTimeout, CancellationToken cancellationToken) | ||
| { | ||
| ValidateMillisecondsTimeout(millisecondsTimeout); | ||
|
|
||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| // If the collection is completed then there is no need remove an item. | ||
| if (IsCompleted) | ||
| { | ||
| item = default(T); | ||
| return false; | ||
| } | ||
|
|
||
| return TryTakeWithNoTimeValidation(out item, millisecondsTimeout, cancellationToken, null); | ||
| } | ||
|
|
||
|
|
@@ -670,16 +695,9 @@ public bool TryTake([MaybeNullWhen(false)] out T item, int millisecondsTimeout, | |
| /// <exception cref="System.ObjectDisposedException">If the collection has been disposed.</exception> | ||
| private bool TryTakeWithNoTimeValidation([MaybeNullWhen(false)] out T item, int millisecondsTimeout, CancellationToken cancellationToken, CancellationTokenSource? combinedTokenSource) | ||
| { | ||
| CheckDisposed(); | ||
| Debug.Assert(!_isDisposed); | ||
| item = default(T)!; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the |
||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| //If the collection is completed then there is no need to wait. | ||
| if (IsCompleted) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be replaced with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
IsCompleted can be changed from antoher thread , the assert would not be always true https://helixre8s23ayyeko0k025g8.blob.core.windows.net/dotnet-runtime-refs-pull-69631-merge-a83f55ae749f443aa3/System.Collections.Concurrent.Tests/1/console.b9f2d628.log?helixlogtype=result |
||
| { | ||
| return false; | ||
| } | ||
| bool waitForSemaphoreWasSuccessful = false; | ||
|
|
||
| // set the combined token source to the combinedToken parameter if it is not null (came from GetConsumingEnumerable) | ||
|
|
@@ -1645,6 +1663,7 @@ public IEnumerable<T> GetConsumingEnumerable() | |
| /// <exception cref="OperationCanceledException">If the <see cref="CancellationToken"/> is canceled.</exception> | ||
| public IEnumerable<T> GetConsumingEnumerable(CancellationToken cancellationToken) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| CancellationTokenSource? linkedTokenSource = null; | ||
| try | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pedrobsaila this alters the behavior of the
Takemethod in case the collection is completed and thecancellationTokenis canceled. The existing behavior is to throw anOperationCanceledException. The new behavior will be to returnfalse. Altering the behavior of the method is unlikely to be desirable. A demonstration of the current behavior can be found in this comment.