From cb25b01e70c9ecfe75ac38e66ad977a08621a145 Mon Sep 17 00:00:00 2001 From: sneak Date: Sun, 9 Aug 2026 01:48:44 +0000 Subject: [PATCH] Preallocate append targets flagged by prealloc (refs #61) collectBatchFlushData now sizes the file-chunk and chunk-file slices to the number of pending files, a safe lower bound since every file contributes at least one mapping of each kind. The chunker test sizes its reconstruction buffer to the input length, which is exactly what it ends up holding. Append semantics and results are unchanged. --- internal/chunker/chunker_test.go | 2 +- internal/snapshot/scanner.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/chunker/chunker_test.go b/internal/chunker/chunker_test.go index f44919f..520bcd6 100644 --- a/internal/chunker/chunker_test.go +++ b/internal/chunker/chunker_test.go @@ -53,7 +53,7 @@ func TestChunkerLargeFileMultipleChunks(t *testing.T) { } // Verify chunks reconstruct original data - var reconstructed []byte + reconstructed := make([]byte, 0, len(data)) for _, chunk := range chunks { reconstructed = append(reconstructed, chunk.Data...) } diff --git a/internal/snapshot/scanner.go b/internal/snapshot/scanner.go index 8e4b5cd..fc00251 100644 --- a/internal/snapshot/scanner.go +++ b/internal/snapshot/scanner.go @@ -624,11 +624,11 @@ func (s *Scanner) collectBatchFlushData( collectStart := time.Now() - var ( - allFileChunks []database.FileChunk - allChunkFiles []database.ChunkFile - ) - + // Every pending file contributes at least one file-chunk and one + // chunk-file mapping, so the file count is a safe lower bound for the + // initial capacity of both slices. + allFileChunks := make([]database.FileChunk, 0, len(canFlush)) + allChunkFiles := make([]database.ChunkFile, 0, len(canFlush)) allFileIDs := make([]types.FileID, 0, len(canFlush)) allFiles := make([]*database.File, 0, len(canFlush))