perf annotate: Sort list of recognised instructions
[deliverable/linux.git] / tools / perf / util / annotate.c
index 3d9f2ca2ed2db2f35ce10a170f74373289411074..7e5a1e8874cee4a263d2577f55fd3ddf7dde3a95 100644 (file)
@@ -354,9 +354,6 @@ static struct ins_ops nop_ops = {
        .scnprintf = nop__scnprintf,
 };
 
-/*
- * Must be sorted by name!
- */
 static struct ins instructions[] = {
        { .name = "add",   .ops  = &mov_ops, },
        { .name = "addl",  .ops  = &mov_ops, },
@@ -449,18 +446,39 @@ static struct ins instructions[] = {
        { .name = "xbeginq", .ops  = &jump_ops, },
 };
 
-static int ins__cmp(const void *name, const void *insp)
+static int ins__key_cmp(const void *name, const void *insp)
 {
        const struct ins *ins = insp;
 
        return strcmp(name, ins->name);
 }
 
+static int ins__cmp(const void *a, const void *b)
+{
+       const struct ins *ia = a;
+       const struct ins *ib = b;
+
+       return strcmp(ia->name, ib->name);
+}
+
+static void ins__sort(void)
+{
+       const int nmemb = ARRAY_SIZE(instructions);
+
+       qsort(instructions, nmemb, sizeof(struct ins), ins__cmp);
+}
+
 static struct ins *ins__find(const char *name)
 {
        const int nmemb = ARRAY_SIZE(instructions);
+       static bool sorted;
+
+       if (!sorted) {
+               ins__sort();
+               sorted = true;
+       }
 
-       return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
+       return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__key_cmp);
 }
 
 int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
This page took 0.025389 seconds and 5 git commands to generate.