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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
b2a95dfd63
commit
9202ffcf10
@ -317,27 +317,28 @@ int hash_algo_by_length(int len)
|
|||||||
* to write them into the object store (e.g. a browse-only
|
* to write them into the object store (e.g. a browse-only
|
||||||
* application).
|
* application).
|
||||||
*/
|
*/
|
||||||
static struct cached_object {
|
static struct cached_object_entry {
|
||||||
struct object_id oid;
|
struct object_id oid;
|
||||||
|
struct cached_object {
|
||||||
enum object_type type;
|
enum object_type type;
|
||||||
const void *buf;
|
const void *buf;
|
||||||
unsigned long size;
|
unsigned long size;
|
||||||
|
} value;
|
||||||
} *cached_objects;
|
} *cached_objects;
|
||||||
static int cached_object_nr, cached_object_alloc;
|
static int cached_object_nr, cached_object_alloc;
|
||||||
|
|
||||||
static struct cached_object *find_cached_object(const struct object_id *oid)
|
static struct cached_object *find_cached_object(const struct object_id *oid)
|
||||||
{
|
{
|
||||||
static struct cached_object empty_tree = {
|
static struct cached_object empty_tree = {
|
||||||
/* no oid needed; we'll look it up manually based on the_hash_algo */
|
|
||||||
.type = OBJ_TREE,
|
.type = OBJ_TREE,
|
||||||
.buf = "",
|
.buf = "",
|
||||||
};
|
};
|
||||||
int i;
|
int i;
|
||||||
struct cached_object *co = cached_objects;
|
struct cached_object_entry *co = cached_objects;
|
||||||
|
|
||||||
for (i = 0; i < cached_object_nr; i++, co++) {
|
for (i = 0; i < cached_object_nr; i++, co++) {
|
||||||
if (oideq(&co->oid, oid))
|
if (oideq(&co->oid, oid))
|
||||||
return co;
|
return &co->value;
|
||||||
}
|
}
|
||||||
if (oideq(oid, the_hash_algo->empty_tree))
|
if (oideq(oid, the_hash_algo->empty_tree))
|
||||||
return &empty_tree;
|
return &empty_tree;
|
||||||
@ -1850,7 +1851,7 @@ int oid_object_info(struct repository *r,
|
|||||||
int pretend_object_file(void *buf, unsigned long len, enum object_type type,
|
int pretend_object_file(void *buf, unsigned long len, enum object_type type,
|
||||||
struct object_id *oid)
|
struct object_id *oid)
|
||||||
{
|
{
|
||||||
struct cached_object *co;
|
struct cached_object_entry *co;
|
||||||
char *co_buf;
|
char *co_buf;
|
||||||
|
|
||||||
hash_object_file(the_hash_algo, buf, len, type, oid);
|
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,
|
|||||||
return 0;
|
return 0;
|
||||||
ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
|
ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
|
||||||
co = &cached_objects[cached_object_nr++];
|
co = &cached_objects[cached_object_nr++];
|
||||||
co->size = len;
|
co->value.size = len;
|
||||||
co->type = type;
|
co->value.type = type;
|
||||||
co_buf = xmalloc(len);
|
co_buf = xmalloc(len);
|
||||||
memcpy(co_buf, buf, len);
|
memcpy(co_buf, buf, len);
|
||||||
co->buf = co_buf;
|
co->value.buf = co_buf;
|
||||||
oidcpy(&co->oid, oid);
|
oidcpy(&co->oid, oid);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user