From 76f44b251a9b0abaf616ff2bdb7f0092728cbf9c 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 62.61µ ± 1% 62.90µ ± 0% ~ (p=0.132 n=6) SliceDataIntoObjects/slice_1-128/writer-16 64.94µ ± 1% 65.30µ ± 1% ~ (p=0.394 n=6) SliceDataIntoObjects/slice_4-128/reader-16 65.48µ ± 1% 61.19µ ± 1% -6.56% (p=0.002 n=6) SliceDataIntoObjects/slice_4-128/writer-16 67.38µ ± 2% 63.62µ ± 1% -5.59% (p=0.002 n=6) SliceDataIntoObjects/slice_16-128/reader-16 61.57µ ± 2% 63.15µ ± 1% +2.57% (p=0.002 n=6) SliceDataIntoObjects/slice_16-128/writer-16 63.93µ ± 1% 64.99µ ± 1% +1.66% (p=0.004 n=6) SliceDataIntoObjects/slice_64-128/reader-16 64.89µ ± 1% 66.61µ ± 1% +2.66% (p=0.002 n=6) SliceDataIntoObjects/slice_64-128/writer-16 67.24µ ± 1% 71.96µ ± 2% +7.02% (p=0.002 n=6) SliceDataIntoObjects/slice_256-128/reader-16 61.19µ ± 1% 65.92µ ± 1% +7.73% (p=0.002 n=6) SliceDataIntoObjects/slice_256-128/writer-16 236.2µ ± 2% 252.7µ ± 1% +6.97% (p=0.002 n=6) SliceDataIntoObjects/slice_1024-128/reader-16 64.34µ ± 1% 64.30µ ± 2% ~ (p=0.937 n=6) SliceDataIntoObjects/slice_1024-128/writer-16 569.6µ ± 2% 611.3µ ± 2% +7.32% (p=0.002 n=6) SliceDataIntoObjects/slice_4096-128/reader-16 63.45µ ± 1% 67.79µ ± 1% +6.85% (p=0.002 n=6) SliceDataIntoObjects/slice_4096-128/writer-16 1.865m ± 1% 2.073m ± 1% +11.16% (p=0.002 n=6) SliceDataIntoObjects/slice_16384-128/reader-16 67.15µ ± 0% 64.50µ ± 1% -3.95% (p=0.002 n=6) SliceDataIntoObjects/slice_16384-128/writer-16 7.889m ± 1% 7.067m ± 1% -10.42% (p=0.002 n=6) SliceDataIntoObjects/slice_65536-128/reader-16 67.12µ ± 1% 65.98µ ± 1% -1.70% (p=0.002 n=6) SliceDataIntoObjects/slice_65536-128/writer-16 31.07m ± 0% 31.06m ± 1% ~ (p=1.000 n=6) SliceDataIntoObjects/slice_262144-128/reader-16 72.49µ ± 1% 73.88µ ± 1% +1.91% (p=0.002 n=6) SliceDataIntoObjects/slice_262144-128/writer-16 112.7m ± 0% 112.6m ± 0% ~ (p=0.699 n=6) SliceDataIntoObjects/slice_1048576-128/reader-16 125.1µ ± 3% 125.1µ ± 3% ~ (p=0.818 n=6) SliceDataIntoObjects/slice_1048576-128/writer-16 518.8m ± 2% 510.0m ± 1% -1.71% (p=0.026 n=6) geomean 318.0µ 321.5µ +1.09% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ SliceDataIntoObjects/slice_1-128/reader-16 9.453Ki ± 0% 9.547Ki ± 0% +0.99% (p=0.002 n=6) SliceDataIntoObjects/slice_1-128/writer-16 9.344Ki ± 0% 9.438Ki ± 0% +1.00% (p=0.002 n=6) SliceDataIntoObjects/slice_4-128/reader-16 10.329Ki ± 0% 9.077Ki ± 0% -12.12% (p=0.002 n=6) SliceDataIntoObjects/slice_4-128/writer-16 10.220Ki ± 0% 8.968Ki ± 0% -12.25% (p=0.002 n=6) SliceDataIntoObjects/slice_16-128/reader-16 8.984Ki ± 0% 9.547Ki ± 0% +6.26% (p=0.002 n=6) SliceDataIntoObjects/slice_16-128/writer-16 8.968Ki ± 0% 9.438Ki ± 0% +5.24% (p=0.002 n=6) SliceDataIntoObjects/slice_64-128/reader-16 10.33Ki ± 0% 11.17Ki ± 0% +8.17% (p=0.002 n=6) SliceDataIntoObjects/slice_64-128/writer-16 10.22Ki ± 0% 11.06Ki ± 0% +8.26% (p=0.002 n=6) SliceDataIntoObjects/slice_256-128/reader-16 8.985Ki ± 0% 10.893Ki ± 0% +21.23% (p=0.002 n=6) SliceDataIntoObjects/slice_256-128/writer-16 31.18Ki ± 0% 38.08Ki ± 0% +22.11% (p=0.002 n=6) SliceDataIntoObjects/slice_1024-128/reader-16 9.864Ki ± 0% 9.425Ki ± 0% -4.46% (p=0.002 n=6) SliceDataIntoObjects/slice_1024-128/writer-16 81.16Ki ± 0% 75.66Ki ± 0% -6.78% (p=0.002 n=6) SliceDataIntoObjects/slice_4096-128/reader-16 9.874Ki ± 0% 11.188Ki ± 0% +13.30% (p=0.002 n=6) SliceDataIntoObjects/slice_4096-128/writer-16 266.2Ki ± 0% 260.8Ki ± 0% -2.01% (p=0.002 n=6) SliceDataIntoObjects/slice_16384-128/reader-16 11.14Ki ± 0% 10.00Ki ± 0% -10.18% (p=0.002 n=6) SliceDataIntoObjects/slice_16384-128/writer-16 1038.0Ki ± 0% 936.3Ki ± 0% -9.81% (p=0.002 n=6) SliceDataIntoObjects/slice_65536-128/reader-16 10.97Ki ± 0% 10.10Ki ± 0% -7.89% (p=0.002 n=6) SliceDataIntoObjects/slice_65536-128/writer-16 3.981Mi ± 0% 3.708Mi ± 0% -6.86% (p=0.002 n=6) SliceDataIntoObjects/slice_262144-128/reader-16 11.41Ki ± 0% 11.92Ki ± 0% +4.41% (p=0.002 n=6) SliceDataIntoObjects/slice_262144-128/writer-16 15.53Mi ± 0% 14.45Mi ± 0% -6.94% (p=0.002 n=6) SliceDataIntoObjects/slice_1048576-128/reader-16 17.77Ki ± 3% 18.17Ki ± 3% +2.24% (p=0.002 n=6) SliceDataIntoObjects/slice_1048576-128/writer-16 63.79Mi ± 0% 59.48Mi ± 0% -6.76% (p=0.002 n=6) geomean 46.61Ki 46.55Ki -0.13% │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ SliceDataIntoObjects/slice_1-128/reader-16 149.0 ± 0% 149.0 ± 0% ~ (p=1.000 n=6) ¹ SliceDataIntoObjects/slice_1-128/writer-16 148.0 ± 0% 148.0 ± 0% ~ (p=1.000 n=6) ¹ SliceDataIntoObjects/slice_4-128/reader-16 161.0 ± 0% 142.0 ± 0% -11.80% (p=0.002 n=6) SliceDataIntoObjects/slice_4-128/writer-16 160.0 ± 0% 141.0 ± 0% -11.88% (p=0.002 n=6) SliceDataIntoObjects/slice_16-128/reader-16 142.0 ± 0% 149.0 ± 0% +4.93% (p=0.002 n=6) SliceDataIntoObjects/slice_16-128/writer-16 143.0 ± 0% 148.0 ± 0% +3.50% (p=0.002 n=6) SliceDataIntoObjects/slice_64-128/reader-16 161.0 ± 0% 172.0 ± 0% +6.83% (p=0.002 n=6) SliceDataIntoObjects/slice_64-128/writer-16 160.0 ± 0% 171.0 ± 0% +6.88% (p=0.002 n=6) SliceDataIntoObjects/slice_256-128/reader-16 142.0 ± 0% 167.0 ± 0% +17.61% (p=0.002 n=6) SliceDataIntoObjects/slice_256-128/writer-16 478.0 ± 0% 583.0 ± 0% +21.97% (p=0.002 n=6) SliceDataIntoObjects/slice_1024-128/reader-16 155.0 ± 0% 147.0 ± 0% -5.16% (p=0.002 n=6) SliceDataIntoObjects/slice_1024-128/writer-16 1.283k ± 0% 1.155k ± 0% -9.98% (p=0.002 n=6) SliceDataIntoObjects/slice_4096-128/reader-16 155.0 ± 0% 172.0 ± 0% +10.97% (p=0.002 n=6) SliceDataIntoObjects/slice_4096-128/writer-16 4.237k ± 0% 4.021k ± 0% -5.11% (p=0.002 n=6) SliceDataIntoObjects/slice_16384-128/reader-16 172.0 ± 0% 155.0 ± 0% -9.88% (p=0.002 n=6) SliceDataIntoObjects/slice_16384-128/writer-16 16.51k ± 0% 14.51k ± 0% -12.07% (p=0.002 n=6) SliceDataIntoObjects/slice_65536-128/reader-16 169.0 ± 0% 157.0 ± 0% -7.10% (p=0.002 n=6) SliceDataIntoObjects/slice_65536-128/writer-16 64.87k ± 0% 58.66k ± 0% -9.57% (p=0.002 n=6) SliceDataIntoObjects/slice_262144-128/reader-16 178.0 ± 1% 182.0 ± 0% +2.25% (p=0.002 n=6) SliceDataIntoObjects/slice_262144-128/writer-16 252.3k ± 0% 227.8k ± 0% -9.73% (p=0.002 n=6) SliceDataIntoObjects/slice_1048576-128/reader-16 279.5 ± 3% 279.0 ± 3% ~ (p=0.790 n=6) SliceDataIntoObjects/slice_1048576-128/writer-16 1032.7k ± 0% 934.5k ± 0% -9.51% (p=0.002 n=6) geomean 732.6 720.3 -1.67% 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)