Skip to content
/ git Public
forked from git/git

Commit

Permalink
ref_transaction_commit(): remove local variables n and updates
Browse files Browse the repository at this point in the history
These microoptimizations don't make a significant difference in speed.
And they cause problems if somebody ever wants to modify the function to
add updates to a transaction as part of processing it, as will happen
shortly.

Make the same changes in initial_ref_transaction_commit().

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
  • Loading branch information
mhagger committed Jun 13, 2016
1 parent e711b1a commit efe4728
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions refs/files-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -3076,8 +3076,6 @@ int ref_transaction_commit(struct ref_transaction *transaction,
struct strbuf *err)
{
int ret = 0, i;
int n = transaction->nr;
struct ref_update **updates = transaction->updates;
struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
struct string_list_item *ref_to_delete;
struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
Expand All @@ -3087,14 +3085,15 @@ int ref_transaction_commit(struct ref_transaction *transaction,
if (transaction->state != REF_TRANSACTION_OPEN)
die("BUG: commit called for transaction that is not open");

if (!n) {
if (!transaction->nr) {
transaction->state = REF_TRANSACTION_CLOSED;
return 0;
}

/* Fail if a refname appears more than once in the transaction: */
for (i = 0; i < n; i++)
string_list_append(&affected_refnames, updates[i]->refname);
for (i = 0; i < transaction->nr; i++)
string_list_append(&affected_refnames,
transaction->updates[i]->refname);
string_list_sort(&affected_refnames);
if (ref_update_reject_duplicates(&affected_refnames, err)) {
ret = TRANSACTION_GENERIC_ERROR;
Expand All @@ -3107,8 +3106,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
* lockfiles, ready to be activated. Only keep one lockfile
* open at a time to avoid running out of file descriptors.
*/
for (i = 0; i < n; i++) {
struct ref_update *update = updates[i];
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];

if ((update->flags & REF_HAVE_NEW) &&
is_null_sha1(update->new_sha1))
Expand Down Expand Up @@ -3178,8 +3177,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
}

/* Perform updates first so live commits remain referenced */
for (i = 0; i < n; i++) {
struct ref_update *update = updates[i];
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];

if (update->flags & REF_NEEDS_COMMIT) {
if (commit_ref_update(update->lock,
Expand All @@ -3197,8 +3196,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
}

/* Perform deletes now that updates are safely completed */
for (i = 0; i < n; i++) {
struct ref_update *update = updates[i];
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];

if (update->flags & REF_DELETING) {
if (delete_ref_loose(update->lock, update->type, err)) {
Expand All @@ -3223,9 +3222,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,
cleanup:
transaction->state = REF_TRANSACTION_CLOSED;

for (i = 0; i < n; i++)
if (updates[i]->lock)
unlock_ref(updates[i]->lock);
for (i = 0; i < transaction->nr; i++)
if (transaction->updates[i]->lock)
unlock_ref(transaction->updates[i]->lock);
string_list_clear(&refs_to_delete, 0);
string_list_clear(&affected_refnames, 0);
return ret;
Expand All @@ -3243,8 +3242,6 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
struct strbuf *err)
{
int ret = 0, i;
int n = transaction->nr;
struct ref_update **updates = transaction->updates;
struct string_list affected_refnames = STRING_LIST_INIT_NODUP;

assert(err);
Expand All @@ -3253,8 +3250,9 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
die("BUG: commit called for transaction that is not open");

/* Fail if a refname appears more than once in the transaction: */
for (i = 0; i < n; i++)
string_list_append(&affected_refnames, updates[i]->refname);
for (i = 0; i < transaction->nr; i++)
string_list_append(&affected_refnames,
transaction->updates[i]->refname);
string_list_sort(&affected_refnames);
if (ref_update_reject_duplicates(&affected_refnames, err)) {
ret = TRANSACTION_GENERIC_ERROR;
Expand All @@ -3276,8 +3274,8 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
if (for_each_rawref(ref_present, &affected_refnames))
die("BUG: initial ref transaction called with existing refs");

for (i = 0; i < n; i++) {
struct ref_update *update = updates[i];
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];

if ((update->flags & REF_HAVE_OLD) &&
!is_null_sha1(update->old_sha1))
Expand All @@ -3297,8 +3295,8 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
goto cleanup;
}

for (i = 0; i < n; i++) {
struct ref_update *update = updates[i];
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];

if ((update->flags & REF_HAVE_NEW) &&
!is_null_sha1(update->new_sha1))
Expand Down

0 comments on commit efe4728

Please sign in to comment.