Skip to content

Commit 3e8ec88

Browse files
service/s3/s3manager: Prefer using allocated slices from pool over allocating new ones. (#3534)
1 parent b498264 commit 3e8ec88

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

CHANGELOG_PENDING.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### SDK Features
22

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

56
### SDK Bugs

service/s3/s3manager/pool.go

+8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func (p *maxSlicePool) Get(ctx aws.Context) (*[]byte, error) {
6060
return nil, errZeroCapacity
6161
}
6262
return bs, nil
63+
case <-ctx.Done():
64+
p.mtx.RUnlock()
65+
return nil, ctx.Err()
66+
default:
67+
// pass
68+
}
69+
70+
select {
6371
case _, ok := <-p.allocations:
6472
p.mtx.RUnlock()
6573
if !ok {

service/s3/s3manager/pool_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ func TestMaxSlicePool(t *testing.T) {
9090
pool.Close()
9191
}
9292

93+
func TestPoolShouldPreferAllocatedSlicesOverNewAllocations(t *testing.T) {
94+
pool := newMaxSlicePool(0)
95+
defer pool.Close()
96+
97+
// Prepare pool: make it so that pool contains 1 allocated slice and 1 allocation permit
98+
pool.ModifyCapacity(2)
99+
initialSlice, err := pool.Get(context.Background())
100+
if err != nil {
101+
t.Errorf("failed to get slice from pool: %v", err)
102+
}
103+
pool.Put(initialSlice)
104+
105+
for i := 0; i < 100; i++ {
106+
newSlice, err := pool.Get(context.Background())
107+
if err != nil {
108+
t.Errorf("failed to get slice from pool: %v", err)
109+
return
110+
}
111+
112+
if newSlice != initialSlice {
113+
t.Errorf("pool allocated a new slice despite it having pre-allocated one")
114+
return
115+
}
116+
pool.Put(newSlice)
117+
}
118+
}
119+
93120
type recordedPartPool struct {
94121
recordedAllocs uint64
95122
recordedGets uint64

0 commit comments

Comments
 (0)