Perf: Replace LINQ Min/Max with direct index access on sorted array in BoxAndWhiskerSeries#368
Merged
Merged
Conversation
… 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]; |
Collaborator
There was a problem hiding this comment.
mentioned as ylist already sorted, ensure it are we sorted the points at all flow.
Contributor
There was a problem hiding this comment.
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>
SaiyathAliFathima
approved these changes
May 26, 2026
Collaborator
SaiyathAliFathima
left a comment
There was a problem hiding this comment.
We have ensured the changes in all platforms and working fine.
SaiyathAliFathima
approved these changes
May 26, 2026
Collaborator
SaiyathAliFathima
left a comment
There was a problem hiding this comment.
We have ensured the changes in all platforms and working fine.
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
In
BoxAndWhiskerSeries.CreateSegments(), after theyListarray is sorted withArray.Sort(yList), the code usesyList.Min()andyList.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()andyList.Max()withyList[0]andyList[^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.