[search] Performance: Avoid penalty on polymorphic SubReader and coping of readers.

This commit is contained in:
vng 2012-02-22 21:46:55 +03:00 committed by Alex Zolotarev
parent 32e1833acc
commit 5c3e56b54a
5 changed files with 46 additions and 10 deletions

View file

@ -27,7 +27,7 @@ SOURCES += \
zip_reader.cpp \
mmap_reader.cpp \
reader_streambuf.cpp \
reader_writer_ops.cpp
reader_writer_ops.cpp \
HEADERS += \
internal/xmlparser.h \
@ -82,6 +82,5 @@ HEADERS += \
file_reader_stream.hpp \
file_writer_stream.hpp \
reader_streambuf.hpp \
reader_writer_ops.hpp
reader_writer_ops.hpp \
reader_wrapper.hpp \

View file

@ -134,6 +134,8 @@ public:
{
m_p->ReadAsString(s);
}
ReaderT * GetPtr() const { return m_p.get(); }
};
// Model reader store file id as string.

35
coding/reader_wrapper.hpp Normal file
View file

@ -0,0 +1,35 @@
#pragma once
#include "../base/assert.hpp"
/// Reader wrapper to avoid penalty on copy and polymorphic SubReader creation.
template <class ReaderT> class SubReaderWrapper
{
ReaderT * m_p;
uint64_t m_pos;
uint64_t m_size;
protected:
SubReaderWrapper(ReaderT * p, uint64_t pos, uint64_t size)
: m_p(p), m_pos(pos), m_size(size)
{
ASSERT_LESS_OR_EQUAL ( pos + size, m_p->Size(), (pos, size) );
}
public:
explicit SubReaderWrapper(ReaderT * p) : m_p(p), m_pos(0), m_size(p->Size()) {}
uint64_t Size() const { return m_size; }
void Read(uint64_t pos, void * p, size_t size) const
{
ASSERT_LESS_OR_EQUAL ( pos + size, m_size, (pos, size) );
m_p->Read(pos + m_pos, p, size);
}
SubReaderWrapper SubReader(uint64_t pos, uint64_t size) const
{
return SubReaderWrapper(m_p, pos + m_pos, size);
}
};

View file

@ -4,9 +4,6 @@
#include "../base/matrix.hpp"
class FileReader;
template <class> class ReaderSource;
class FileWriter;
// Calculates screen parameters in navigation (dragging, scaling, etc.).
class Navigator

View file

@ -13,6 +13,7 @@
#include "../indexer/categories_holder.hpp"
#include "../coding/multilang_utf8_string.hpp"
#include "../coding/reader_wrapper.hpp"
#include "../base/logging.hpp"
#include "../base/string_utils.hpp"
@ -643,10 +644,12 @@ void Query::SearchFeatures(vector<vector<strings::UniString> > const & tokens,
continue;
serial::CodingParams cp(GetCPForTrie(header.GetDefCodingParams()));
ModelReaderPtr searchReader = pMwm->m_cont.GetReader(SEARCH_INDEX_FILE_TAG);
scoped_ptr<TrieIterator> pTrieRoot(::trie::reader::ReadTrie(
pMwm->m_cont.GetReader(SEARCH_INDEX_FILE_TAG),
trie::ValueReader(cp),
trie::EdgeValueReader()));
SubReaderWrapper<Reader>(searchReader.GetPtr()),
trie::ValueReader(cp),
trie::EdgeValueReader()));
// Get categories edge root.
scoped_ptr<TrieIterator> pCategoriesRoot;