Skip to content

Commit 86ddd58

Browse files
shejialuogitster
authored andcommitted
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 <[email protected]> Suggested-by: Patrick Steinhardt <[email protected]> Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a0dee3f commit 86ddd58

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

refs/packed-backend.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,7 @@ static int packed_fsck(struct ref_store *ref_store,
20682068
{
20692069
struct packed_ref_store *refs = packed_downcast(ref_store,
20702070
REF_STORE_READ, "fsck");
2071-
struct strbuf packed_ref_content = STRBUF_INIT;
2071+
struct snapshot snapshot = { 0 };
20722072
unsigned int sorted = 0;
20732073
struct stat st;
20742074
int ret = 0;
@@ -2112,7 +2112,7 @@ static int packed_fsck(struct ref_store *ref_store,
21122112
goto cleanup;
21132113
}
21142114

2115-
if (!st.st_size) {
2115+
if (!allocate_snapshot_buffer(&snapshot, fd, &st)) {
21162116
struct fsck_ref_report report = { 0 };
21172117
report.path = "packed-refs";
21182118
ret = fsck_report_ref(o, &report,
@@ -2121,21 +2121,16 @@ static int packed_fsck(struct ref_store *ref_store,
21212121
goto cleanup;
21222122
}
21232123

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);
21312126
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);
21342129

21352130
cleanup:
21362131
if (fd >= 0)
21372132
close(fd);
2138-
strbuf_release(&packed_ref_content);
2133+
clear_snapshot_buffer(&snapshot);
21392134
return ret;
21402135
}
21412136

0 commit comments

Comments
 (0)