During fsck, we use "strbuf_read" to read the content of "packed-refs" without using mmap mechanism. This is a bad practice which would consume more memory than using mmap mechanism. Besides, as all code paths in "packed-backend.c" use this way, we should make "fsck" align with the current codebase. As we have introduced two helper functions "allocate_snapshot_buffer" and "munmap_snapshot_if_temporary", we could simply call these functions to use mmap mechanism. Suggested-by: Jeff King <peff@xxxxxxxx> Suggested-by: Patrick Steinhardt <ps@xxxxxx> Signed-off-by: shejialuo <shejialuo@xxxxxxxxx> --- refs/packed-backend.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/refs/packed-backend.c b/refs/packed-backend.c index ae6b6845a6..ff744f1d4c 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -2079,7 +2079,7 @@ static int packed_fsck(struct ref_store *ref_store, { struct packed_ref_store *refs = packed_downcast(ref_store, REF_STORE_READ, "fsck"); - struct strbuf packed_ref_content = STRBUF_INIT; + struct snapshot *snapshot = xcalloc(1, sizeof(*snapshot)); unsigned int sorted = 0; struct stat st; int ret = 0; @@ -2126,21 +2126,23 @@ static int packed_fsck(struct ref_store *ref_store, if (!st.st_size) goto cleanup; - if (strbuf_read(&packed_ref_content, fd, 0) < 0) { - ret = error_errno(_("unable to read '%s'"), refs->path); + if (!allocate_snapshot_buffer(snapshot, fd, &st)) goto cleanup; - } - ret = packed_fsck_ref_content(o, ref_store, &sorted, packed_ref_content.buf, - packed_ref_content.buf + packed_ref_content.len); + if (mmap_strategy == MMAP_TEMPORARY && snapshot->mmapped) + munmap_temporary_snapshot(snapshot); + + ret = packed_fsck_ref_content(o, ref_store, &sorted, snapshot->start, + snapshot->eof); if (!ret && sorted) - ret = packed_fsck_ref_sorted(o, ref_store, packed_ref_content.buf, - packed_ref_content.buf + packed_ref_content.len); + ret = packed_fsck_ref_sorted(o, ref_store, snapshot->start, + snapshot->eof); cleanup: if (fd >= 0) close(fd); - strbuf_release(&packed_ref_content); + clear_snapshot_buffer(snapshot); + free(snapshot); return ret; } -- 2.49.0