From 86cb865fdd2aacd08b8e506061e98d5bf0ae5835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 3 Sep 2020 21:21:10 +0200 Subject: [PATCH] Make chain export ~1000x times faster --- node/impl/full/chain.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/node/impl/full/chain.go b/node/impl/full/chain.go index 61eb4d3f572..ce5e3822aa7 100644 --- a/node/impl/full/chain.go +++ b/node/impl/full/chain.go @@ -1,6 +1,7 @@ package full import ( + "bufio" "bytes" "context" "encoding/json" @@ -503,7 +504,11 @@ func (a *ChainAPI) ChainExport(ctx context.Context, nroots abi.ChainEpoch, tsk t out := make(chan []byte) go func() { defer w.Close() //nolint:errcheck // it is a pipe - if err := a.Chain.Export(ctx, ts, nroots, w); err != nil { + + bw := bufio.NewWriterSize(w, 1<<20) + defer bw.Flush() //nolint:errcheck // it is a write to a pipe + + if err := a.Chain.Export(ctx, ts, nroots, bw); err != nil { log.Errorf("chain export call failed: %s", err) return } @@ -512,7 +517,7 @@ func (a *ChainAPI) ChainExport(ctx context.Context, nroots abi.ChainEpoch, tsk t go func() { defer close(out) for { - buf := make([]byte, 4096) + buf := make([]byte, 1<<20) n, err := r.Read(buf) if err != nil && err != io.EOF { log.Errorf("chain export pipe read failed: %s", err) @@ -522,6 +527,7 @@ func (a *ChainAPI) ChainExport(ctx context.Context, nroots abi.ChainEpoch, tsk t case out <- buf[:n]: case <-ctx.Done(): log.Warnf("export writer failed: %s", ctx.Err()) + return } if err == io.EOF { return