Commit 1059fb52 authored by Ian Rogers's avatar Ian Rogers Committed by Namhyung Kim

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: 3f4ac23a ("perf dsos: Switch backing storage to array from rbtree/list")
Reported-by: default avatarNamhyung Kim <namhyung@kernel.org>
Signed-off-by: default avatarIan 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.comSigned-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
parent feaaa8be
...@@ -206,11 +206,27 @@ int __dsos__add(struct dsos *dsos, struct dso *dso) ...@@ -206,11 +206,27 @@ int __dsos__add(struct dsos *dsos, struct dso *dso)
dsos->dsos = temp; dsos->dsos = temp;
dsos->allocated = to_allocate; dsos->allocated = to_allocate;
} }
if (!dsos->sorted) {
dsos->dsos[dsos->cnt++] = dso__get(dso); dsos->dsos[dsos->cnt++] = dso__get(dso);
if (dsos->cnt >= 2 && dsos->sorted) { } else {
dsos->sorted = dsos__cmp_long_name_id_short_name(&dsos->dsos[dsos->cnt - 2], int low = 0, high = dsos->cnt - 1;
&dsos->dsos[dsos->cnt - 1]) int insert = dsos->cnt; /* Default to inserting at the end. */
<= 0;
while (low <= high) {
int mid = low + (high - low) / 2;
int cmp = dsos__cmp_long_name_id_short_name(&dsos->dsos[mid], &dso);
if (cmp < 0) {
low = mid + 1;
} else {
high = mid - 1;
insert = mid;
}
}
memmove(&dsos->dsos[insert + 1], &dsos->dsos[insert],
(dsos->cnt - insert) * sizeof(struct dso *));
dsos->cnt++;
dsos->dsos[insert] = dso__get(dso);
} }
dso__set_dsos(dso, dsos); dso__set_dsos(dso, dsos);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment