From 8a474ddd659524afe55df2f1cd5269f1d2003327 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Tue, 26 Aug 2014 08:42:03 +0200 Subject: [PATCH] Uses sync.Pool to recycle some memory. Makes it 1.3. TODO: Find out if it is worth the effort. --- interleaver/main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/interleaver/main.go b/interleaver/main.go index 9c61b26..5af72c4 100644 --- a/interleaver/main.go +++ b/interleaver/main.go @@ -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)