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

ItemDictionary<T>.Enumerator has an unnecessary finalizer #7211

Merged
merged 7 commits into from
Jan 21, 2022

Conversation

elachlan
Copy link
Contributor

@elachlan elachlan commented Jan 4, 2022

Fixes #7208

src/Build/Collections/ItemDictionary.cs Outdated Show resolved Hide resolved
@KalleOlaviNiemitalo
Copy link

KalleOlaviNiemitalo commented Jan 12, 2022

This looks like the nested enumerator type could be replaced with an iterator method: nested foreach loops and yield return. I don't know how the performance would be, though.

Iterators do not support IEnumerator.Reset() but the current implementation of Reset is bogus anyway: it resets _itemEnumerator too, so if you had the hierarchy ((A B C) (D E F)) and the current item is E, then after the reset, it will enumerate (D E F A B C D E F) even though the right answer would be just (A B C D E F). Which perhaps indicates that nothing actually calls Reset.

@ladipro
Copy link
Member

ladipro commented Jan 13, 2022

@KalleOlaviNiemitalo that's indeed a bug, thank you. It looks like we should do

_itemEnumerator = GetNextItemEnumerator();

instead of resetting whatever _itemEnumerator is currently pointing to. Out of scope of this PR but would be nice to fix for sure. Also +1 on trying to replace the class with an iterator.

@KalleOlaviNiemitalo
Copy link

In principle, Reset and MoveNext should also call _itemEnumerator?.Dispose() before assigning _itemEnumerator = GetNextItemEnumerator(). In practice, _itemEnumerator is always a LinkedList<T>.Enumerator whose Dispose() method would do nothing anyway. Although LinkedList<T> is not sealed, ItemDictionary<T> does not let its callers add instances of any derived types to _itemLists.

@elachlan
Copy link
Contributor Author

Could one of you please write up an issue to spec out this suggestion? It sounds like a good idea.

@KalleOlaviNiemitalo
Copy link

Filed #7286.

@ladipro ladipro added the merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now. label Jan 19, 2022
@ladipro ladipro merged commit d7d2a3a into dotnet:main Jan 21, 2022
@elachlan elachlan deleted the itemdictionary-remove-finalizer branch January 21, 2022 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ItemDictionary<T>.Enumerator has an unnecessary finalizer
4 participants