Skip to content

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

Open
PaulAndersonS wants to merge 1 commit into
mainfrom
paulandersons/stunning-lamp
Open

perf: Eliminate LINQ allocations and improve algorithmic complexity in Chart segments#380
PaulAndersonS wants to merge 1 commit 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>
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.

1 participant