Perf: Eliminate unnecessary List allocations in Accordion control#372
Merged
Conversation
Replace Items.ToList().FindAll() and Items.ToList().FirstOrDefault() with direct iteration over the ObservableCollection. The previous pattern allocated a full copy of the Items collection on every call just to perform a filter or search, creating unnecessary GC pressure during expand/collapse and keyboard navigation operations. Changes: - SfAccordion.UpdateSelection(): Use foreach loop instead of ToList().FindAll() - SfAccordion.UpdateAccordionItemsBasedOnExpandModes(): Build filtered list directly without copying the entire collection first - SfAccordion.OnKeyDown(): Use foreach with early break instead of ToList().FirstOrDefault() - AccordionItemView.CanCollapseItemOnSingleAndMultipleExpandMode(): Count expanded items with foreach instead of allocating a filtered list Also adds unit tests verifying the refactored methods maintain correct behavior for edge cases (no items selected, null AccordionItemView, empty collections, multiple expanded items in Single mode). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Collaborator
Author
|
@copilot resolve the merge conflicts in this pull request |
…affle # Conflicts: # maui/src/Accordion/AccordionItemView.cs # maui/src/Accordion/SfAccordion.cs
Contributor
Merge conflicts have been resolved in commit |
Collaborator
|
Changes are fine. |
AmalRajUmapathySelvam
approved these changes
Jun 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root Cause of the Issue
The
SfAccordionandAccordionItemViewclasses usedItems.ToList().FindAll()andItems.ToList().FirstOrDefault()patterns in multiple methods. SinceItemsis anObservableCollection<AccordionItem>, calling.ToList()allocates a full copy of the collection on every invocation — only to immediately filter or search it. This creates unnecessary GC pressure during user interactions (expand/collapse, keyboard navigation).Description of Change
Replaced all
Items.ToList().FindAll(predicate)andItems.ToList().FirstOrDefault(predicate)calls with directforeachiteration over theObservableCollection:SfAccordion.UpdateSelection()Items.ToList().FindAll(...)foreachwith counterSfAccordion.UpdateAccordionItemsBasedOnExpandModes()Items.ToList().FindAll(...)foreachbuilding filtered list directlySfAccordion.OnKeyDown()Items.ToList().FirstOrDefault(...)foreachwith earlybreakAccordionItemView.CanCollapseItemOnSingleAndMultipleExpandMode()Accordion.Items.ToList().FindAll(...)foreachwith counter and early exitImpact: Eliminates 4 unnecessary
List<T>allocations per operation, reducing GC pressure during accordion interactions.Unit tests added: 4 new tests covering edge cases for the refactored methods.
Issues Fixed
N/A (performance improvement)
Screenshots
N/A (no visual changes)