perf: Reduce allocations and redundant enumerations across Carousel, NumericEntry, Calendar, and BottomSheet#383
Open
PaulAndersonS wants to merge 1 commit into
Open
Conversation
1. Carousel (iOS/Windows/Android): Cache ItemsSource.Count() to avoid repeated IEnumerable enumeration in MapSelectedIndex/UpdateSelectedIndex. 2. NumericEntry: Replace string concatenation loop with direct int.TryParse on the format suffix, eliminating per-char ToString() allocations. 3. Calendar ViewChangedEventArgs: Use a shared static empty ReadOnlyCollection instead of allocating new List + AsReadOnly per event args instance. 4. Calendar CalendarViewHelper: Pre-allocate List<DateTime> with known capacity in GetCurrentMonthDates to avoid intermediate resizing. 5. BottomSheet: Replace ContainsKey + indexer SET with TryAdd to eliminate redundant dictionary hash lookups. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Multiple controls perform redundant work in frequently-called paths — repeated IEnumerable enumeration, per-character string allocations, per-instance collection allocations for defaults, and double dictionary lookups — all of which create unnecessary GC pressure.
Description of Change
Five targeted performance improvements across 4 controls:
Carousel (iOS/Windows/Android) —
MapSelectedIndex/UpdateSelectedIndexcalled.Count()up to 3 times and.Any()once onIEnumerable<object> ItemsSource. Each call fully enumerates the collection. Fixed by caching the count in a local variable.NumericEntry —
GetMaximumFractionDigitbuilt a digit string character-by-character using+=(O(n²) allocations). Since the format suffix is at most 2 characters, replaced with a singleint.TryParse()call that avoids all intermediate string allocations.Calendar ViewChangedEventArgs — Each instance allocated
new List<DateTime>().AsReadOnly()twice for default property values (2 List + 2 ReadOnlyCollection objects per event). Replaced with a sharedstatic readonlyempty collection.Calendar CalendarViewHelper.GetCurrentMonthDates —
new List<DateTime>()without capacity hint caused repeated internal array resizing. Pre-allocated withvisibleDates.Countas the upper bound.BottomSheet —
ContainsKey+ indexer SET pattern performs two hash lookups. Replaced withTryAdd()which performs a single lookup.Issues Fixed
N/A — proactive performance improvement
Unit Tests Added
CustomFormat_StandardFormatWithDigits_ParsesMaxFractionCorrectly— Verifies standard format specifiers (C2, C10, N0, F99) are parsed correctly after the int.TryParse optimizationCustomFormat_CustomFormatWithNonDigitSuffix_FallsBackToCustomParsing— Verifies non-numeric suffixes fall back to custom format parsingCalendarViewChangedEventArgs_DefaultProperties_UseSharedEmptyCollection— Verifies the static empty collection is shared across instances (no per-instance allocation)CalendarViewChangedEventArgs_AssignedDates_ReturnsCorrectValues— Verifies assigned dates work correctlySelectedIndex_WithItemsSource_ClampsCorrectly— Verifies carousel index clamping with the cached count approachScreenshots
N/A — no visual changes