From 9407f30248f3626140db1e311a5cb9ae4746c9b6 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 24 Feb 2023 17:01:38 +0300 Subject: [PATCH] [#19] transformer: Optimize payload hashers a bit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` name old time/op new time/op delta Transformer/small-8 74.8µs ±11% 73.7µs ±15% ~ (p=0.529 n=10+10) Transformer/big-8 1.38s ±11% 1.36s ± 4% ~ (p=0.796 n=10+10) name old alloc/op new alloc/op delta Transformer/small-8 7.69kB ± 0% 7.67kB ± 0% -0.21% (p=0.000 n=10+10) Transformer/big-8 49.2kB ± 0% 49.0kB ± 0% -0.48% (p=0.004 n=10+10) name old allocs/op new allocs/op delta Transformer/small-8 102 ± 0% 101 ± 0% -0.98% (p=0.000 n=9+10) Transformer/big-8 620 ± 1% 609 ± 0% -1.66% (p=0.000 n=10+10) ``` Signed-off-by: Evgenii Stratonikov --- object/transformer/transformer.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/object/transformer/transformer.go b/object/transformer/transformer.go index 86b0a392..613bc5e2 100644 --- a/object/transformer/transformer.go +++ b/object/transformer/transformer.go @@ -21,7 +21,7 @@ type payloadSizeLimiter struct { current, parent *object.Object - currentHashers, parentHashers []*payloadChecksumHasher + currentHashers, parentHashers []payloadChecksumHasher previous []oid.ID @@ -83,7 +83,7 @@ func (s *payloadSizeLimiter) initialize() { // initialize parent object once (after 1st object) if ln == 1 { s.parent = fromObject(s.current) - s.parentHashers = s.currentHashers + s.parentHashers = append(s.parentHashers[:0], s.currentHashers...) // return source attributes s.parent.SetAttributes(s.parAttrs...) @@ -117,7 +117,7 @@ func fromObject(obj *object.Object) *object.Object { func (s *payloadSizeLimiter) initializeCurrent() { // create payload hashers s.writtenCurrent = 0 - s.currentHashers = payloadHashersForObject(s.WithoutHomomorphicHash) + s.initPayloadHashers() // compose multi-writer from target and all payload hashers ws := make([]io.Writer, 0, 1+len(s.currentHashers)+len(s.parentHashers)) @@ -135,22 +135,18 @@ func (s *payloadSizeLimiter) initializeCurrent() { s.chunkWriter = io.MultiWriter(ws...) } -func payloadHashersForObject(withoutHomomorphicHash bool) []*payloadChecksumHasher { - hashers := make([]*payloadChecksumHasher, 0, 2) - - hashers = append(hashers, &payloadChecksumHasher{ +func (s *payloadSizeLimiter) initPayloadHashers() { + s.currentHashers = append(s.currentHashers[:0], payloadChecksumHasher{ hasher: sha256.New(), typ: checksum.SHA256, }) - if !withoutHomomorphicHash { - hashers = append(hashers, &payloadChecksumHasher{ + if !s.WithoutHomomorphicHash { + s.currentHashers = append(s.currentHashers, payloadChecksumHasher{ hasher: tz.New(), typ: checksum.TZ, }) } - - return hashers } func (s *payloadSizeLimiter) release(finalize bool) (*AccessIdentifiers, error) {