diff options
author | vng <viktor.govako@gmail.com> | 2012-02-22 22:46:55 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:34:27 +0300 |
commit | 5c3e56b54a630918e2206a2b682d8b18e6dc4c9a (patch) | |
tree | 62e2e4c4c953338d0e6e5c1020879b16be94d547 /coding | |
parent | 32e1833acc0c1e7f7f547fae5fbfbfd55fa88692 (diff) |
[search] Performance: Avoid penalty on polymorphic SubReader and coping of readers.
Diffstat (limited to 'coding')
-rw-r--r-- | coding/coding.pro | 7 | ||||
-rw-r--r-- | coding/reader.hpp | 2 | ||||
-rw-r--r-- | coding/reader_wrapper.hpp | 35 |
3 files changed, 40 insertions, 4 deletions
diff --git a/coding/coding.pro b/coding/coding.pro index ee1b9c4de6..e79f8a2786 100644 --- a/coding/coding.pro +++ b/coding/coding.pro @@ -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 \ diff --git a/coding/reader.hpp b/coding/reader.hpp index edbb2833a4..9aa5d5fff8 100644 --- a/coding/reader.hpp +++ b/coding/reader.hpp @@ -134,6 +134,8 @@ public: { m_p->ReadAsString(s); } + + ReaderT * GetPtr() const { return m_p.get(); } }; // Model reader store file id as string. diff --git a/coding/reader_wrapper.hpp b/coding/reader_wrapper.hpp new file mode 100644 index 0000000000..e45b940e9e --- /dev/null +++ b/coding/reader_wrapper.hpp @@ -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); + } +}; |