Skip to content

Commit

Permalink
slicer: Reuse stub object
Browse files Browse the repository at this point in the history
Updated after benchmark. During object slicing we create object for each part, we may reuse it, just resetting some fields.

                                                 │    old.txt    │                new.txt                │
                                                 │    sec/op     │    sec/op     vs base                 │
SliceDataIntoObjects/slice_1-128/reader-16         106.71µ ± ∞ ¹   64.06µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1-128/writer-16         110.32µ ± ∞ ¹   65.76µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4-128/reader-16         109.83µ ± ∞ ¹   61.09µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4-128/writer-16         113.74µ ± ∞ ¹   63.21µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16-128/reader-16        108.11µ ± ∞ ¹   63.71µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16-128/writer-16        112.33µ ± ∞ ¹   66.81µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_64-128/reader-16        113.14µ ± ∞ ¹   62.64µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_64-128/writer-16        121.37µ ± ∞ ¹   65.29µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_256-128/reader-16       107.53µ ± ∞ ¹   65.86µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_256-128/writer-16        232.0µ ± ∞ ¹   245.8µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1024-128/reader-16      111.45µ ± ∞ ¹   67.47µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1024-128/writer-16       600.3µ ± ∞ ¹   623.9µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4096-128/reader-16      112.14µ ± ∞ ¹   63.59µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4096-128/writer-16       2.014m ± ∞ ¹   1.978m ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16384-128/reader-16     109.55µ ± ∞ ¹   61.47µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16384-128/writer-16      7.637m ± ∞ ¹   6.874m ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_65536-128/reader-16     117.70µ ± ∞ ¹   67.41µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_65536-128/writer-16      30.22m ± ∞ ¹   27.09m ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_262144-128/reader-16    123.20µ ± ∞ ¹   72.06µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_262144-128/writer-16     111.4m ± ∞ ¹   106.2m ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1048576-128/reader-16    194.2µ ± ∞ ¹   105.3µ ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1048576-128/writer-16    495.5m ± ∞ ¹   430.6m ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                                             459.0µ         310.1µ        -32.44%
¹ 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.797Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1-128/writer-16         14.283Ki ± ∞ ¹   9.672Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4-128/reader-16         14.690Ki ± ∞ ¹   8.921Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4-128/writer-16         15.910Ki ± ∞ ¹   8.796Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16-128/reader-16        14.190Ki ± ∞ ¹   9.797Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16-128/writer-16        15.347Ki ± ∞ ¹   9.672Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_64-128/reader-16        16.316Ki ± ∞ ¹   9.390Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_64-128/writer-16        17.536Ki ± ∞ ¹   9.265Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_256-128/reader-16       13.441Ki ± ∞ ¹   9.735Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_256-128/writer-16        30.20Ki ± ∞ ¹   31.49Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1024-128/reader-16       15.70Ki ± ∞ ¹   11.02Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1024-128/writer-16       77.03Ki ± ∞ ¹   75.16Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4096-128/reader-16      15.710Ki ± ∞ ¹   9.745Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4096-128/writer-16       247.3Ki ± ∞ ¹   220.1Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16384-128/reader-16     14.766Ki ± ∞ ¹   8.961Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16384-128/writer-16      926.9Ki ± ∞ ¹   792.7Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_65536-128/reader-16      16.69Ki ± ∞ ¹   10.44Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_65536-128/writer-16      3.569Mi ± ∞ ¹   3.054Mi ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_262144-128/reader-16     17.28Ki ± ∞ ¹   11.09Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_262144-128/writer-16     13.88Mi ± ∞ ¹   12.26Mi ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1048576-128/reader-16    23.90Ki ± ∞ ¹   15.01Ki ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1048576-128/writer-16    57.21Mi ± ∞ ¹   48.96Mi ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                                             60.34Ki         42.88Ki        -28.94%
¹ 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 ± ∞ ¹    154.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1-128/writer-16          220.0 ± ∞ ¹    153.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4-128/reader-16          224.0 ± ∞ ¹    141.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4-128/writer-16          246.0 ± ∞ ¹    140.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16-128/reader-16         217.0 ± ∞ ¹    154.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16-128/writer-16         238.0 ± ∞ ¹    153.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_64-128/reader-16         248.0 ± ∞ ¹    148.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_64-128/writer-16         270.0 ± ∞ ¹    147.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_256-128/reader-16        205.0 ± ∞ ¹    153.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_256-128/writer-16        460.0 ± ∞ ¹    472.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1024-128/reader-16       236.0 ± ∞ ¹    171.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1024-128/writer-16      1.194k ± ∞ ¹   1.128k ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4096-128/reader-16       236.0 ± ∞ ¹    153.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_4096-128/writer-16      3.860k ± ∞ ¹   3.266k ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16384-128/reader-16      225.0 ± ∞ ¹    141.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_16384-128/writer-16     14.49k ± ∞ ¹   11.77k ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_65536-128/reader-16      253.0 ± ∞ ¹    162.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_65536-128/writer-16     57.18k ± ∞ ¹   46.44k ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_262144-128/reader-16     260.0 ± ∞ ¹    172.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_262144-128/writer-16    221.6k ± ∞ ¹   184.7k ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1048576-128/reader-16    358.0 ± ∞ ¹    229.0 ± ∞ ¹        ~ (p=1.000 n=1) ²
SliceDataIntoObjects/slice_1048576-128/writer-16   909.7k ± ∞ ¹   737.8k ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                                             924.5          661.1        -28.49%
¹ 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 <[email protected]>
  • Loading branch information
