Commit 122c3405 by Vicent Marti

Git trees are now always lazily sorted

Removed `git_tree_add_entry_unsorted`. Now the `git_tree_add_entry`
method doesn't sort the entries array by default; entries are only
sorted lazily when required. This is done automatically by the library
(the `git_tree_sort_entries` call has been removed).

This should improve performance. No point on sorting entries all the time, anyway.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
parent 8212e2d7
...@@ -149,36 +149,6 @@ GIT_EXTERN(int) git_tree_entry_2object(git_object **object, git_tree_entry *entr ...@@ -149,36 +149,6 @@ GIT_EXTERN(int) git_tree_entry_2object(git_object **object, git_tree_entry *entr
GIT_EXTERN(int) git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes); GIT_EXTERN(int) git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes);
/** /**
* Add a new entry to a tree, returning that new entry.
* The only difference with this call is that it does not sort
* tree afterwards, this requirement is left to the caller.
*
* This will mark the tree as modified; the new entry will
* be written back to disk on the next git_object_write()
*
* @param entry Entry object which will be created
* @param tree Tree object to store the entry
* @iparam id OID for the tree entry
* @param filename Filename for the tree entry
* @param attributes UNIX file attributes for the entry
* @return 0 on success; otherwise error code
*/
GIT_EXTERN(int) git_tree_add_entry_unsorted(git_tree_entry **entry, git_tree *tree, const git_oid *id, const char *filename, int attributes);
/**
* Sort the entries in a tree created using git_tree_add_entry2.
*
* This does not mark the tree as modified. It is intended to be used
* after several invocations of git_tree_add_entry2.
* git_tree_add_entry, on the other hand, sorts after each entry is
* added.
*
* @param tree Tree object whose entries are to be sorted
* @return 0 on success; otherwise error code
*/
GIT_EXTERN(int) git_tree_sort_entries(git_tree *tree);
/**
* Remove an entry by its index. * Remove an entry by its index.
* *
* Index must be >= 0 and < than git_tree_entrycount(). * Index must be >= 0 and < than git_tree_entrycount().
......
...@@ -174,6 +174,14 @@ int git_tree_entry_2object(git_object **object_out, git_tree_entry *entry) ...@@ -174,6 +174,14 @@ int git_tree_entry_2object(git_object **object_out, git_tree_entry *entry)
return git_repository_lookup(object_out, entry->owner->object.repo, &entry->oid, GIT_OBJ_ANY); return git_repository_lookup(object_out, entry->owner->object.repo, &entry->oid, GIT_OBJ_ANY);
} }
static void sort_entries(git_tree *tree)
{
if (tree->sorted == 0) {
git_vector_sort(&tree->entries);
tree->sorted = 1;
}
}
git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename) git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
{ {
int idx; int idx;
...@@ -181,7 +189,7 @@ git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename) ...@@ -181,7 +189,7 @@ git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
assert(tree && filename); assert(tree && filename);
if (!tree->sorted) if (!tree->sorted)
git_tree_sort_entries(tree); sort_entries(tree);
idx = git_vector_search(&tree->entries, filename); idx = git_vector_search(&tree->entries, filename);
if (idx == GIT_ENOTFOUND) if (idx == GIT_ENOTFOUND)
...@@ -195,7 +203,7 @@ git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx) ...@@ -195,7 +203,7 @@ git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
assert(tree); assert(tree);
if (!tree->sorted) if (!tree->sorted)
git_tree_sort_entries(tree); sort_entries(tree);
return git_vector_get(&tree->entries, (unsigned int)idx); return git_vector_get(&tree->entries, (unsigned int)idx);
} }
...@@ -206,7 +214,7 @@ size_t git_tree_entrycount(git_tree *tree) ...@@ -206,7 +214,7 @@ size_t git_tree_entrycount(git_tree *tree)
return tree->entries.length; return tree->entries.length;
} }
int git_tree_add_entry_unsorted(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes) int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes)
{ {
git_tree_entry *entry; git_tree_entry *entry;
...@@ -233,22 +241,6 @@ int git_tree_add_entry_unsorted(git_tree_entry **entry_out, git_tree *tree, cons ...@@ -233,22 +241,6 @@ int git_tree_add_entry_unsorted(git_tree_entry **entry_out, git_tree *tree, cons
return GIT_SUCCESS; return GIT_SUCCESS;
} }
int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes)
{
int result = git_tree_add_entry_unsorted(entry_out, tree, id, filename, attributes);
if (result == GIT_SUCCESS)
git_tree_sort_entries(tree);
return result;
}
int git_tree_sort_entries(git_tree *tree)
{
git_vector_sort(&tree->entries);
tree->sorted = 1;
return GIT_SUCCESS;
}
int git_tree_remove_entry_byindex(git_tree *tree, int idx) int git_tree_remove_entry_byindex(git_tree *tree, int idx)
{ {
git_tree_entry *remove_ptr; git_tree_entry *remove_ptr;
...@@ -256,7 +248,7 @@ int git_tree_remove_entry_byindex(git_tree *tree, int idx) ...@@ -256,7 +248,7 @@ int git_tree_remove_entry_byindex(git_tree *tree, int idx)
assert(tree); assert(tree);
if (!tree->sorted) if (!tree->sorted)
git_tree_sort_entries(tree); sort_entries(tree);
remove_ptr = git_vector_get(&tree->entries, (unsigned int)idx); remove_ptr = git_vector_get(&tree->entries, (unsigned int)idx);
if (remove_ptr == NULL) if (remove_ptr == NULL)
...@@ -277,7 +269,7 @@ int git_tree_remove_entry_byname(git_tree *tree, const char *filename) ...@@ -277,7 +269,7 @@ int git_tree_remove_entry_byname(git_tree *tree, const char *filename)
assert(tree && filename); assert(tree && filename);
if (!tree->sorted) if (!tree->sorted)
git_tree_sort_entries(tree); sort_entries(tree);
idx = git_vector_search(&tree->entries, filename); idx = git_vector_search(&tree->entries, filename);
if (idx == GIT_ENOTFOUND) if (idx == GIT_ENOTFOUND)
...@@ -297,7 +289,7 @@ int git_tree__writeback(git_tree *tree, git_odb_source *src) ...@@ -297,7 +289,7 @@ int git_tree__writeback(git_tree *tree, git_odb_source *src)
return GIT_EMISSINGOBJDATA; return GIT_EMISSINGOBJDATA;
if (!tree->sorted) if (!tree->sorted)
git_tree_sort_entries(tree); sort_entries(tree);
for (i = 0; i < tree->entries.length; ++i) { for (i = 0; i < tree->entries.length; ++i) {
git_tree_entry *entry; git_tree_entry *entry;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment