Skip to content

Commit 720e20e

Browse files
committed
Merge branch 'jc/commit-slab'
Memory use reduction when commit-slab facility is used to annotate sparsely (which is not recommended in the first place). * jc/commit-slab: commit-slab: introduce slabname##_peek() function
2 parents 2dded96 + 862e730 commit 720e20e

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

commit-slab.h

+29-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
* - int *indegree_at(struct indegree *, struct commit *);
1616
*
1717
* This function locates the data associated with the given commit in
18-
* the indegree slab, and returns the pointer to it.
18+
* the indegree slab, and returns the pointer to it. The location to
19+
* store the data is allocated as necessary.
20+
*
21+
* - int *indegree_peek(struct indegree *, struct commit *);
22+
*
23+
* This function is similar to indegree_at(), but it will return NULL
24+
* until a call to indegree_at() was made for the commit.
1925
*
2026
* - void init_indegree(struct indegree *);
2127
* void init_indegree_with_stride(struct indegree *, int);
@@ -80,8 +86,9 @@ static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
8086
s->slab = NULL; \
8187
} \
8288
\
83-
static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
84-
const struct commit *c) \
89+
static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s, \
90+
const struct commit *c, \
91+
int add_if_missing) \
8592
{ \
8693
int nth_slab, nth_slot; \
8794
\
@@ -90,16 +97,33 @@ static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
9097
\
9198
if (s->slab_count <= nth_slab) { \
9299
int i; \
100+
if (!add_if_missing) \
101+
return NULL; \
93102
REALLOC_ARRAY(s->slab, nth_slab + 1); \
94103
stat_ ##slabname## realloc++; \
95104
for (i = s->slab_count; i <= nth_slab; i++) \
96105
s->slab[i] = NULL; \
97106
s->slab_count = nth_slab + 1; \
98107
} \
99-
if (!s->slab[nth_slab]) \
108+
if (!s->slab[nth_slab]) { \
109+
if (!add_if_missing) \
110+
return NULL; \
100111
s->slab[nth_slab] = xcalloc(s->slab_size, \
101112
sizeof(**s->slab) * s->stride); \
102-
return &s->slab[nth_slab][nth_slot * s->stride]; \
113+
} \
114+
return &s->slab[nth_slab][nth_slot * s->stride]; \
115+
} \
116+
\
117+
static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
118+
const struct commit *c) \
119+
{ \
120+
return slabname##_at_peek(s, c, 1); \
121+
} \
122+
\
123+
static MAYBE_UNUSED elemtype *slabname## _peek(struct slabname *s, \
124+
const struct commit *c) \
125+
{ \
126+
return slabname##_at_peek(s, c, 0); \
103127
} \
104128
\
105129
static int stat_ ##slabname## realloc

commit.c

+20-8
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,12 @@ void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
245245

246246
const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
247247
{
248-
struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
248+
struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
249+
if (!v) {
250+
if (sizep)
251+
*sizep = 0;
252+
return NULL;
253+
}
249254
if (sizep)
250255
*sizep = v->size;
251256
return v->buffer;
@@ -272,24 +277,31 @@ const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
272277

273278
void unuse_commit_buffer(const struct commit *commit, const void *buffer)
274279
{
275-
struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
276-
if (v->buffer != buffer)
280+
struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
281+
if (!(v && v->buffer == buffer))
277282
free((void *)buffer);
278283
}
279284

280285
void free_commit_buffer(struct commit *commit)
281286
{
282-
struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
283-
free(v->buffer);
284-
v->buffer = NULL;
285-
v->size = 0;
287+
struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
288+
if (v) {
289+
free(v->buffer);
290+
v->buffer = NULL;
291+
v->size = 0;
292+
}
286293
}
287294

288295
const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
289296
{
290-
struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
297+
struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
291298
void *ret;
292299

300+
if (!v) {
301+
if (sizep)
302+
*sizep = 0;
303+
return NULL;
304+
}
293305
ret = v->buffer;
294306
if (sizep)
295307
*sizep = v->size;

0 commit comments

Comments
 (0)