Skip to content

Commit 1059fb5

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 <[email protected]> Signed-off-by: Ian Rogers <[email protected]> Cc: Steinar Gunderson <[email protected]> Cc: Athira Rajeev <[email protected]> Cc: Matt Fleming <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent feaaa8b commit 1059fb5

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
@@ -206,11 +206,27 @@ int __dsos__add(struct dsos *dsos, struct dso *dso)
206206
dsos->dsos = temp;
207207
dsos->allocated = to_allocate;
208208
}
209-
dsos->dsos[dsos->cnt++] = dso__get(dso);
210-
if (dsos->cnt >= 2 && dsos->sorted) {
211-
dsos->sorted = dsos__cmp_long_name_id_short_name(&dsos->dsos[dsos->cnt - 2],
212-
&dsos->dsos[dsos->cnt - 1])
213-
<= 0;
209+
if (!dsos->sorted) {
210+
dsos->dsos[dsos->cnt++] = dso__get(dso);
211+
} else {
212+
int low = 0, high = dsos->cnt - 1;
213+
int insert = dsos->cnt; /* Default to inserting at the end. */
214+
215+
while (low <= high) {
216+
int mid = low + (high - low) / 2;
217+
int cmp = dsos__cmp_long_name_id_short_name(&dsos->dsos[mid], &dso);
218+
219+
if (cmp < 0) {
220+
low = mid + 1;
221+
} else {
222+
high = mid - 1;
223+
insert = mid;
224+
}
225+
}
226+
memmove(&dsos->dsos[insert + 1], &dsos->dsos[insert],
227+
(dsos->cnt - insert) * sizeof(struct dso *));
228+
dsos->cnt++;
229+
dsos->dsos[insert] = dso__get(dso);
214230
}
215231
dso__set_dsos(dso, dsos);
216232
return 0;

0 commit comments

Comments
 (0)