perf(replacer): optimize memory allocation for file placeholders (#7773)

Co-authored-by: jalikajalika5 <105954036+jalikajalika5@users.noreply.github.com>
This commit is contained in:
Muhammad Syafri, S.Kom 2026-05-27 21:20:33 +07:00 committed by GitHub
parent 176b043b01
commit 4d60d936ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -427,14 +427,10 @@ func readFileIntoBuffer(filename string, size int) ([]byte, error) {
}
defer file.Close()
buffer := make([]byte, size)
n, err := file.Read(buffer)
if err != nil && err != io.EOF {
return nil, err
}
// slice the buffer to the actual size
return buffer[:n], nil
// io.LimitReader ensures we never read more than 'size' bytes.
// io.ReadAll starts with a small buffer and grows it as needed,
// preventing a massive 1MB allocation for small files.
return io.ReadAll(io.LimitReader(file, int64(size)))
}
// ReplacementFunc is a function that is called when a