Skip to content

Commit ef46ad0

Browse files
pks-tgitster
authored andcommitted
reftable: rename scratch buffer
Both `struct block_writer` and `struct reftable_writer` have a `buf` member that is being reused to optimize the number of allocations. Rename the variable to `scratch` to clarify its intend and provide a comment explaining why it exists. Suggested-by: Christian Couder <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0f5762b commit ef46ad0

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

reftable/block.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
115115
int n = 0;
116116
int err;
117117

118-
err = reftable_record_key(rec, &w->buf);
118+
err = reftable_record_key(rec, &w->scratch);
119119
if (err < 0)
120120
goto done;
121121

122-
if (!w->buf.len) {
122+
if (!w->scratch.len) {
123123
err = REFTABLE_API_ERROR;
124124
goto done;
125125
}
126126

127-
n = reftable_encode_key(&is_restart, out, last, w->buf,
127+
n = reftable_encode_key(&is_restart, out, last, w->scratch,
128128
reftable_record_val_type(rec));
129129
if (n < 0) {
130130
err = -1;
@@ -140,7 +140,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
140140
string_view_consume(&out, n);
141141

142142
err = block_writer_register_restart(w, start.len - out.len, is_restart,
143-
&w->buf);
143+
&w->scratch);
144144
done:
145145
return err;
146146
}
@@ -565,7 +565,7 @@ void block_writer_release(struct block_writer *bw)
565565
REFTABLE_FREE_AND_NULL(bw->zstream);
566566
REFTABLE_FREE_AND_NULL(bw->restarts);
567567
REFTABLE_FREE_AND_NULL(bw->compressed);
568-
reftable_buf_release(&bw->buf);
568+
reftable_buf_release(&bw->scratch);
569569
reftable_buf_release(&bw->last_key);
570570
/* the block is not owned. */
571571
}

reftable/block.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ struct block_writer {
3939
uint32_t restart_cap;
4040

4141
struct reftable_buf last_key;
42-
struct reftable_buf buf;
42+
/* Scratch buffer used to avoid allocations. */
43+
struct reftable_buf scratch;
4344
int entries;
4445
};
4546

reftable/writer.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ int reftable_writer_new(struct reftable_writer **out,
148148

149149
reftable_buf_init(&wp->block_writer_data.last_key);
150150
reftable_buf_init(&wp->last_key);
151-
reftable_buf_init(&wp->buf);
151+
reftable_buf_init(&wp->scratch);
152152
REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size);
153153
if (!wp->block) {
154154
reftable_free(wp);
@@ -181,7 +181,7 @@ static void writer_release(struct reftable_writer *w)
181181
w->block_writer = NULL;
182182
writer_clear_index(w);
183183
reftable_buf_release(&w->last_key);
184-
reftable_buf_release(&w->buf);
184+
reftable_buf_release(&w->scratch);
185185
}
186186
}
187187

@@ -253,17 +253,17 @@ static int writer_add_record(struct reftable_writer *w,
253253
{
254254
int err;
255255

256-
err = reftable_record_key(rec, &w->buf);
256+
err = reftable_record_key(rec, &w->scratch);
257257
if (err < 0)
258258
goto done;
259259

260-
if (reftable_buf_cmp(&w->last_key, &w->buf) >= 0) {
260+
if (reftable_buf_cmp(&w->last_key, &w->scratch) >= 0) {
261261
err = REFTABLE_API_ERROR;
262262
goto done;
263263
}
264264

265265
reftable_buf_reset(&w->last_key);
266-
err = reftable_buf_add(&w->last_key, w->buf.buf, w->buf.len);
266+
err = reftable_buf_add(&w->last_key, w->scratch.buf, w->scratch.len);
267267
if (err < 0)
268268
goto done;
269269

@@ -339,25 +339,25 @@ int reftable_writer_add_ref(struct reftable_writer *w,
339339
goto out;
340340

341341
if (!w->opts.skip_index_objects && reftable_ref_record_val1(ref)) {
342-
reftable_buf_reset(&w->buf);
343-
err = reftable_buf_add(&w->buf, (char *)reftable_ref_record_val1(ref),
342+
reftable_buf_reset(&w->scratch);
343+
err = reftable_buf_add(&w->scratch, (char *)reftable_ref_record_val1(ref),
344344
hash_size(w->opts.hash_id));
345345
if (err < 0)
346346
goto out;
347347

348-
err = writer_index_hash(w, &w->buf);
348+
err = writer_index_hash(w, &w->scratch);
349349
if (err < 0)
350350
goto out;
351351
}
352352

353353
if (!w->opts.skip_index_objects && reftable_ref_record_val2(ref)) {
354-
reftable_buf_reset(&w->buf);
355-
err = reftable_buf_add(&w->buf, reftable_ref_record_val2(ref),
354+
reftable_buf_reset(&w->scratch);
355+
err = reftable_buf_add(&w->scratch, reftable_ref_record_val2(ref),
356356
hash_size(w->opts.hash_id));
357357
if (err < 0)
358358
goto out;
359359

360-
err = writer_index_hash(w, &w->buf);
360+
err = writer_index_hash(w, &w->scratch);
361361
if (err < 0)
362362
goto out;
363363
}

reftable/writer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ struct reftable_writer {
2020
void *write_arg;
2121
int pending_padding;
2222
struct reftable_buf last_key;
23-
struct reftable_buf buf;
23+
/* Scratch buffer used to avoid allocations. */
24+
struct reftable_buf scratch;
2425

2526
/* offset of next block to write. */
2627
uint64_t next;

0 commit comments

Comments
 (0)