Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 09:45 UTC
perf(replacer): optimize memory allocation for file placeholders (#7773)
Co-authored-by: jalikajalika5 <105954036+jalikajalika5@users.noreply.github.com>
Muhammad Syafri, S.Kom committed
May 27, 2026 at 21:20 UTC
4d60d936edce2ab49cb36c47d0d1bdc0029a0ed2
1 file changed
+4
-8
replacer.go
+4
-8
@@ -427,14 +427,10 @@ func readFileIntoBuffer(filename string, size int) ([]byte, error) {
427
}
428
defer file.Close()
429
430
- buffer := make([]byte, size)
431
- n, err := file.Read(buffer)
432
- if err != nil && err != io.EOF {
433
- return nil, err
434
- }
435
-
436
- // slice the buffer to the actual size
437
- return buffer[:n], nil
430
+ // io.LimitReader ensures we never read more than 'size' bytes.
431
+ // io.ReadAll starts with a small buffer and grows it as needed,
432
+ // preventing a massive 1MB allocation for small files.
433
+ return io.ReadAll(io.LimitReader(file, int64(size)))
434
}
435
436
// ReplacementFunc is a function that is called when a