Skip to content

Commit e2ed70f

Browse files
derrickstoleeJeff Hostetler
and
Jeff Hostetler
committed
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 4666de1 commit e2ed70f

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

builtin/survey.c

+113
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,119 @@ struct survey_report_object_size_summary {
7777
size_t num_missing;
7878
};
7979

80+
typedef int (*survey_top_cmp)(void *v1, void *v2);
81+
82+
MAYBE_UNUSED
83+
static int cmp_by_nr(void *v1, void *v2)
84+
{
85+
struct survey_report_object_size_summary *s1 = v1;
86+
struct survey_report_object_size_summary *s2 = v2;
87+
88+
if (s1->nr < s2->nr)
89+
return -1;
90+
if (s1->nr > s2->nr)
91+
return 1;
92+
return 0;
93+
}
94+
95+
MAYBE_UNUSED
96+
static int cmp_by_disk_size(void *v1, void *v2)
97+
{
98+
struct survey_report_object_size_summary *s1 = v1;
99+
struct survey_report_object_size_summary *s2 = v2;
100+
101+
if (s1->disk_size < s2->disk_size)
102+
return -1;
103+
if (s1->disk_size > s2->disk_size)
104+
return 1;
105+
return 0;
106+
}
107+
108+
MAYBE_UNUSED
109+
static int cmp_by_inflated_size(void *v1, void *v2)
110+
{
111+
struct survey_report_object_size_summary *s1 = v1;
112+
struct survey_report_object_size_summary *s2 = v2;
113+
114+
if (s1->inflated_size < s2->inflated_size)
115+
return -1;
116+
if (s1->inflated_size > s2->inflated_size)
117+
return 1;
118+
return 0;
119+
}
120+
121+
/**
122+
* Store a list of "top" categories by some sorting function. When
123+
* inserting a new category, reorder the list and free the one that
124+
* got ejected (if any).
125+
*/
126+
struct survey_report_top_table {
127+
const char *name;
128+
survey_top_cmp cmp_fn;
129+
size_t nr;
130+
size_t alloc;
131+
132+
/**
133+
* 'data' stores an array of structs and must be cast into
134+
* the proper array type before evaluating an index.
135+
*/
136+
void *data;
137+
};
138+
139+
MAYBE_UNUSED
140+
static void init_top_sizes(struct survey_report_top_table *top,
141+
size_t limit, const char *name,
142+
survey_top_cmp cmp)
143+
{
144+
struct survey_report_object_size_summary *sz_array;
145+
146+
top->name = name;
147+
top->cmp_fn = cmp;
148+
top->alloc = limit;
149+
top->nr = 0;
150+
151+
CALLOC_ARRAY(sz_array, limit);
152+
top->data = sz_array;
153+
}
154+
155+
MAYBE_UNUSED
156+
static void clear_top_sizes(struct survey_report_top_table *top)
157+
{
158+
struct survey_report_object_size_summary *sz_array = top->data;
159+
160+
for (size_t i = 0; i < top->nr; i++)
161+
free(sz_array[i].label);
162+
free(sz_array);
163+
}
164+
165+
MAYBE_UNUSED
166+
static void maybe_insert_into_top_size(struct survey_report_top_table *top,
167+
struct survey_report_object_size_summary *summary)
168+
{
169+
struct survey_report_object_size_summary *sz_array = top->data;
170+
size_t pos = top->nr;
171+
172+
/* Compare against list from the bottom. */
173+
while (pos > 0 && top->cmp_fn(&sz_array[pos - 1], summary) < 0)
174+
pos--;
175+
176+
/* Not big enough! */
177+
if (pos >= top->alloc)
178+
return;
179+
180+
/* We need to shift the data. */
181+
if (top->nr == top->alloc)
182+
free(sz_array[top->nr - 1].label);
183+
else
184+
top->nr++;
185+
186+
for (size_t i = top->nr - 1; i > pos; i--)
187+
memcpy(&sz_array[i], &sz_array[i - 1], sizeof(*sz_array));
188+
189+
memcpy(&sz_array[pos], summary, sizeof(*summary));
190+
sz_array[pos].label = xstrdup(summary->label);
191+
}
192+
80193
/**
81194
* This struct contains all of the information that needs to be printed
82195
* at the end of the exploration of the repository and its references.

0 commit comments

Comments
 (0)