Skip to content

perf: Eliminate LINQ allocations and improve algorithmic complexity in Chart segments#380

Merged
PaulAndersonS merged 3 commits into
mainfrom
paulandersons/stunning-lamp
Jun 9, 2026
Merged

perf: Eliminate LINQ allocations and improve algorithmic complexity in Chart segments#380
PaulAndersonS merged 3 commits into
mainfrom
paulandersons/stunning-lamp

Conversation

@PaulAndersonS

Copy link
Copy Markdown
Collaborator

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:

  1. ErrorBarSegment.SetData — Replaced 4 chained LINQ Where/Min/Max calls with a single-pass for loop that computes min/max in one traversal. Eliminates 4 enumerator allocations per data bind.

  2. ErrorBarSegment.GetSdErrorValue — Replaced Where().ToList(), two List<double> allocations (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 delegate allocation per call.

  3. CircularSeries label arrangement — Added HashSet<PieSegment> LeftPointsLookup for O(1) Contains() checks, replacing O(n) List.Contains() that caused O(n²) behavior during label overlap resolution.

  4. AreaSegment.SetData — Replaced LINQ Where/DefaultIfEmpty/Min chain with a simple for loop to find the minimum non-NaN value.

  5. SplineAreaSegment.UpdateRange & SplineRangeAreaSegment.UpdateRange — Same LINQ-to-loop optimization for NaN-filtered minimum calculation.

Issues Fixed

N/A — Proactive performance improvement

Test Coverage

  • Added 5 new unit tests validating ErrorBarSegment and AreaSegment SetData behavior with NaN and mixed values
  • All 2353 existing Chart unit tests pass without regression

Screenshots

N/A — No visual changes

…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>
Comment thread maui/src/Charts/Segment/SplineRangeAreaSegment.cs Outdated
Comment thread maui/src/Charts/Segment/SplineAreaSegment.cs Outdated
@PaulAndersonS

Copy link
Copy Markdown
Collaborator Author

@copilot check the comments and do the needed changes

Copilot AI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

@copilot check the comments and do the needed changes

Updated based on the review feedback in commit 61480a5. I aligned the local minimum variable names in SplineAreaSegment and SplineRangeAreaSegment to match AreaSegment, with no behavior changes, and re-ran chart tests + validation.

@SaiyathAliFathima SaiyathAliFathima left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are fine

@PaulAndersonS PaulAndersonS added this to the v1.0.11 milestone Jun 9, 2026
@PaulAndersonS PaulAndersonS merged commit c33d82a into main Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants