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

Commit 88076e4

Browse files
captain5050namhyung
authored andcommitted
perf comm str: Avoid sort during insert
The array is sorted, so just move the elements and insert in order. Fixes: 13ca628 ("perf comm: Add reference count checking to 'struct comm_str'") Reported-by: Matt Fleming <matt@readmodwrite.com> Signed-off-by: Ian Rogers <irogers@google.com> Tested-by: Matt Fleming <matt@readmodwrite.com> Cc: Steinar Gunderson <sesse@google.com> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Link: https://lore.kernel.org/r/20240703172117.810918-2-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 83a7eef commit 88076e4

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

tools/perf/util/comm.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,6 @@ static struct comm_str *comm_str__new(const char *str)
8686
return result;
8787
}
8888

89-
static int comm_str__cmp(const void *_lhs, const void *_rhs)
90-
{
91-
const struct comm_str *lhs = *(const struct comm_str * const *)_lhs;
92-
const struct comm_str *rhs = *(const struct comm_str * const *)_rhs;
93-
94-
return strcmp(comm_str__str(lhs), comm_str__str(rhs));
95-
}
96-
9789
static int comm_str__search(const void *_key, const void *_member)
9890
{
9991
const char *key = _key;
@@ -169,9 +161,24 @@ static struct comm_str *comm_strs__findnew(const char *str)
169161
}
170162
result = comm_str__new(str);
171163
if (result) {
172-
comm_strs->strs[comm_strs->num_strs++] = result;
173-
qsort(comm_strs->strs, comm_strs->num_strs, sizeof(struct comm_str *),
174-
comm_str__cmp);
164+
int low = 0, high = comm_strs->num_strs - 1;
165+
int insert = comm_strs->num_strs; /* Default to inserting at the end. */
166+
167+
while (low <= high) {
168+
int mid = low + (high - low) / 2;
169+
int cmp = strcmp(comm_str__str(comm_strs->strs[mid]), str);
170+
171+
if (cmp < 0) {
172+
low = mid + 1;
173+
} else {
174+
high = mid - 1;
175+
insert = mid;
176+
}
177+
}
178+
memmove(&comm_strs->strs[insert + 1], &comm_strs->strs[insert],
179+
(comm_strs->num_strs - insert) * sizeof(struct comm_str *));
180+
comm_strs->num_strs++;
181+
comm_strs->strs[insert] = result;
175182
}
176183
}
177184
up_write(&comm_strs->lock);

0 commit comments

Comments
 (0)