Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 7b2450b

Browse files
captain5050namhyung
authored andcommitted
perf dsos: When adding a dso into sorted dsos maintain the sort order
dsos__add would add at the end of the dso array possibly requiring a later find to re-sort the array. Patterns of find then add were becoming O(n*log n) due to the sorts. Change the add routine to be O(n) rather than O(1) but to maintain the sorted-ness of the dsos array so that later finds don't need the O(n*log n) sort. Fixes: 3f4ac23 ("perf dsos: Switch backing storage to array from rbtree/list") Reported-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Ian Rogers <irogers@google.com> Cc: Steinar Gunderson <sesse@google.com> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Cc: Matt Fleming <matt@readmodwrite.com> Link: https://lore.kernel.org/r/20240703172117.810918-3-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 88076e4 commit 7b2450b

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

tools/perf/util/dsos.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,27 @@ int __dsos__add(struct dsos *dsos, struct dso *dso)
203203
dsos->dsos = temp;
204204
dsos->allocated = to_allocate;
205205
}
206-
dsos->dsos[dsos->cnt++] = dso__get(dso);
207-
if (dsos->cnt >= 2 && dsos->sorted) {
208-
dsos->sorted = dsos__cmp_long_name_id_short_name(&dsos->dsos[dsos->cnt - 2],
209-
&dsos->dsos[dsos->cnt - 1])
210-
<= 0;
206+
if (!dsos->sorted) {
207+
dsos->dsos[dsos->cnt++] = dso__get(dso);
208+
} else {
209+
int low = 0, high = dsos->cnt - 1;
210+
int insert = dsos->cnt; /* Default to inserting at the end. */
211+
212+
while (low <= high) {
213+
int mid = low + (high - low) / 2;
214+
int cmp = dsos__cmp_long_name_id_short_name(&dsos->dsos[mid], &dso);
215+
216+
if (cmp < 0) {
217+
low = mid + 1;
218+
} else {
219+
high = mid - 1;
220+
insert = mid;
221+
}
222+
}
223+
memmove(&dsos->dsos[insert + 1], &dsos->dsos[insert],
224+
(dsos->cnt - insert) * sizeof(struct dso *));
225+
dsos->cnt++;
226+
dsos->dsos[insert] = dso__get(dso);
211227
}
212228
dso__set_dsos(dso, dsos);
213229
return 0;

0 commit comments

Comments
 (0)