Skip to content

Commit 43f06e8

Browse files
visitorckwalexdeucher
authored andcommitted
drm/amd/display: Optimize remove_duplicates() from O(N^2) to O(N)
Replace the previous O(N^2) implementation of remove_duplicates() with a O(N) version using a fast/slow pointer approach. The new version keeps only the first occurrence of each element and compacts the array in place, improving efficiency without changing functionality. Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Reviewed-by: Alex Hung <alex.hung@amd.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
1 parent 51cb93a commit 43f06e8

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

drivers/gpu/drm/amd/display/dc/dml2/dml21/src/dml2_pmo/dml2_pmo_dcn3.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@ static void set_reserved_time_on_all_planes_with_stream_index(struct display_con
4848

4949
static void remove_duplicates(double *list_a, int *list_a_size)
5050
{
51-
int cur_element = 0;
52-
// For all elements b[i] in list_b[]
53-
while (cur_element < *list_a_size - 1) {
54-
if (list_a[cur_element] == list_a[cur_element + 1]) {
55-
for (int j = cur_element + 1; j < *list_a_size - 1; j++) {
56-
list_a[j] = list_a[j + 1];
57-
}
58-
*list_a_size = *list_a_size - 1;
59-
} else {
60-
cur_element++;
51+
int j = 0;
52+
53+
if (*list_a_size == 0)
54+
return;
55+
56+
for (int i = 1; i < *list_a_size; i++) {
57+
if (list_a[j] != list_a[i]) {
58+
j++;
59+
list_a[j] = list_a[i];
6160
}
6261
}
62+
63+
*list_a_size = j + 1;
6364
}
6465

6566
static bool increase_mpc_combine_factor(unsigned int *mpc_combine_factor, unsigned int limit)

0 commit comments

Comments
 (0)