Skip to content

Commit 23c3251

Browse files
pks-tgitster
authored andcommitted
refs/reftable: stop micro-optimizing refname allocations on copy
When copying refs, we execute `write_copy_table()` to write the new table. As the names are given to us via `arg->newname` and `arg->oldname`, respectively, we optimize away some allocations by assigning those fields to the reftable records we are about to write directly, without duplicating them. This requires us to cast the input to `char *` pointers as they are in fact constant strings. Later on, we then unset the refname for all of the records before calling `reftable_log_record_release()` on them. We also do this when assigning the "HEAD" constant, but here we do not cast because its type is `char[]` by default. It's about to be turned into `const char *` though once we enable `-Wwrite-strings` and will thus cause another warning. It's quite dubious whether this micro-optimization really helps. We're about to write to disk anyway, which is going to be way slower than a small handful of allocations. Let's drop the optimization altogther and instead copy arguments to simplify the code and avoid the future warning with `-Wwrite-strings`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c113c5d commit 23c3251

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

refs/reftable-backend.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,10 +1340,10 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
13401340
* old reference.
13411341
*/
13421342
refs[0] = old_ref;
1343-
refs[0].refname = (char *)arg->newname;
1343+
refs[0].refname = xstrdup(arg->newname);
13441344
refs[0].update_index = creation_ts;
13451345
if (arg->delete_old) {
1346-
refs[1].refname = (char *)arg->oldname;
1346+
refs[1].refname = xstrdup(arg->oldname);
13471347
refs[1].value_type = REFTABLE_REF_DELETION;
13481348
refs[1].update_index = deletion_ts;
13491349
}
@@ -1366,7 +1366,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
13661366
ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
13671367
memset(&logs[logs_nr], 0, sizeof(logs[logs_nr]));
13681368
fill_reftable_log_record(&logs[logs_nr], &committer_ident);
1369-
logs[logs_nr].refname = (char *)arg->newname;
1369+
logs[logs_nr].refname = xstrdup(arg->newname);
13701370
logs[logs_nr].update_index = deletion_ts;
13711371
logs[logs_nr].value.update.message =
13721372
xstrndup(arg->logmsg, arg->refs->write_options.block_size / 2);
@@ -1387,7 +1387,13 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
13871387
if (append_head_reflog) {
13881388
ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
13891389
logs[logs_nr] = logs[logs_nr - 1];
1390-
logs[logs_nr].refname = "HEAD";
1390+
logs[logs_nr].refname = xstrdup("HEAD");
1391+
logs[logs_nr].value.update.name =
1392+
xstrdup(logs[logs_nr].value.update.name);
1393+
logs[logs_nr].value.update.email =
1394+
xstrdup(logs[logs_nr].value.update.email);
1395+
logs[logs_nr].value.update.message =
1396+
xstrdup(logs[logs_nr].value.update.message);
13911397
logs_nr++;
13921398
}
13931399
}
@@ -1398,7 +1404,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
13981404
ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
13991405
memset(&logs[logs_nr], 0, sizeof(logs[logs_nr]));
14001406
fill_reftable_log_record(&logs[logs_nr], &committer_ident);
1401-
logs[logs_nr].refname = (char *)arg->newname;
1407+
logs[logs_nr].refname = xstrdup(arg->newname);
14021408
logs[logs_nr].update_index = creation_ts;
14031409
logs[logs_nr].value.update.message =
14041410
xstrndup(arg->logmsg, arg->refs->write_options.block_size / 2);
@@ -1430,7 +1436,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
14301436
*/
14311437
ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
14321438
logs[logs_nr] = old_log;
1433-
logs[logs_nr].refname = (char *)arg->newname;
1439+
logs[logs_nr].refname = xstrdup(arg->newname);
14341440
logs_nr++;
14351441

14361442
/*
@@ -1439,7 +1445,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
14391445
if (arg->delete_old) {
14401446
ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
14411447
memset(&logs[logs_nr], 0, sizeof(logs[logs_nr]));
1442-
logs[logs_nr].refname = (char *)arg->oldname;
1448+
logs[logs_nr].refname = xstrdup(arg->oldname);
14431449
logs[logs_nr].value_type = REFTABLE_LOG_DELETION;
14441450
logs[logs_nr].update_index = old_log.update_index;
14451451
logs_nr++;
@@ -1462,13 +1468,11 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
14621468
reftable_iterator_destroy(&it);
14631469
string_list_clear(&skip, 0);
14641470
strbuf_release(&errbuf);
1465-
for (i = 0; i < logs_nr; i++) {
1466-
if (!strcmp(logs[i].refname, "HEAD"))
1467-
continue;
1468-
logs[i].refname = NULL;
1471+
for (i = 0; i < logs_nr; i++)
14691472
reftable_log_record_release(&logs[i]);
1470-
}
14711473
free(logs);
1474+
for (i = 0; i < ARRAY_SIZE(refs); i++)
1475+
reftable_ref_record_release(&refs[i]);
14721476
reftable_ref_record_release(&old_ref);
14731477
reftable_log_record_release(&old_log);
14741478
return ret;

0 commit comments

Comments
 (0)