smallhive committed Jul 28, 2023
1 parent 53af101 commit c41562a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
17 changes: 17 additions & 0 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ func (o *Object) SetID(v oid.ID) {
SetObjectID(&v2)
}

// ResetID removes object identifier.
//
// See also [Object.SetID].
func (o *Object) ResetID() {
(*object.Object)(o).
SetObjectID(nil)
}

// Signature returns signature of the object identifier.
//
// See also [Object.SetSignature].
Expand Down Expand Up @@ -536,6 +544,15 @@ func (o *Object) SetParentID(v oid.ID) {
})
}

// ResetParentID removes identifier of the parent object.
//
// See also [Object.SetParentID].
func (o *Object) ResetParentID() {
o.setSplitFields(func(split *object.SplitHeader) {
split.SetParent(nil)
})
}

// Parent returns parent object w/o payload.
//
// See also [Object.SetParent].
Expand Down
32 changes: 20 additions & 12 deletions object/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ func initPayloadStream(ctx context.Context, ow ObjectWriter, header object.Objec
currentVersion := version.Current()
header.SetVersion(&currentVersion)

var stubObject object.Object
stubObject.SetVersion(&currentVersion)
stubObject.SetContainerID(containerID)
stubObject.SetCreationEpoch(opts.currentNeoFSEpoch)
stubObject.SetType(object.TypeRegular)
stubObject.SetOwnerID(&owner)
stubObject.SetSessionToken(opts.sessionToken)

res := &PayloadWriter{
ctx: ctx,
isHeaderWriteStep: true,
Expand All @@ -245,6 +253,7 @@ func initPayloadStream(ctx context.Context, ow ObjectWriter, header object.Objec
rootMeta: newDynamicObjectMetadata(opts.withHomoChecksum),
childMeta: newDynamicObjectMetadata(opts.withHomoChecksum),
prmObjectPutInit: prm,
stubObject: &stubObject,
}

maxObjSize := childPayloadSizeLimit(opts)
Expand Down Expand Up @@ -283,6 +292,7 @@ type PayloadWriter struct {

writtenChildren []oid.ID
prmObjectPutInit client.PrmObjectPutInit
stubObject *object.Object
}

// Write writes next chunk of the object data. Concatenation of all chunks forms
Expand Down Expand Up @@ -364,19 +374,14 @@ func (x *PayloadWriter) writeLastChild(ctx context.Context, meta dynamicObjectMe
}

func (x *PayloadWriter) _writeChild(ctx context.Context, meta dynamicObjectMetadata, last bool, rootIDHandler func(id oid.ID)) error {
currentVersion := version.Current()

fCommon := func(obj *object.Object) {
obj.SetVersion(&currentVersion)
obj.SetContainerID(x.container)
obj.SetCreationEpoch(x.currentEpoch)
obj.SetType(object.TypeRegular)
obj.SetOwnerID(&x.owner)
obj.SetSessionToken(x.sessionToken)
}
obj := *x.stubObject
obj.SetSplitID(nil)
obj.ResetPreviousID()
obj.SetParent(nil)
obj.ResetParentID()
obj.SetSignature(nil)
obj.ResetID()

var obj object.Object
fCommon(&obj)
if x.withSplit {
obj.SetSplitID(x.splitID)
}
Expand Down Expand Up @@ -420,6 +425,9 @@ func (x *PayloadWriter) _writeChild(ctx context.Context, meta dynamicObjectMetad
meta.reset()
obj.ResetPreviousID()
obj.SetChildren(x.writtenChildren...)
// we reuse already written object, we should reset these fields, to eval them one more time in writeInMemObject.
obj.ResetID()
obj.SetSignature(nil)

_, err = writeInMemObject(ctx, x.signer, x.stream, obj, nil, meta, x.prmObjectPutInit)
if err != nil {
Expand Down

0 comments on commit c41562a

Please sign in to comment.