perf: Reduce allocations and eliminate boxing across ProgressBar, SparkChart, Picker, SegmentedControl, and EffectsView#387
Open
PaulAndersonS wants to merge 1 commit into
Open
Conversation
1. ProgressBar Utility: Replace OrderBy().ToList() with in-place Sort() and cache First()/Last() lookups as direct indexer access, eliminating LINQ allocations in gradient stop processing. 2. SparkAreaChart: Cache segment[0], segment[Count-1], and Count in OnDraw loop to avoid repeated First()/Last() enumerations during rendering. 3. DatePicker/TimePicker: Replace Distinct().ToList() with HashSet-based RemoveAll() to eliminate intermediate list allocations during format order deduplication. 4. SegmentedControl: Replace OfType<T>().FirstOrDefault() and Cast<T>() LINQ calls with direct type-check loops for early-exit selection lookup. 5. EffectsView: Change GetAllItems()/GetAllAutoResetEffectsItems() return types from IEnumerable<Enum> to typed IEnumerable<SfEffects> and IEnumerable<AutoResetEffects>, eliminating boxing, string comparisons, and Select() cast wrappers in touch event hot paths. 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 have unnecessary heap allocations and CPU overhead in hot paths (rendering, layout, touch events) due to:
OrderBy().ToList(),Distinct().ToList(),First(),Last()) creating intermediate collectionsIEnumerable<Enum>return types causing boxing of value-type enumsstring.ToString()comparisons instead of direct enum equality checksOfType<T>().FirstOrDefault()andCast<T>()creating unnecessary enumerator wrappersDescription of Change
5 targeted performance improvements:
ProgressBar
Utility.UpdateGradientStopCollection— ReplaceOrderBy().ToList()with in-placeList.Sort()and cacheFirst()/Last()as direct[0]/[Count-1]indexer access. Eliminates LINQ allocation + 4 redundant enumerations.SparkAreaChart
OnDraw— Cachesegment[0],segment[Count-1], andCountbefore the inner loop. Eliminates repeatedFirst()/Last()calls in the per-frame rendering hot path.DatePickerHelper & TimePickerHelper — Replace
Distinct().ToList()withHashSet<int>-basedRemoveAll(). Deduplicates in-place without allocating a new list.SegmentedControl
SegmentLayout— ReplaceChildren.OfType<T>().FirstOrDefault()andChildren.Cast<T>()with directforeach+type-check loops with early exit. Avoids enumerator allocations.EffectsView
Utils.cs— ChangeGetAllItems()/GetAllAutoResetEffectsItems()return types fromIEnumerable<Enum>toIEnumerable<SfEffects>/IEnumerable<AutoResetEffects>. Eliminates boxing, removes.ToString()comparisons (replaced with direct enum equality), and removes.Select(v => (T)v)cast wrappers at call sites.Issues Fixed
N/A — Proactive performance improvement (no user-reported issue)
Unit Tests Added
ProgressBarUtilityUnitTests— 5 tests verifying sort order, boundary insertion at start/end, and no-op when range is coveredEffectsViewUtilsUnitTests— 6 tests verifying flag decomposition forNone, single flags, and combined flags for bothSfEffectsandAutoResetEffectsScreenshots
N/A — No visual changes