Skip to content

Perf: Replace LINQ Min/Max with direct index access on sorted array in BoxAndWhiskerSeries#368

Merged
PaulAndersonS merged 2 commits into
mainfrom
paulandersons/perf-improvement
May 27, 2026
Merged

Perf: Replace LINQ Min/Max with direct index access on sorted array in BoxAndWhiskerSeries#368
PaulAndersonS merged 2 commits into
mainfrom
paulandersons/perf-improvement

Conversation

@PaulAndersonS
Copy link
Copy Markdown
Collaborator

Root Cause of the Issue

In BoxAndWhiskerSeries.CreateSegments(), after the yList array is sorted with Array.Sort(yList), the code uses yList.Min() and yList.Max() (LINQ extension methods) to get the minimum and maximum values. These are O(n) operations that redundantly iterate through an already-sorted array.

Description of Change

Replaced yList.Min() and yList.Max() with yList[0] and yList[^1] respectively. Since the array is guaranteed to be sorted at this point, the first element is the minimum and the last element is the maximum — giving O(1) access instead of O(n) iteration.

This improvement is especially relevant for charts with large datasets where this loop runs once per data point.

Issues Fixed

Performance improvement — no associated issue.

Screenshots

N/A — no visual change.

… in BoxAndWhiskerSeries

In CreateSegments(), after Array.Sort(yList), the Min() and Max() LINQ calls
perform redundant O(n) iterations. Since the array is already sorted, we can
use yList[0] and yList[^1] for O(1) access.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
maximum = yList.Max();
// yList is already sorted, so use direct index access instead of LINQ Min/Max.
minimum = yList[0];
maximum = yList[^1];
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.

mentioned as ylist already sorted, ensure it are we sorted the points at all flow.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@copilot can you check this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checked it: GenerateSegments() sorts yList with Array.Sort(yList) before the quartile/whisker/min-max path, and the array is not modified afterwards. I pushed f889217 to make that invariant explicit in the code comments next to the sort and direct index access.

Agent-Logs-Url: https://github.com/syncfusion/maui-toolkit/sessions/deb0ed97-498d-47eb-90c1-f8c8805a10fd

Co-authored-by: PaulAndersonS <42271912+PaulAndersonS@users.noreply.github.com>
Copy link
Copy Markdown
Collaborator

@SaiyathAliFathima SaiyathAliFathima left a comment

Choose a reason for hiding this comment

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

We have ensured the changes in all platforms and working fine.

Copy link
Copy Markdown
Collaborator

@SaiyathAliFathima SaiyathAliFathima left a comment

Choose a reason for hiding this comment

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

We have ensured the changes in all platforms and working fine.

@PaulAndersonS PaulAndersonS requested review from SaiyathAliFathima and removed request for SaiyathAliFathima May 26, 2026 14:39
@PaulAndersonS PaulAndersonS merged commit b165aac into main May 27, 2026
1 check passed
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.

4 participants