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