optimize memory allocation during erasure-read by using temporary buffer pool. (#2259)
* XL/erasure-read: optimize memory allocation during erasure-read by using temporary buffer pool. With the change the buffer needed during GetObject by erasureReadFile is allocated only once.master
parent
04f90bd463
commit
043ddbd834
@ -0,0 +1,69 @@ |
||||
/* |
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
// Package bpool implements a fixed size pool of byte slices.
|
||||
package bpool |
||||
|
||||
import ( |
||||
"errors" |
||||
"sync" |
||||
) |
||||
|
||||
// ErrBpoolNoFree - Normally this error should never be returned, this error
|
||||
// indicates a bug in the package consumer.
|
||||
var ErrBpoolNoFree = errors.New("no free byte slice in pool") |
||||
|
||||
// BytePool - temporary pool of byte slices.
|
||||
type BytePool struct { |
||||
buf [][]byte // array of byte slices
|
||||
used []bool // indicates if a buf[i] is in use
|
||||
size int64 // size of buf[i]
|
||||
mu sync.Mutex |
||||
} |
||||
|
||||
// Get - Returns an unused byte slice.
|
||||
func (b *BytePool) Get() (buf []byte, err error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
for i := 0; i < len(b.used); i++ { |
||||
if !b.used[i] { |
||||
b.used[i] = true |
||||
if b.buf[i] == nil { |
||||
b.buf[i] = make([]byte, b.size) |
||||
} |
||||
return b.buf[i], nil |
||||
} |
||||
} |
||||
return nil, ErrBpoolNoFree |
||||
} |
||||
|
||||
// Reset - Marks all slices as unused.
|
||||
func (b *BytePool) Reset() { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
for i := 0; i < len(b.used); i++ { |
||||
b.used[i] = false |
||||
} |
||||
} |
||||
|
||||
// NewBytePool - Returns new pool.
|
||||
// size - length of each slice.
|
||||
// n - number of slices in the pool.
|
||||
func NewBytePool(size int64, n int) *BytePool { |
||||
used := make([]bool, n) |
||||
buf := make([][]byte, n) |
||||
return &BytePool{buf, used, size, sync.Mutex{}} |
||||
} |
@ -0,0 +1,53 @@ |
||||
/* |
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package bpool |
||||
|
||||
import "testing" |
||||
|
||||
func TestBytePool(t *testing.T) { |
||||
size := int64(10) |
||||
n := 16 |
||||
pool := NewBytePool(size, n) |
||||
enBlocks := make([][]byte, n) |
||||
|
||||
// Allocates all the 16 byte slices in the pool.
|
||||
alloc := func() { |
||||
for i := range enBlocks { |
||||
var err error |
||||
enBlocks[i], err = pool.Get() |
||||
if err != nil { |
||||
t.Fatal("expected nil, got", err) |
||||
} |
||||
// Make sure the slice length is as expected.
|
||||
if len(enBlocks[i]) != int(size) { |
||||
t.Fatalf("expected size %d, got %d", len(enBlocks[i]), size) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Allocate everything in the pool.
|
||||
alloc() |
||||
// Any Get() will fail when the pool does not have any free buffer.
|
||||
_, err := pool.Get() |
||||
if err == nil { |
||||
t.Fatalf("expected %s, got nil", err) |
||||
} |
||||
// Reset - so that all the buffers are marked as unused.
|
||||
pool.Reset() |
||||
// Allocation of all the buffers in the pool should succeed now.
|
||||
alloc() |
||||
} |
Loading…
Reference in new issue