From a285f4dce86606ff6b908e7763593f12f24a7934 Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Thu, 27 Jul 2023 15:56:54 +0400 Subject: [PATCH] slicer: Reuse PrmObjectPutInit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updated after benchmark. During object slicing we recreate PrmObjectPutInit for each part, we may reuse it. │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ SliceDataIntoObjects/slice_1-128/reader-16 106.71µ ± ∞ ¹ 62.43µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1-128/writer-16 110.32µ ± ∞ ¹ 66.60µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4-128/reader-16 109.83µ ± ∞ ¹ 60.68µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4-128/writer-16 113.74µ ± ∞ ¹ 63.75µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16-128/reader-16 108.11µ ± ∞ ¹ 62.82µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16-128/writer-16 112.33µ ± ∞ ¹ 66.82µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_64-128/reader-16 113.14µ ± ∞ ¹ 61.70µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_64-128/writer-16 121.37µ ± ∞ ¹ 65.74µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_256-128/reader-16 107.53µ ± ∞ ¹ 62.76µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_256-128/writer-16 232.0µ ± ∞ ¹ 255.5µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1024-128/reader-16 111.45µ ± ∞ ¹ 67.08µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1024-128/writer-16 600.3µ ± ∞ ¹ 625.6µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4096-128/reader-16 112.14µ ± ∞ ¹ 63.49µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4096-128/writer-16 2.014m ± ∞ ¹ 2.048m ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16384-128/reader-16 109.55µ ± ∞ ¹ 60.83µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16384-128/writer-16 7.637m ± ∞ ¹ 6.919m ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_65536-128/reader-16 117.70µ ± ∞ ¹ 66.33µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_65536-128/writer-16 30.22m ± ∞ ¹ 27.99m ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_262144-128/reader-16 123.20µ ± ∞ ¹ 71.48µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_262144-128/writer-16 111.4m ± ∞ ¹ 112.7m ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1048576-128/reader-16 194.2µ ± ∞ ¹ 110.0µ ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1048576-128/writer-16 495.5m ± ∞ ¹ 452.4m ± ∞ ¹ ~ (p=1.000 n=1) ² geomean 459.0µ 312.1µ -32.00% ¹ need >= 6 samples for confidence interval at level 0.95 ² need >= 4 samples to detect a difference at alpha level 0.05 │ old.txt │ new.txt │ │ B/op │ B/op vs base │ SliceDataIntoObjects/slice_1-128/reader-16 13.064Ki ± ∞ ¹ 9.672Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1-128/writer-16 14.283Ki ± ∞ ¹ 9.562Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4-128/reader-16 14.690Ki ± ∞ ¹ 8.796Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4-128/writer-16 15.910Ki ± ∞ ¹ 8.687Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16-128/reader-16 14.190Ki ± ∞ ¹ 9.672Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16-128/writer-16 15.347Ki ± ∞ ¹ 9.562Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_64-128/reader-16 16.316Ki ± ∞ ¹ 9.265Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_64-128/writer-16 17.536Ki ± ∞ ¹ 9.155Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_256-128/reader-16 13.441Ki ± ∞ ¹ 9.610Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_256-128/writer-16 30.20Ki ± ∞ ¹ 32.26Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1024-128/reader-16 15.70Ki ± ∞ ¹ 10.89Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1024-128/writer-16 77.03Ki ± ∞ ¹ 80.81Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4096-128/reader-16 15.710Ki ± ∞ ¹ 9.622Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4096-128/writer-16 247.3Ki ± ∞ ¹ 245.3Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16384-128/reader-16 14.766Ki ± ∞ ¹ 8.841Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16384-128/writer-16 926.9Ki ± ∞ ¹ 895.9Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_65536-128/reader-16 16.69Ki ± ∞ ¹ 10.33Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_65536-128/writer-16 3.569Mi ± ∞ ¹ 3.460Mi ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_262144-128/reader-16 17.28Ki ± ∞ ¹ 11.10Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_262144-128/writer-16 13.88Mi ± ∞ ¹ 13.89Mi ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1048576-128/reader-16 23.90Ki ± ∞ ¹ 15.95Ki ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1048576-128/writer-16 57.21Mi ± ∞ ¹ 55.46Mi ± ∞ ¹ ~ (p=1.000 n=1) ² geomean 60.34Ki 44.07Ki -26.97% ¹ need >= 6 samples for confidence interval at level 0.95 ² need >= 4 samples to detect a difference at alpha level 0.05 │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ SliceDataIntoObjects/slice_1-128/reader-16 198.0 ± ∞ ¹ 152.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1-128/writer-16 220.0 ± ∞ ¹ 151.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4-128/reader-16 224.0 ± ∞ ¹ 139.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4-128/writer-16 246.0 ± ∞ ¹ 138.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16-128/reader-16 217.0 ± ∞ ¹ 152.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16-128/writer-16 238.0 ± ∞ ¹ 151.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_64-128/reader-16 248.0 ± ∞ ¹ 146.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_64-128/writer-16 270.0 ± ∞ ¹ 145.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_256-128/reader-16 205.0 ± ∞ ¹ 151.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_256-128/writer-16 460.0 ± ∞ ¹ 489.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1024-128/reader-16 236.0 ± ∞ ¹ 169.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1024-128/writer-16 1.194k ± ∞ ¹ 1.253k ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4096-128/reader-16 236.0 ± ∞ ¹ 151.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_4096-128/writer-16 3.860k ± ∞ ¹ 3.823k ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16384-128/reader-16 225.0 ± ∞ ¹ 139.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_16384-128/writer-16 14.49k ± ∞ ¹ 14.06k ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_65536-128/reader-16 253.0 ± ∞ ¹ 161.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_65536-128/writer-16 57.18k ± ∞ ¹ 55.64k ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_262144-128/reader-16 260.0 ± ∞ ¹ 173.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_262144-128/writer-16 221.6k ± ∞ ¹ 221.6k ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1048576-128/reader-16 358.0 ± ∞ ¹ 248.0 ± ∞ ¹ ~ (p=1.000 n=1) ² SliceDataIntoObjects/slice_1048576-128/writer-16 909.7k ± ∞ ¹ 885.2k ± ∞ ¹ ~ (p=1.000 n=1) ² geomean 924.5 689.9 -25.37% ¹ need >= 6 samples for confidence interval at level 0.95 ² need >= 4 samples to detect a difference at alpha level 0.05 Signed-off-by: Evgenii Baidakov --- object/slicer/slicer.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/object/slicer/slicer.go b/object/slicer/slicer.go index 3f725dc1..4e176811 100644 --- a/object/slicer/slicer.go +++ b/object/slicer/slicer.go @@ -217,7 +217,10 @@ func initPayloadStream(ctx context.Context, ow ObjectWriter, header object.Objec return nil, err } + var prm client.PrmObjectPutInit + if opts.sessionToken != nil { + prm.WithinSession(*opts.sessionToken) header.SetSessionToken(opts.sessionToken) // session issuer is a container owner. issuer := opts.sessionToken.Issuer() @@ -241,6 +244,7 @@ func initPayloadStream(ctx context.Context, ow ObjectWriter, header object.Objec sessionToken: opts.sessionToken, rootMeta: newDynamicObjectMetadata(opts.withHomoChecksum), childMeta: newDynamicObjectMetadata(opts.withHomoChecksum), + prmObjectPutInit: prm, } maxObjSize := childPayloadSizeLimit(opts) @@ -277,7 +281,8 @@ type PayloadWriter struct { withSplit bool splitID *object.SplitID - writtenChildren []oid.ID + writtenChildren []oid.ID + prmObjectPutInit client.PrmObjectPutInit } // Write writes next chunk of the object data. Concatenation of all chunks forms @@ -400,9 +405,9 @@ func (x *PayloadWriter) _writeChild(ctx context.Context, meta dynamicObjectMetad // The first object must be a header. Note: if object is less than MaxObjectSize, we don't need to slice it. // Thus, we have a legitimate situation when, last == true and x.isHeaderWriteStep == true. if x.isHeaderWriteStep { - id, err = writeInMemObject(ctx, x.signer, x.stream, x.headerObject, x.buf.Bytes(), meta, x.sessionToken) + id, err = writeInMemObject(ctx, x.signer, x.stream, x.headerObject, x.buf.Bytes(), meta, x.prmObjectPutInit) } else { - id, err = writeInMemObject(ctx, x.signer, x.stream, obj, x.buf.Bytes(), meta, x.sessionToken) + id, err = writeInMemObject(ctx, x.signer, x.stream, obj, x.buf.Bytes(), meta, x.prmObjectPutInit) } if err != nil { @@ -416,7 +421,7 @@ func (x *PayloadWriter) _writeChild(ctx context.Context, meta dynamicObjectMetad obj.ResetPreviousID() obj.SetChildren(x.writtenChildren...) - _, err = writeInMemObject(ctx, x.signer, x.stream, obj, nil, meta, x.sessionToken) + _, err = writeInMemObject(ctx, x.signer, x.stream, obj, nil, meta, x.prmObjectPutInit) if err != nil { return fmt.Errorf("write linking object: %w", err) } @@ -468,7 +473,7 @@ func flushObjectMetadata(signer neofscrypto.Signer, meta dynamicObjectMetadata, return id, nil } -func writeInMemObject(ctx context.Context, signer user.Signer, w ObjectWriter, header object.Object, payload []byte, meta dynamicObjectMetadata, session *session.Object) (oid.ID, error) { +func writeInMemObject(ctx context.Context, signer user.Signer, w ObjectWriter, header object.Object, payload []byte, meta dynamicObjectMetadata, prm client.PrmObjectPutInit) (oid.ID, error) { var ( id oid.ID err error @@ -484,11 +489,6 @@ func writeInMemObject(ctx context.Context, signer user.Signer, w ObjectWriter, h } } - var prm client.PrmObjectPutInit - if session != nil { - prm.WithinSession(*session) - } - stream, err := w.ObjectPutInit(ctx, header, signer, prm) if err != nil { return id, fmt.Errorf("init data stream for next object: %w", err)