read_packed_refs(): read references with minimal copying

Instead of copying data from the `packed-refs` file one line at time and then processing it, process the data in place as much as possible. Also, instead of processing one line per iteration of the main loop, process a reference line plus its corresponding peeled line (if present) together. Note that this change slightly tightens up the parsing of the `packed-refs` file. Previously, the parser would have accepted multiple "peeled" lines for a single reference (ignoring all but the last one). Now it would reject that. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 25, 2017 at 10:00 UTC 6a9bc4034ab59a2d241151f9025c07e62178ad95
1 file changed +40 -61
refs/packed-backend.c
+40 -61
@@ -134,33 +134,6 @@ static void clear_packed_ref_cache(struct packed_ref_store *refs)
134 }
135 }
136
137 -/* The length of a peeled reference line in packed-refs, including EOL: */
138 -#define PEELED_LINE_LENGTH 42
139 -
140 -/*
141 - * Parse one line from a packed-refs file. Write the SHA1 to sha1.
142 - * Return a pointer to the refname within the line (null-terminated),
143 - * or NULL if there was a problem.
144 - */
145 -static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
146 -{
147 - const char *ref;
148 -
149 - if (parse_oid_hex(line->buf, oid, &ref) < 0)
150 - return NULL;
151 - if (!isspace(*ref++))
152 - return NULL;
153 -
154 - if (isspace(*ref))
155 - return NULL;
156 -
157 - if (line->buf[line->len - 1] != '\n')
158 - return NULL;
159 - line->buf[--line->len] = 0;
160 -
161 - return ref;
162 -}
163 -
137 static NORETURN void die_unterminated_line(const char *path,
138 const char *p, size_t len)
139 {
@@ -221,8 +194,7 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
194 size_t size;
195 char *buf;
196 const char *pos, *eol, *eof;
224 - struct ref_entry *last = NULL;
225 - struct strbuf line = STRBUF_INIT;
197 + struct strbuf tmp = STRBUF_INIT;
198 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
199 struct ref_dir *dir;
200
@@ -264,9 +236,9 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
236 if (!eol)
237 die_unterminated_line(refs->path, pos, eof - pos);
238
267 - strbuf_add(&line, pos, eol - pos);
239 + strbuf_add(&tmp, pos, eol - pos);
240
269 - if (!skip_prefix(line.buf, "# pack-refs with:", (const char **)&p))
241 + if (!skip_prefix(tmp.buf, "# pack-refs with:", (const char **)&p))
242 die_invalid_line(refs->path, pos, eof - pos);
243
244 string_list_split_in_place(&traits, p, ' ', -1);
@@ -281,61 +253,68 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
253 pos = eol + 1;
254
255 string_list_clear(&traits, 0);
284 - strbuf_reset(&line);
256 + strbuf_reset(&tmp);
257 }
258
259 dir = get_ref_dir(packed_refs->cache->root);
260 while (pos < eof) {
261 + const char *p = pos;
262 struct object_id oid;
263 const char *refname;
264 + int flag = REF_ISPACKED;
265 + struct ref_entry *entry = NULL;
266
292 - eol = memchr(pos, '\n', eof - pos);
267 + if (eof - pos < GIT_SHA1_HEXSZ + 2 ||
268 + parse_oid_hex(p, &oid, &p) ||
269 + !isspace(*p++))
270 + die_invalid_line(refs->path, pos, eof - pos);
271 +
272 + eol = memchr(p, '\n', eof - p);
273 if (!eol)
274 die_unterminated_line(refs->path, pos, eof - pos);
275
296 - strbuf_add(&line, pos, eol + 1 - pos);
276 + strbuf_add(&tmp, p, eol - p);
277 + refname = tmp.buf;
278 +
279 + if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
280 + if (!refname_is_safe(refname))
281 + die("packed refname is dangerous: %s", refname);
282 + oidclr(&oid);
283 + flag |= REF_BAD_NAME | REF_ISBROKEN;
284 + }
285 + if (peeled == PEELED_FULLY ||
286 + (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
287 + flag |= REF_KNOWS_PEELED;
288 + entry = create_ref_entry(refname, &oid, flag);
289 + add_ref_entry(dir, entry);
290
298 - refname = parse_ref_line(&line, &oid);
299 - if (refname) {
300 - int flag = REF_ISPACKED;
291 + pos = eol + 1;
292 +
293 + if (pos < eof && *pos == '^') {
294 + p = pos + 1;
295 + if (eof - p < GIT_SHA1_HEXSZ + 1 ||
296 + parse_oid_hex(p, &entry->u.value.peeled, &p) ||
297 + *p++ != '\n')
298 + die_invalid_line(refs->path, pos, eof - pos);
299
302 - if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
303 - if (!refname_is_safe(refname))
304 - die("packed refname is dangerous: %s", refname);
305 - oidclr(&oid);
306 - flag |= REF_BAD_NAME | REF_ISBROKEN;
307 - }
308 - last = create_ref_entry(refname, &oid, flag);
309 - if (peeled == PEELED_FULLY ||
310 - (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
311 - last->flag |= REF_KNOWS_PEELED;
312 - add_ref_entry(dir, last);
313 - } else if (last &&
314 - line.buf[0] == '^' &&
315 - line.len == PEELED_LINE_LENGTH &&
316 - line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
317 - !get_oid_hex(line.buf + 1, &oid)) {
318 - oidcpy(&last->u.value.peeled, &oid);
300 /*
301 * Regardless of what the file header said,
302 * we definitely know the value of *this*
303 * reference:
304 */
324 - last->flag |= REF_KNOWS_PEELED;
325 - } else {
326 - die_invalid_line(refs->path, line.buf, line.len);
305 + entry->flag |= REF_KNOWS_PEELED;
306 +
307 + pos = p;
308 }
309
329 - /* The "+ 1" is for the LF character. */
330 - pos = eol + 1;
331 - strbuf_reset(&line);
310 + strbuf_reset(&tmp);
311 }
312
313 if (munmap(buf, size))
314 die_errno("error ummapping packed-refs file");
315 close(fd);
316
338 - strbuf_release(&line);
317 + strbuf_release(&tmp);
318 return packed_refs;
319 }
320