perf(Charts): Reduce allocations and optimize hot paths in Chart control#384
Open
PaulAndersonS wants to merge 1 commit into
Open
perf(Charts): Reduce allocations and optimize hot paths in Chart control#384PaulAndersonS wants to merge 1 commit into
PaulAndersonS wants to merge 1 commit into
Conversation
…ndering performance - Replace O(n²) IndexOf with Dictionary lookup in CategoryAxis - Cache Dictionary.Values.ToList() outside loops in CartesianChartArea - Cache GetType() results to avoid repeated reflection - Use HashSet for O(1) lookups instead of List.Contains() - Use StringBuilder for string concatenation in loops - Replace LINQ Where().Sum() with manual loop in rendering path - Single-pass min/max calculation in ErrorBarSegment - Reuse list allocations in TrackballBehavior - Use string.Concat instead of += for tooltip text - Replace Cast<T>() with pattern matching to avoid enumerator allocation 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.
Description of Change
This PR implements 10 targeted performance optimizations in the Chart control to reduce allocations and improve rendering performance in hot paths:
Replace O(n²) IndexOf with Dictionary lookup (
CategoryAxis.cs) —distinctXValues.IndexOf(val)inside loops was O(n) per call. A pre-builtDictionary<string, int>gives O(1) lookups.Cache
Dictionary.Values.ToList()outside loops (CartesianChartArea.cs) —SideBySideSeriesPosition.Values.ToList()[i]was allocating a new list every iteration. Now cached once before the loop.Cache
GetType()results (CartesianChartArea.cs) — RepeatedstackingSeries.GetType()and.Namecalls replaced with local variables to avoid repeated reflection.Replace
List.Contains()with HashSet (CategoryAxis.cs) —groupingValues.Contains(xValues[j])was O(n). A parallelHashSet<string>provides O(1) membership tests.Replace string concatenation with StringBuilder (
CategoryAxis.cs) — String+=in a loop inGetLabelContentreplaced withStringBuilderto avoid repeated allocations.Manual loop instead of LINQ in stacking calculation (
CartesianChartArea.cs) — TheGetYValuemethod already uses an optimized manual loop pattern (verified existing implementation).Single-pass min/max calculation (
ErrorBarSegment.cs) — Four separate LINQ iterations (Where().Min(),Where().Max()) replaced with a single loop computing all four values.Reuse list allocations in TrackballBehavior (
ChartTrackballBehavior.cs) —new List<T>(...)on every mouse move replaced with clear-and-reuse pattern to reduce GC pressure.Use
string.Concatinstead of+=(RangeColumnSeries.cs) — Avoids intermediate string allocation in tooltip text construction.Replace
Cast<T>()with pattern matching (CategoryAxis.cs,CartesianChartArea.cs,ChartTrackballBehavior.cs) — Avoids allocating a LINQ enumerator wrapper; direct iteration withis not Tpattern is allocation-free.Performance Impact
These changes target allocation-heavy hot paths that execute during chart rendering, data grouping, trackball interaction, and tooltip display. The optimizations reduce:
Issues Fixed
N/A — Performance improvement, no functional changes.