Skip to content

Commit d886c14

Browse files
derrickstoleeJeff Hostetler
andcommitted
survey: add ability to track prioritized lists
In future changes, we will make use of these methods. The intention is to keep track of the top contributors according to some metric. We don't want to store all of the entries and do a sort at the end, so track a constant-size table and remove rows that get pushed out depending on the chosen sorting algorithm. Co-authored-by: Jeff Hostetler <[email protected]> Signed-off-by; Jeff Hostetler <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
1 parent 448892d commit d886c14

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

builtin/survey.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,102 @@ struct survey_report_object_size_summary {
7373
size_t num_missing;
7474
};
7575

76+
typedef int (*survey_top_size_cmp)(struct survey_report_object_size_summary *s1,
77+
struct survey_report_object_size_summary *s2);
78+
79+
MAYBE_UNUSED
80+
static int cmp_by_nr(struct survey_report_object_size_summary *s1,
81+
struct survey_report_object_size_summary *s2)
82+
{
83+
if (s1->nr < s2->nr)
84+
return -1;
85+
if (s1->nr > s2->nr)
86+
return 1;
87+
return 0;
88+
}
89+
90+
MAYBE_UNUSED
91+
static int cmp_by_disk_size(struct survey_report_object_size_summary *s1,
92+
struct survey_report_object_size_summary *s2)
93+
{
94+
if (s1->disk_size < s2->disk_size)
95+
return -1;
96+
if (s1->disk_size > s2->disk_size)
97+
return 1;
98+
return 0;
99+
}
100+
101+
MAYBE_UNUSED
102+
static int cmp_by_inflated_size(struct survey_report_object_size_summary *s1,
103+
struct survey_report_object_size_summary *s2)
104+
{
105+
if (s1->inflated_size < s2->inflated_size)
106+
return -1;
107+
if (s1->inflated_size > s2->inflated_size)
108+
return 1;
109+
return 0;
110+
}
111+
112+
/**
113+
* Store a list of "top" categories by some sorting function. When
114+
* inserting a new category, reorder the list and free the one that
115+
* got ejected (if any).
116+
*/
117+
struct survey_report_top_sizes {
118+
const char *name;
119+
survey_top_size_cmp cmp_fn;
120+
struct survey_report_object_size_summary *data;
121+
size_t nr;
122+
size_t alloc;
123+
};
124+
125+
MAYBE_UNUSED
126+
static void init_top_sizes(struct survey_report_top_sizes *top,
127+
size_t limit, const char *name,
128+
survey_top_size_cmp cmp)
129+
{
130+
top->name = name;
131+
top->alloc = limit;
132+
top->nr = 0;
133+
CALLOC_ARRAY(top->data, limit);
134+
top->cmp_fn = cmp;
135+
}
136+
137+
MAYBE_UNUSED
138+
static void clear_top_sizes(struct survey_report_top_sizes *top)
139+
{
140+
for (size_t i = 0; i < top->nr; i++)
141+
free(top->data[i].label);
142+
free(top->data);
143+
}
144+
145+
MAYBE_UNUSED
146+
static void maybe_insert_into_top_size(struct survey_report_top_sizes *top,
147+
struct survey_report_object_size_summary *summary)
148+
{
149+
size_t pos = top->nr;
150+
151+
/* Compare against list from the bottom. */
152+
while (pos > 0 && top->cmp_fn(&top->data[pos - 1], summary) < 0)
153+
pos--;
154+
155+
/* Not big enough! */
156+
if (pos >= top->alloc)
157+
return;
158+
159+
/* We need to shift the data. */
160+
if (top->nr == top->alloc)
161+
free(top->data[top->nr - 1].label);
162+
else
163+
top->nr++;
164+
165+
for (size_t i = top->nr - 1; i > pos; i--)
166+
memcpy(&top->data[i], &top->data[i - 1], sizeof(*top->data));
167+
168+
memcpy(&top->data[pos], summary, sizeof(*summary));
169+
top->data[pos].label = xstrdup(summary->label);
170+
}
171+
76172
/**
77173
* This struct contains all of the information that needs to be printed
78174
* at the end of the exploration of the repository and its references.

0 commit comments

Comments
 (0)