From 386c349c8c6a9dcc578a57be5a8ada67b9c37fb5 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 3 Nov 2019 12:31:44 +0200 Subject: [PATCH] lib/storage: remove interface conversion in hot path during block merging This should improve merge speed a bit for parts with big number of small blocks. --- lib/storage/block_stream_reader.go | 23 ++++++++++++++++++----- lib/storage/block_stream_writer.go | 26 ++++++++++++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/lib/storage/block_stream_reader.go b/lib/storage/block_stream_reader.go index 0f433125e..af46702e8 100644 --- a/lib/storage/block_stream_reader.go +++ b/lib/storage/block_stream_reader.go @@ -25,9 +25,13 @@ type blockStreamReader struct { ph partHeader - timestampsReader filestream.ReadCloser - valuesReader filestream.ReadCloser - indexReader filestream.ReadCloser + // Use io.Reader type for timestampsReader and valuesReader + // in order to remove I2I conversion in readBlock + // when passing them to fs.ReadFullData + timestampsReader io.Reader + valuesReader io.Reader + + indexReader filestream.ReadCloser mrs []metaindexRow @@ -56,6 +60,11 @@ type blockStreamReader struct { err error } +func (bsr *blockStreamReader) assertWriteClosers() { + _ = bsr.timestampsReader.(filestream.ReadCloser) + _ = bsr.valuesReader.(filestream.ReadCloser) +} + func (bsr *blockStreamReader) reset() { bsr.Block.Reset() @@ -108,6 +117,8 @@ func (bsr *blockStreamReader) InitFromInmemoryPart(mp *inmemoryPart) { if err != nil { logger.Panicf("BUG: cannot unmarshal metaindex rows from inmemoryPart: %s", err) } + + bsr.assertWriteClosers() } // InitFromFilePart initializes bsr from a file-based part on the given path. @@ -167,6 +178,8 @@ func (bsr *blockStreamReader) InitFromFilePart(path string) error { bsr.indexReader = indexFile bsr.mrs = mrs + bsr.assertWriteClosers() + return nil } @@ -174,8 +187,8 @@ func (bsr *blockStreamReader) InitFromFilePart(path string) error { // // It closes *Reader files passed to Init. func (bsr *blockStreamReader) MustClose() { - bsr.timestampsReader.MustClose() - bsr.valuesReader.MustClose() + bsr.timestampsReader.(filestream.ReadCloser).MustClose() + bsr.valuesReader.(filestream.ReadCloser).MustClose() bsr.indexReader.MustClose() bsr.reset() diff --git a/lib/storage/block_stream_writer.go b/lib/storage/block_stream_writer.go index 3e89c08a6..ee9292251 100644 --- a/lib/storage/block_stream_writer.go +++ b/lib/storage/block_stream_writer.go @@ -2,6 +2,7 @@ package storage import ( "fmt" + "io" "path/filepath" "sync" "sync/atomic" @@ -17,10 +18,14 @@ type blockStreamWriter struct { compressLevel int path string - timestampsWriter filestream.WriteCloser - valuesWriter filestream.WriteCloser - indexWriter filestream.WriteCloser - metaindexWriter filestream.WriteCloser + // Use io.Writer type for timestampsWriter and valuesWriter + // in order to remove I2I conversion in WriteExternalBlock + // when passing them to fs.MustWriteData + timestampsWriter io.Writer + valuesWriter io.Writer + + indexWriter filestream.WriteCloser + metaindexWriter filestream.WriteCloser mr metaindexRow @@ -35,6 +40,11 @@ type blockStreamWriter struct { compressedMetaindexData []byte } +func (bsw *blockStreamWriter) assertWriteClosers() { + _ = bsw.timestampsWriter.(filestream.WriteCloser) + _ = bsw.valuesWriter.(filestream.WriteCloser) +} + // Init initializes bsw with the given writers. func (bsw *blockStreamWriter) reset() { bsw.compressLevel = 0 @@ -67,6 +77,8 @@ func (bsw *blockStreamWriter) InitFromInmemoryPart(mp *inmemoryPart) { bsw.valuesWriter = &mp.valuesData bsw.indexWriter = &mp.indexData bsw.metaindexWriter = &mp.metaindexData + + bsw.assertWriteClosers() } // InitFromFilePart initializes bsw from a file-based part on the given path. @@ -126,6 +138,8 @@ func (bsw *blockStreamWriter) InitFromFilePart(path string, nocache bool, compre bsw.indexWriter = indexFile bsw.metaindexWriter = metaindexFile + bsw.assertWriteClosers() + return nil } @@ -141,8 +155,8 @@ func (bsw *blockStreamWriter) MustClose() { fs.MustWriteData(bsw.metaindexWriter, bsw.compressedMetaindexData) // Close writers. - bsw.timestampsWriter.MustClose() - bsw.valuesWriter.MustClose() + bsw.timestampsWriter.(filestream.WriteCloser).MustClose() + bsw.valuesWriter.(filestream.WriteCloser).MustClose() bsw.indexWriter.MustClose() bsw.metaindexWriter.MustClose()