From f77ff78d4f6606981637cf847f0e2736764d3354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Duchesneau?= Date: Mon, 2 Dec 2024 14:09:43 -0500 Subject: [PATCH] perf: skip unused Errorf() call in storage some CRC32 checks can cause lots of failing calls to decodeUint32, generating a new 'fmt.Errorf()', but the error is never actually read, it is only checked for nil condition. Replacing with a (reused) more generic error prevents lots of allocations leading to less stress on the GC. --- storage/storage.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/storage/storage.go b/storage/storage.go index b6316fa668f9..e359f77b1d3a 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -1592,6 +1592,8 @@ func newObjectFromProto(o *storagepb.Object) *ObjectAttrs { } } +var ErrStorageDoesNotEncode32BitValue = errors.New("storage does not encode 32-bit value") + // Decode a uint32 encoded in Base64 in big-endian byte order. func decodeUint32(b64 string) (uint32, error) { d, err := base64.StdEncoding.DecodeString(b64) @@ -1599,7 +1601,7 @@ func decodeUint32(b64 string) (uint32, error) { return 0, err } if len(d) != 4 { - return 0, fmt.Errorf("storage: %q does not encode a 32-bit value", d) + return 0, ErrStorageDoesNotEncode32BitValue } return uint32(d[0])<<24 + uint32(d[1])<<16 + uint32(d[2])<<8 + uint32(d[3]), nil }