From b0e871876a585bdab21af192f333d5be92eeb04b Mon Sep 17 00:00:00 2001 From: Menno Finlay-Smits Date: Tue, 14 Nov 2017 11:50:00 +1300 Subject: [PATCH] pkg/escape: Preallocate output in Unescape Preallocating the capacity of the output is faster and uses less memory than letting append() do its own (over)allocation. --- pkg/escape/bytes.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/escape/bytes.go b/pkg/escape/bytes.go index ac7ed5ab38..f3b31f42d3 100644 --- a/pkg/escape/bytes.go +++ b/pkg/escape/bytes.go @@ -78,7 +78,11 @@ func Unescape(in []byte) []byte { i := 0 inLen := len(in) - var out []byte + + // The output size will be no more than inLen. Preallocating the + // capacity of the output is faster and uses less memory than + // letting append() do its own (over)allocation. + out := make([]byte, 0, inLen) for { if i >= inLen {