Skip to content

perf: Reduce allocations and improve algorithmic efficiency across Chart controls#381

Open
PaulAndersonS wants to merge 1 commit into
mainfrom
paulandersons/friendly-broccoli
Open

perf: Reduce allocations and improve algorithmic efficiency across Chart controls#381
PaulAndersonS wants to merge 1 commit into
mainfrom
paulandersons/friendly-broccoli

Conversation

@PaulAndersonS
Copy link
Copy Markdown
Collaborator

Root Cause of the Issue

Multiple performance anti-patterns exist in Chart control rendering and layout paths that cause unnecessary memory allocations and CPU usage:

  • Array allocation just to access a single element
  • String concatenation in loops creating O(n) intermediate string objects
  • Lists created without capacity hints in rendering hot paths causing repeated resizing
  • Multiple string concatenation operators creating intermediate string allocations

Description of Change

Five targeted performance optimizations:

  1. AxisLabelLayout.cs — Replace Keys.ToArray()[lastIndex] with Keys.ElementAt(lastIndex) to avoid allocating an entire array just to access the last key during axis label intersection checks.

  2. CategoryAxis.cs — Replace string + ", " + label concatenation in a loop with StringBuilder in GetLabelContent(). This reduces string allocations from O(n) to O(1) when generating grouped category labels.

  3. PolarAreaSegment.cs — Pre-allocate List<float> with known capacity (_pointsCount + 2) * 2 in both GenerateInteriorPoints() and GenerateStrokePoints(). These methods run on every frame during animation, so avoiding List resizing directly reduces GC pressure.

  4. BoxAndWhiskerSeries.cs — Replace 5-way string concatenation with string interpolation in tooltip text generation to reduce intermediate string allocations.

  5. HiLoOpenCloseSeries.cs — Replace 4-way string concatenation with string interpolation in tooltip text generation.

Issues Fixed

N/A — Proactive performance improvement

Screenshots

N/A — Internal optimization with no visual changes

…art controls

Performance improvements:

1. AxisLabelLayout: Replace Keys.ToArray()[lastIndex] with Keys.ElementAt()
   to avoid allocating an entire array just to access one element.

2. CategoryAxis.GetLabelContent: Replace string concatenation in loop with
   StringBuilder to reduce O(n) string allocations to O(1).

3. PolarAreaSegment: Pre-allocate List<float> with known capacity in
   GenerateInteriorPoints and GenerateStrokePoints to avoid repeated
   resizing during rendering.

4. BoxAndWhiskerSeries: Replace string concatenation with string
   interpolation in tooltip text generation.

5. HiLoOpenCloseSeries: Replace string concatenation with string
   interpolation in tooltip text generation.

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