Skip to content

Commit

Permalink
service/s3/s3manager: Prefer using allocated slices from pool over al…
Browse files Browse the repository at this point in the history
…locating new ones. (#3534)
  • Loading branch information
mikhail-chebakov committed Sep 22, 2020
1 parent b498264 commit 3e8ec88
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
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

0 comments on commit 3e8ec88

Please sign in to comment.