mirror of
https://github.com/caddyserver/caddy.git
synced 2026-08-03 22:33:28 +00:00
perf(replacer): optimize memory allocation for file placeholders (#7773)
Co-authored-by: jalikajalika5 <105954036+jalikajalika5@users.noreply.github.com>
This commit is contained in:
parent
176b043b01
commit
4d60d936ed
1 changed files with 4 additions and 8 deletions
12
replacer.go
12
replacer.go
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue