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

service/s3/s3manager: Prefer using allocated slices from pool over allocating new ones. #3534

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### SDK Features

### SDK Enhancements
* `service/s3/s3manager`: Prefer using allocated slices from pool over allocating new ones. ([#3534](https://github.com/aws/aws-sdk-go/pull/3534))

### SDK Bugs
8 changes: 8 additions & 0 deletions service/s3/s3manager/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func (p *maxSlicePool) Get(ctx aws.Context) (*[]byte, error) {
return nil, errZeroCapacity
}
return bs, nil
case <-ctx.Done():
p.mtx.RUnlock()
return nil, ctx.Err()
default:
// pass
}

select {
case _, ok := <-p.allocations:
p.mtx.RUnlock()
if !ok {
Expand Down
27 changes: 27 additions & 0 deletions service/s3/s3manager/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,33 @@ func TestMaxSlicePool(t *testing.T) {
pool.Close()
}

func TestPoolShouldPreferAllocatedSlicesOverNewAllocations(t *testing.T) {
pool := newMaxSlicePool(0)
defer pool.Close()

// Prepare pool: make it so that pool contains 1 allocated slice and 1 allocation permit
pool.ModifyCapacity(2)
initialSlice, err := pool.Get(context.Background())
if err != nil {
t.Errorf("failed to get slice from pool: %v", err)
}
pool.Put(initialSlice)

for i := 0; i < 100; i++ {
newSlice, err := pool.Get(context.Background())
if err != nil {
t.Errorf("failed to get slice from pool: %v", err)
return
}

if newSlice != initialSlice {
t.Errorf("pool allocated a new slice despite it having pre-allocated one")
return
}
pool.Put(newSlice)
}
}

type recordedPartPool struct {
recordedAllocs uint64
recordedGets uint64
Expand Down