Perf: Replace O(n²) lookups with Dictionary in CategoryAxis.GroupData#371
Open
PaulAndersonS wants to merge 1 commit into
Open
Perf: Replace O(n²) lookups with Dictionary in CategoryAxis.GroupData#371PaulAndersonS wants to merge 1 commit into
PaulAndersonS wants to merge 1 commit into
Conversation
The GroupData() method used List.Contains() and List.IndexOf() inside loops, resulting in O(n²) time complexity for data grouping. This is especially impactful for charts with large datasets. Changes: - Replace List.Contains() with HashSet.Add() for O(1) deduplication - Build a Dictionary<string, int> for O(1) index lookups instead of O(n) List.IndexOf() calls per element - Use explicit null-safe pattern matching for ActualXValues - Pre-allocate lists with known capacity Added 5 unit tests covering: - Duplicate values - Double (numeric) X values - Multiple series with overlapping values - Single series - Large dataset (1000+ items) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
The
CategoryAxis.GroupData()method had two O(n²) performance bottlenecks:List.Contains()in a loop (line 45): For each x-value,groupingValues.Contains()performs a linear scan of the list to check for duplicates — O(n) per call × n calls = O(n²).List.IndexOf()in a LINQ query (lines 73, 77): For each value in a series,distinctXValues.IndexOf(val)performs a linear search — O(n) per call × n calls = O(n²).For charts with large datasets (thousands of data points), these patterns cause noticeable delays during chart initialization and data grouping.
Description of Change
List.Contains()withHashSet.Add()— provides O(1) amortized lookup for deduplication while maintaining insertion order via the parallel list.Dictionary<string, int>index lookup — maps each distinct x-value to its index for O(1) lookups instead of O(n)List.IndexOf()calls.ActualXValuestype checks (is List<double>instead ofas List<double>).Complexity improvement: O(n²) → O(n) for the grouping operation.
Unit Tests Added
5 new unit tests covering:
Issues Fixed
N/A — Proactive performance improvement
Screenshots
N/A — No visual changes