Uses sync.Pool to recycle some memory. Makes it 1.3. TODO: Find out if it is worth the effort.

This commit is contained in:
Sascha L. Teichmann 2014-08-26 08:42:03 +02:00
parent d51ecc6eef
commit 8a474ddd65

View File

@ -9,6 +9,7 @@ import (
"fmt"
"log"
"os"
"sync"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
)
@ -54,10 +55,12 @@ func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) erro
done := make(chan struct{})
defer close(done)
pool := sync.Pool{New: func() interface{} { return new(Block) }}
go func() {
defer close(blocks)
for {
block := new(Block)
block := pool.Get().(*Block)
if err := producer.Next(block); err != nil {
if err != ErrNoMoreBlocks {
log.Printf("Reading failed: %s\n", err)
@ -77,6 +80,8 @@ func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) erro
if err := consumer.Consume(block); err != nil {
return err
}
block.Data = nil
pool.Put(block)
i++
if i%1000 == 0 {
log.Printf("%d blocks transferred.\n", i)