perf: Eliminate LINQ allocations and improve algorithmic complexity in Chart segments#380
Open
PaulAndersonS wants to merge 1 commit into
Open
perf: Eliminate LINQ allocations and improve algorithmic complexity in Chart segments#380PaulAndersonS wants to merge 1 commit into
PaulAndersonS wants to merge 1 commit into
Conversation
…n Chart segments 1. ErrorBarSegment.SetData: Replace 4 LINQ Where/Min/Max chains with single-pass loops, eliminating enumerator allocations on every data bind. 2. ErrorBarSegment.GetSdErrorValue: Replace Where().ToList(), List<double> allocations for dev/sQDev, and Sum(x => x) with two simple loops computing mean and sum-of-squared-deviations in-place. Eliminates 3 List allocations and 1 LINQ delegate allocation per call. 3. CircularSeries: Add HashSet<PieSegment> (LeftPointsLookup) for O(1) Contains() checks during label arrangement, replacing O(n) List.Contains() that caused O(n²) behavior with many data points. 4. AreaSegment.SetData: Replace LINQ Where/DefaultIfEmpty/Min chain with a simple for loop to find minimum non-NaN value. 5. SplineAreaSegment.UpdateRange & SplineRangeAreaSegment.UpdateRange: Same LINQ-to-loop optimization for NaN-filtered minimum calculation. 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 Chart segment classes use LINQ chains (
.Where(),.DefaultIfEmpty(),.Min(),.Max(),.ToList(),.Sum()) in data processing and layout methods. These allocate enumerators, delegate objects, and intermediate collections on every call — causing unnecessary GC pressure during data binding and rendering.Additionally,
CircularSeries.LeftPoints.Contains()performs O(n) linear searches during label arrangement, leading to O(n²) complexity with many data points.Description of Change
Five targeted performance optimizations:
ErrorBarSegment.SetData — Replaced 4 chained LINQ
Where/Min/Maxcalls with a single-passforloop that computes min/max in one traversal. Eliminates 4 enumerator allocations per data bind.ErrorBarSegment.GetSdErrorValue — Replaced
Where().ToList(), twoList<double>allocations (dev,sQDev), andSum(x => x)with two simple loops computing mean and sum-of-squared-deviations in-place. Eliminates 3 List allocations and 1 delegate allocation per call.CircularSeries label arrangement — Added
HashSet<PieSegment> LeftPointsLookupfor O(1)Contains()checks, replacing O(n)List.Contains()that caused O(n²) behavior during label overlap resolution.AreaSegment.SetData — Replaced LINQ
Where/DefaultIfEmpty/Minchain with a simpleforloop to find the minimum non-NaN value.SplineAreaSegment.UpdateRange & SplineRangeAreaSegment.UpdateRange — Same LINQ-to-loop optimization for NaN-filtered minimum calculation.
Issues Fixed
N/A — Proactive performance improvement
Test Coverage
Screenshots
N/A — No visual changes