From 36f25a7e7ba3d5527cee8f4ef645827085243e59 Mon Sep 17 00:00:00 2001 From: David Crawshaw Date: Sun, 28 Feb 2016 17:58:57 -0900 Subject: [PATCH] cmd/link: allocate small []byte reads together Reduces number of memory allocations by 12%: Before: 1816664 After: 1581591 Small speed improvement. Change-Id: I61281fb852e8e31851a350e3ae756676705024a4 Reviewed-on: https://go-review.googlesource.com/20027 Reviewed-by: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/cmd/link/internal/ld/objfile.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/cmd/link/internal/ld/objfile.go b/src/cmd/link/internal/ld/objfile.go index 36a65ba40e..b04244f3cc 100644 --- a/src/cmd/link/internal/ld/objfile.go +++ b/src/cmd/link/internal/ld/objfile.go @@ -431,9 +431,23 @@ func rdstring(f *obj.Biobuf) string { return string(p) } +var ( + rddataBuf = make([]byte, rddataBufMax) + rddataBufMax = 1 << 14 +) + func rddata(f *obj.Biobuf) []byte { - n := rdint64(f) - p := make([]byte, n) + var p []byte + n := rdint(f) + if n > rddataBufMax { + p = make([]byte, n) + } else { + if len(rddataBuf) < n { + rddataBuf = make([]byte, rddataBufMax) + } + p = rddataBuf[:n:n] + rddataBuf = rddataBuf[n:] + } obj.Bread(f, p) return p }