Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shrink allocations when setting new elements #90

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions amt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package amt

import (
"bytes"
"context"
"fmt"
"math"
Expand Down Expand Up @@ -149,11 +148,11 @@ func (r *Root) Set(ctx context.Context, i uint64, val cbg.CBORMarshaler) error {
if val == nil {
d.Raw = cbg.CborNull
} else {
valueBuf := new(bytes.Buffer)
if err := val.MarshalCBOR(valueBuf); err != nil {
data, err := cborToBytes(val)
if err != nil {
return err
}
d.Raw = valueBuf.Bytes()
d.Raw = data
}

// where the index is greater than the number of elements we can fit into the
Expand Down
35 changes: 34 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package amt

import "math"
import (
"bytes"
"math"
"sync"

cbg "github.com/whyrusleeping/cbor-gen"
)

// Given height 'height', how many nodes in a maximally full tree can we
// build? (bitWidth^2)^height = width^height. If we pass in height+1 we can work
Expand All @@ -13,3 +19,30 @@ func nodesForHeight(bitWidth uint, height int) uint64 {
}
return 1 << heightLogTwo
}

var bufferPool sync.Pool = sync.Pool{
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
New: func() any {
return bytes.NewBuffer(nil)
},
}

func cborToBytes(val cbg.CBORMarshaler) ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, checking my understanding: I think what we're doing here is (1) using bytes.Buffer to do efficient allocation of bytes when we don't know up-front what we'll need, (2) using a pool so we're not going to suffer from the consistent over-allocation bytes.Buffer does every time we use one, and (3) copy the bytes into a fixed length slice so we end up with precisely what we need and can reuse the bytes.Buffer without concern.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly.

// Temporary location to put values. We'll copy them to an exact-sized buffer when done.
valueBuf := bufferPool.Get().(*bytes.Buffer)
defer func() {
valueBuf.Reset()
bufferPool.Put(valueBuf)
}()

if err := val.MarshalCBOR(valueBuf); err != nil {

Stebalien marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}

// Copy to shrink the allocation.
buf := valueBuf.Bytes()
cpy := make([]byte, len(buf))
copy(cpy, buf)

return cpy, nil
}