mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-09 20:29:27 +00:00
Minor tree cache speedups
While I was looking at the conflict cleanup code, I looked over at the tree cache code, since we clear the tree cache for each entry that gets removed and there is some redundancy there. I made some small tweaks to avoid extra calls to strchr and strlen in a few circumstances.
This commit is contained in:
parent
aba6b5edbd
commit
1fa17b5c92
@ -7,23 +7,16 @@
|
|||||||
|
|
||||||
#include "tree-cache.h"
|
#include "tree-cache.h"
|
||||||
|
|
||||||
static git_tree_cache *find_child(const git_tree_cache *tree, const char *path)
|
static git_tree_cache *find_child(
|
||||||
|
const git_tree_cache *tree, const char *path, const char *end)
|
||||||
{
|
{
|
||||||
size_t i, dirlen;
|
size_t i, dirlen = end ? (size_t)(end - path) : strlen(path);
|
||||||
const char *end;
|
|
||||||
|
|
||||||
end = strchr(path, '/');
|
|
||||||
if (end == NULL) {
|
|
||||||
end = strrchr(path, '\0');
|
|
||||||
}
|
|
||||||
|
|
||||||
dirlen = end - path;
|
|
||||||
|
|
||||||
for (i = 0; i < tree->children_count; ++i) {
|
for (i = 0; i < tree->children_count; ++i) {
|
||||||
const char *childname = tree->children[i]->name;
|
git_tree_cache *child = tree->children[i];
|
||||||
|
|
||||||
if (strlen(childname) == dirlen && !memcmp(path, childname, dirlen))
|
if (child->namelen == dirlen && !memcmp(path, child->name, dirlen))
|
||||||
return tree->children[i];
|
return child;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -44,7 +37,7 @@ void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
|
|||||||
if (end == NULL) /* End of path */
|
if (end == NULL) /* End of path */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
tree = find_child(tree, ptr);
|
tree = find_child(tree, ptr, end);
|
||||||
if (tree == NULL) /* We don't have that tree */
|
if (tree == NULL) /* We don't have that tree */
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -64,10 +57,9 @@ const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char
|
|||||||
while (1) {
|
while (1) {
|
||||||
end = strchr(ptr, '/');
|
end = strchr(ptr, '/');
|
||||||
|
|
||||||
tree = find_child(tree, ptr);
|
tree = find_child(tree, ptr, end);
|
||||||
if (tree == NULL) { /* Can't find it */
|
if (tree == NULL) /* Can't find it */
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
if (end == NULL || *end + 1 == '\0')
|
if (end == NULL || *end + 1 == '\0')
|
||||||
return tree;
|
return tree;
|
||||||
@ -100,6 +92,7 @@ static int read_tree_internal(git_tree_cache **out,
|
|||||||
tree->parent = parent;
|
tree->parent = parent;
|
||||||
|
|
||||||
/* NUL-terminated tree name */
|
/* NUL-terminated tree name */
|
||||||
|
tree->namelen = name_len;
|
||||||
memcpy(tree->name, name_start, name_len);
|
memcpy(tree->name, name_start, name_len);
|
||||||
tree->name[name_len] = '\0';
|
tree->name[name_len] = '\0';
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ struct git_tree_cache {
|
|||||||
|
|
||||||
ssize_t entries;
|
ssize_t entries;
|
||||||
git_oid oid;
|
git_oid oid;
|
||||||
|
size_t namelen;
|
||||||
char name[GIT_FLEX_ARRAY];
|
char name[GIT_FLEX_ARRAY];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user