Skip to content

Commit 0d85f8d

Browse files
peffdscho
authored andcommitted
object-file: drop oid field from find_cached_object() return value
The pretend_object_file() function adds to an array mapping oids to object contents, which are later retrieved with find_cached_object(). We naturally need to store the oid for each entry, since it's the lookup key. But find_cached_object() also returns a hard-coded empty_tree object. There we don't care about its oid field and instead compare against the_hash_algo->empty_tree. The oid field is left as all-zeroes. This all works, but it means that the cached_object struct we return from find_cached_object() may or may not have a valid oid field, depend whether it is the hard-coded tree or came from pretend_object_file(). Nobody looks at the field, so there's no bug. But let's future-proof it by returning only the object contents themselves, not the oid. We'll continue to call this "struct cached_object", and the array entry mapping the key to those contents will be a "cached_object_entry". This would also let us swap out the array for a better data structure (like a hashmap) if we chose, but there's not much point. The only code that adds an entry is git-blame, which adds at most a single entry per process. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 216c405 commit 0d85f8d

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

object-file.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,27 +317,28 @@ int hash_algo_by_length(int len)
317317
* to write them into the object store (e.g. a browse-only
318318
* application).
319319
*/
320-
static struct cached_object {
320+
static struct cached_object_entry {
321321
struct object_id oid;
322-
enum object_type type;
323-
const void *buf;
324-
unsigned long size;
322+
struct cached_object {
323+
enum object_type type;
324+
const void *buf;
325+
unsigned long size;
326+
} value;
325327
} *cached_objects;
326328
static int cached_object_nr, cached_object_alloc;
327329

328330
static struct cached_object *find_cached_object(const struct object_id *oid)
329331
{
330332
static struct cached_object empty_tree = {
331-
/* no oid needed; we'll look it up manually based on the_hash_algo */
332333
.type = OBJ_TREE,
333334
.buf = "",
334335
};
335336
int i;
336-
struct cached_object *co = cached_objects;
337+
struct cached_object_entry *co = cached_objects;
337338

338339
for (i = 0; i < cached_object_nr; i++, co++) {
339340
if (oideq(&co->oid, oid))
340-
return co;
341+
return &co->value;
341342
}
342343
if (oideq(oid, the_hash_algo->empty_tree))
343344
return &empty_tree;
@@ -1850,7 +1851,7 @@ int oid_object_info(struct repository *r,
18501851
int pretend_object_file(void *buf, unsigned long len, enum object_type type,
18511852
struct object_id *oid)
18521853
{
1853-
struct cached_object *co;
1854+
struct cached_object_entry *co;
18541855
char *co_buf;
18551856

18561857
hash_object_file(the_hash_algo, buf, len, type, oid);
@@ -1859,11 +1860,11 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type,
18591860
return 0;
18601861
ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
18611862
co = &cached_objects[cached_object_nr++];
1862-
co->size = len;
1863-
co->type = type;
1863+
co->value.size = len;
1864+
co->value.type = type;
18641865
co_buf = xmalloc(len);
18651866
memcpy(co_buf, buf, len);
1866-
co->buf = co_buf;
1867+
co->value.buf = co_buf;
18671868
oidcpy(&co->oid, oid);
18681869
return 0;
18691870
}

0 commit comments

Comments
 (0)