packed-backend: mmap large "packed-refs" file during fsck
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 the helper function "allocate_snapshot_buffer", we can simply use this function to use mmap mechanism. Suggested-by: Jeff King <peff@peff.net> Suggested-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
shejialuo committed
May 14, 2025 at 23:50 UTC
86ddd588f24acf3960489dccb8aed82dc570796b
1 file changed
+7
-12
refs/packed-backend.c
+7
-12
@@ -2068,7 +2068,7 @@ static int packed_fsck(struct ref_store *ref_store,
2068
{
2069
struct packed_ref_store *refs = packed_downcast(ref_store,
2070
REF_STORE_READ, "fsck");
2071
- struct strbuf packed_ref_content = STRBUF_INIT;
2071
+ struct snapshot snapshot = { 0 };
2072
unsigned int sorted = 0;
2073
struct stat st;
2074
int ret = 0;
@@ -2112,7 +2112,7 @@ static int packed_fsck(struct ref_store *ref_store,
2112
goto cleanup;
2113
}
2114
2115
- if (!st.st_size) {
2115
+ if (!allocate_snapshot_buffer(&snapshot, fd, &st)) {
2116
struct fsck_ref_report report = { 0 };
2117
report.path = "packed-refs";
2118
ret = fsck_report_ref(o, &report,
@@ -2121,21 +2121,16 @@ static int packed_fsck(struct ref_store *ref_store,
2121
goto cleanup;
2122
}
2123
2124
- if (strbuf_read(&packed_ref_content, fd, 0) < 0) {
2125
- ret = error_errno(_("unable to read '%s'"), refs->path);
2126
- goto cleanup;
2127
- }
2128
-
2129
- ret = packed_fsck_ref_content(o, ref_store, &sorted, packed_ref_content.buf,
2130
- packed_ref_content.buf + packed_ref_content.len);
2124
+ ret = packed_fsck_ref_content(o, ref_store, &sorted, snapshot.start,
2125
+ snapshot.eof);
2126
if (!ret && sorted)
2132
- ret = packed_fsck_ref_sorted(o, ref_store, packed_ref_content.buf,
2133
- packed_ref_content.buf + packed_ref_content.len);
2127
+ ret = packed_fsck_ref_sorted(o, ref_store, snapshot.start,
2128
+ snapshot.eof);
2129
2130
cleanup:
2131
if (fd >= 0)
2132
close(fd);
2138
- strbuf_release(&packed_ref_content);
2133
+ clear_snapshot_buffer(&snapshot);
2134
return ret;
2135
}
2136