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 <christian.couder@gmail.com> Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Nov 25, 2024 at 08:34 UTC
ef46ad0815509c464e8a2558b4ebf6dc42736a01
4 files changed
+20
-18
reftable/block.c
+5
-5
@@ -115,16 +115,16 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
115
int n = 0;
116
int err;
117
118
- err = reftable_record_key(rec, &w->buf);
118
+ err = reftable_record_key(rec, &w->scratch);
119
if (err < 0)
120
goto done;
121
122
- if (!w->buf.len) {
122
+ if (!w->scratch.len) {
123
err = REFTABLE_API_ERROR;
124
goto done;
125
}
126
127
- n = reftable_encode_key(&is_restart, out, last, w->buf,
127
+ n = reftable_encode_key(&is_restart, out, last, w->scratch,
128
reftable_record_val_type(rec));
129
if (n < 0) {
130
err = -1;
@@ -140,7 +140,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
140
string_view_consume(&out, n);
141
142
err = block_writer_register_restart(w, start.len - out.len, is_restart,
143
- &w->buf);
143
+ &w->scratch);
144
done:
145
return err;
146
}
@@ -565,7 +565,7 @@ void block_writer_release(struct block_writer *bw)
565
REFTABLE_FREE_AND_NULL(bw->zstream);
566
REFTABLE_FREE_AND_NULL(bw->restarts);
567
REFTABLE_FREE_AND_NULL(bw->compressed);
568
- reftable_buf_release(&bw->buf);
568
+ reftable_buf_release(&bw->scratch);
569
reftable_buf_release(&bw->last_key);
570
/* the block is not owned. */
571
}
reftable/block.h
+2
-1
@@ -39,7 +39,8 @@ struct block_writer {
39
uint32_t restart_cap;
40
41
struct reftable_buf last_key;
42
- struct reftable_buf buf;
42
+ /* Scratch buffer used to avoid allocations. */
43
+ struct reftable_buf scratch;
44
int entries;
45
};
46
reftable/writer.c
+11
-11
@@ -148,7 +148,7 @@ int reftable_writer_new(struct reftable_writer **out,
148
149
reftable_buf_init(&wp->block_writer_data.last_key);
150
reftable_buf_init(&wp->last_key);
151
- reftable_buf_init(&wp->buf);
151
+ reftable_buf_init(&wp->scratch);
152
REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size);
153
if (!wp->block) {
154
reftable_free(wp);
@@ -181,7 +181,7 @@ static void writer_release(struct reftable_writer *w)
181
w->block_writer = NULL;
182
writer_clear_index(w);
183
reftable_buf_release(&w->last_key);
184
- reftable_buf_release(&w->buf);
184
+ reftable_buf_release(&w->scratch);
185
}
186
}
187
@@ -253,17 +253,17 @@ static int writer_add_record(struct reftable_writer *w,
253
{
254
int err;
255
256
- err = reftable_record_key(rec, &w->buf);
256
+ err = reftable_record_key(rec, &w->scratch);
257
if (err < 0)
258
goto done;
259
260
- if (reftable_buf_cmp(&w->last_key, &w->buf) >= 0) {
260
+ if (reftable_buf_cmp(&w->last_key, &w->scratch) >= 0) {
261
err = REFTABLE_API_ERROR;
262
goto done;
263
}
264
265
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);
267
if (err < 0)
268
goto done;
269
@@ -339,25 +339,25 @@ int reftable_writer_add_ref(struct reftable_writer *w,
339
goto out;
340
341
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),
344
hash_size(w->opts.hash_id));
345
if (err < 0)
346
goto out;
347
348
- err = writer_index_hash(w, &w->buf);
348
+ err = writer_index_hash(w, &w->scratch);
349
if (err < 0)
350
goto out;
351
}
352
353
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),
356
hash_size(w->opts.hash_id));
357
if (err < 0)
358
goto out;
359
360
- err = writer_index_hash(w, &w->buf);
360
+ err = writer_index_hash(w, &w->scratch);
361
if (err < 0)
362
goto out;
363
}
reftable/writer.h
+2
-1
@@ -20,7 +20,8 @@ struct reftable_writer {
20
void *write_arg;
21
int pending_padding;
22
struct reftable_buf last_key;
23
- struct reftable_buf buf;
23
+ /* Scratch buffer used to avoid allocations. */
24
+ struct reftable_buf scratch;
25
26
/* offset of next block to write. */
27
uint64_t next;