Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2580077

Browse files
committed
tree: calculate the filename length once
We already know the size due to the `memchr()` so use that information instead of calling `strlen()` on it.
1 parent ed97074 commit 2580077

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/tree.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2)
8686
* owns it. This is useful when reading trees, so we don't allocate a
8787
* ton of small strings but can use the pool.
8888
*/
89-
static git_tree_entry *alloc_entry_pooled(git_pool *pool, const char *filename)
89+
static git_tree_entry *alloc_entry_pooled(git_pool *pool, const char *filename, size_t filename_len)
9090
{
9191
git_tree_entry *entry = NULL;
92-
size_t filename_len = strlen(filename), tree_len;
92+
size_t tree_len;
9393

9494
if (GIT_ADD_SIZET_OVERFLOW(&tree_len, sizeof(git_tree_entry), filename_len) ||
9595
GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1) ||
@@ -416,6 +416,8 @@ int git_tree__parse(void *_tree, git_odb_object *odb_obj)
416416

417417
while (buffer < buffer_end) {
418418
git_tree_entry *entry;
419+
size_t filename_len;
420+
const char *nul;
419421
int attr;
420422

421423
if (git__strtol32(&attr, buffer, &buffer, 8) < 0 || !buffer)
@@ -424,12 +426,13 @@ int git_tree__parse(void *_tree, git_odb_object *odb_obj)
424426
if (*buffer++ != ' ')
425427
return tree_error("Failed to parse tree. Object is corrupted", NULL);
426428

427-
if (memchr(buffer, 0, buffer_end - buffer) == NULL)
429+
if ((nul = memchr(buffer, 0, buffer_end - buffer)) == NULL)
428430
return tree_error("Failed to parse tree. Object is corrupted", NULL);
429431

432+
filename_len = nul - buffer;
430433
/** Allocate the entry and store it in the entries vector */
431434
{
432-
entry = alloc_entry_pooled(&tree->pool, buffer);
435+
entry = alloc_entry_pooled(&tree->pool, buffer, filename_len);
433436
GITERR_CHECK_ALLOC(entry);
434437

435438
if (git_vector_insert(&tree->entries, entry) < 0)
@@ -439,7 +442,7 @@ int git_tree__parse(void *_tree, git_odb_object *odb_obj)
439442
}
440443

441444
/* Advance to the ID just after the path */
442-
buffer += entry->filename_len + 1;
445+
buffer += filename_len + 1;
443446

444447
git_oid_fromraw(&entry->oid, (const unsigned char *)buffer);
445448
buffer += GIT_OID_RAWSZ;

0 commit comments

Comments
 (0)