Skip to content

Commit 3db103e

Browse files
derrickstoleeJeff Hostetler
authored 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 02f6cab commit 3db103e

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
@@ -77,6 +77,102 @@ struct survey_report_object_size_summary {
7777
size_t num_missing;
7878
};
7979

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

0 commit comments

Comments
 (0)