Repository: ignite Updated Branches: refs/heads/master fd3c50e0d -> 40432b093
IGNITE-9161: Optimization for C++ (copy elision) This closes #4476 Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/40432b09 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/40432b09 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/40432b09 Branch: refs/heads/master Commit: 40432b093f400ae505869cca8a1f5ab745bf5206 Parents: fd3c50e Author: Igor Sapego <[email protected]> Authored: Fri Sep 7 15:16:02 2018 +0300 Committer: Igor Sapego <[email protected]> Committed: Fri Sep 7 15:16:02 2018 +0300 ---------------------------------------------------------------------- .../include/ignite/binary/binary_raw_reader.h | 22 ++- .../ignite/impl/binary/binary_reader_impl.h | 11 ++ .../ignite/impl/binary/binary_type_impl.h | 16 +- .../cpp/core/include/ignite/cache/cache.h | 46 ++++-- .../include/ignite/cache/query/query_cursor.h | 15 +- .../cpp/core/include/ignite/impl/operations.h | 150 ++++++------------- .../cpp/odbc-test/src/connection_test.cpp | 7 +- 7 files changed, 128 insertions(+), 139 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/40432b09/modules/platforms/cpp/binary/include/ignite/binary/binary_raw_reader.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/binary/binary_raw_reader.h b/modules/platforms/cpp/binary/include/ignite/binary/binary_raw_reader.h index c06cb91..73d2525 100644 --- a/modules/platforms/cpp/binary/include/ignite/binary/binary_raw_reader.h +++ b/modules/platforms/cpp/binary/include/ignite/binary/binary_raw_reader.h @@ -313,18 +313,30 @@ namespace ignite */ std::string ReadString() { + std::string res; + + ReadString(res); + + return res; + } + + /** + * Read string from the stream. + * + * @param dst String. + */ + void ReadString(std::string& dst) + { int32_t len = ReadString(NULL, 0); if (len != -1) { - ignite::common::FixedSizeArray<char> arr(len + 1); - - ReadString(arr.GetData(), static_cast<int32_t>(arr.GetSize())); + dst.resize(static_cast<size_t>(len)); - return std::string(arr.GetData()); + ReadString(&dst[0], len); } else - return std::string(); + dst.clear(); } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/40432b09/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_reader_impl.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_reader_impl.h b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_reader_impl.h index 94f5ec5..a3b880c 100644 --- a/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_reader_impl.h +++ b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_reader_impl.h @@ -902,6 +902,17 @@ namespace ignite /** * Read object. * + * @param res Read object. + */ + template<typename T> + void ReadTopObject(T& res) + { + return ignite::binary::ReadHelper<T>::Read(*this, res); + } + + /** + * Read object. + * * @return Read object. */ template<typename R, typename T> http://git-wip-us.apache.org/repos/asf/ignite/blob/40432b09/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_type_impl.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_type_impl.h b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_type_impl.h index 64638c2..e9bbace 100644 --- a/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_type_impl.h +++ b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_type_impl.h @@ -97,10 +97,16 @@ namespace ignite { T res; - reader.template ReadTopObject0<ignite::binary::BinaryReader, T>(res); + Read<R>(reader, res); return res; } + + template<typename R> + static void Read(R& reader, T& val) + { + reader.template ReadTopObject0<BinaryReader, T>(val); + } }; /** @@ -117,10 +123,16 @@ namespace ignite std::auto_ptr<T> res(new T()); - reader.template ReadTopObject0<ignite::binary::BinaryReader, T>(*res); + reader.template ReadTopObject0<BinaryReader, T>(*res); return res.release(); } + + template<typename R> + static void Read(R& reader, T*& ptr) + { + ptr = Read<R>(reader); + } }; } } http://git-wip-us.apache.org/repos/asf/ignite/blob/40432b09/modules/platforms/cpp/core/include/ignite/cache/cache.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core/include/ignite/cache/cache.h b/modules/platforms/cpp/core/include/ignite/cache/cache.h index b16d7f5..c230361 100644 --- a/modules/platforms/cpp/core/include/ignite/cache/cache.h +++ b/modules/platforms/cpp/core/include/ignite/cache/cache.h @@ -259,12 +259,14 @@ namespace ignite */ V LocalPeek(const K& key, int32_t peekModes, IgniteError& err) { + V val; + impl::InCacheLocalPeekOperation<K> inOp(key, peekModes); - impl::Out1Operation<V> outOp; + impl::Out1Operation<V> outOp(val); impl.Get()->LocalPeek(inOp, outOp, peekModes, err); - return outOp.GetResult(); + return val; } /** @@ -305,12 +307,13 @@ namespace ignite */ V Get(const K& key, IgniteError& err) { + V val; impl::In1Operation<K> inOp(key); - impl::Out1Operation<V> outOp; + impl::Out1Operation<V> outOp(val); impl.Get()->Get(inOp, outOp, err); - return outOp.GetResult(); + return val; } /** @@ -351,12 +354,14 @@ namespace ignite */ std::map<K, V> GetAll(const std::set<K>& keys, IgniteError& err) { + std::map<K, V> res; + impl::InSetOperation<K> inOp(keys); - impl::OutMapOperation<K, V> outOp; + impl::OutMapOperation<K, V> outOp(res); impl.Get()->GetAll(inOp, outOp, err); - return outOp.GetResult(); + return res; } /** @@ -515,12 +520,14 @@ namespace ignite */ V GetAndPut(const K& key, const V& val, IgniteError& err) { + V oldVal; + impl::In2Operation<K, V> inOp(key, val); - impl::Out1Operation<V> outOp; + impl::Out1Operation<V> outOp(oldVal); impl.Get()->GetAndPut(inOp, outOp, err); - return outOp.GetResult(); + return oldVal; } /** @@ -559,12 +566,14 @@ namespace ignite */ V GetAndReplace(const K& key, const V& val, IgniteError& err) { + V oldVal; + impl::In2Operation<K, V> inOp(key, val); - impl::Out1Operation<V> outOp; + impl::Out1Operation<V> outOp(oldVal); impl.Get()->GetAndReplace(inOp, outOp, err); - return outOp.GetResult(); + return oldVal; } /** @@ -597,12 +606,14 @@ namespace ignite */ V GetAndRemove(const K& key, IgniteError& err) { + V oldVal; + impl::In1Operation<K> inOp(key); - impl::Out1Operation<V> outOp; + impl::Out1Operation<V> outOp(oldVal); impl.Get()->GetAndRemove(inOp, outOp, err); - return outOp.GetResult(); + return oldVal; } /** @@ -694,12 +705,14 @@ namespace ignite */ V GetAndPutIfAbsent(const K& key, const V& val, IgniteError& err) { + V oldVal; + impl::In2Operation<K, V> inOp(key, val); - impl::Out1Operation<V> outOp; + impl::Out1Operation<V> outOp(oldVal); impl.Get()->GetAndPutIfAbsent(inOp, outOp, err); - return outOp.GetResult(); + return oldVal; } /** @@ -1607,14 +1620,15 @@ namespace ignite { typedef impl::cache::CacheEntryProcessorHolder<P, A> ProcessorHolder; + R res; ProcessorHolder procHolder(processor, arg); impl::In2Operation<K, ProcessorHolder> inOp(key, procHolder); - impl::Out1Operation<R> outOp; + impl::Out1Operation<R> outOp(res); impl.Get()->Invoke(inOp, outOp, err); - return outOp.GetResult(); + return res; } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/40432b09/modules/platforms/cpp/core/include/ignite/cache/query/query_cursor.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core/include/ignite/cache/query/query_cursor.h b/modules/platforms/cpp/core/include/ignite/cache/query/query_cursor.h index 3f7ccce..e77706b 100644 --- a/modules/platforms/cpp/core/include/ignite/cache/query/query_cursor.h +++ b/modules/platforms/cpp/core/include/ignite/cache/query/query_cursor.h @@ -158,19 +158,14 @@ namespace ignite impl::cache::query::QueryCursorImpl* impl0 = impl.Get(); if (impl0) { - impl::Out2Operation<K, V> outOp; + K key; + V val; - impl0->GetNext(outOp, err); + impl::Out2Operation<K, V> outOp(key, val); - if (err.GetCode() == IgniteError::IGNITE_SUCCESS) - { - K& key = outOp.Get1(); - V& val = outOp.Get2(); + impl0->GetNext(outOp, err); - return CacheEntry<K, V>(key, val); - } - else - return CacheEntry<K, V>(); + return CacheEntry<K, V>(key, val); } else { http://git-wip-us.apache.org/repos/asf/ignite/blob/40432b09/modules/platforms/cpp/core/include/ignite/impl/operations.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core/include/ignite/impl/operations.h b/modules/platforms/cpp/core/include/ignite/impl/operations.h index 1645fb56..9f816bf 100644 --- a/modules/platforms/cpp/core/include/ignite/impl/operations.h +++ b/modules/platforms/cpp/core/include/ignite/impl/operations.h @@ -66,7 +66,7 @@ namespace ignite public: /** * Constructor. - * + * * @param val Value. */ In1Operation(const T& val) : val(val) @@ -248,7 +248,7 @@ namespace ignite const T& key; /** Peek modes. */ - int32_t peekModes; + int32_t peekModes; IGNITE_NO_COPY_ASSIGNMENT(InCacheLocalPeekOperation) }; @@ -333,15 +333,18 @@ namespace ignite public: /** * Constructor. + * + * @param val Value. */ - Out1Operation() + Out1Operation(T& val) : + val(val) { // No-op. } virtual void ProcessOutput(binary::BinaryReaderImpl& reader) { - val = reader.ReadTopObject<T>(); + reader.ReadTopObject<T>(val); } virtual void SetNull() @@ -349,18 +352,9 @@ namespace ignite val = binary::BinaryUtils::GetDefaultValue<T>(); } - /** - * Get value. - * - * @param Value. - */ - T GetResult() - { - return val; - } private: /** Value. */ - T val; + T& val; IGNITE_NO_COPY_ASSIGNMENT(Out1Operation) }; @@ -374,16 +368,21 @@ namespace ignite public: /** * Constructor. + * + * @param val1 Value 1. + * @param val2 Value 2. */ - Out2Operation() + Out2Operation(T1& val1, T2& val2) : + val1(val1), + val2(val2) { // No-op. } virtual void ProcessOutput(binary::BinaryReaderImpl& reader) { - val1 = reader.ReadTopObject<T1>(); - val2 = reader.ReadTopObject<T2>(); + reader.ReadTopObject<T1>(val1); + reader.ReadTopObject<T2>(val2); } virtual void SetNull() @@ -392,32 +391,12 @@ namespace ignite val2 = binary::BinaryUtils::GetDefaultValue<T2>(); } - /** - * Get value 1. - * - * @param Value 1. - */ - T1& Get1() - { - return val1; - } - - /** - * Get value 2. - * - * @param Value 2. - */ - T2& Get2() - { - return val2; - } - private: /** Value 1. */ - T1 val1; - + T1& val1; + /** Value 2. */ - T2 val2; + T2& val2; IGNITE_NO_COPY_ASSIGNMENT(Out2Operation) }; @@ -431,18 +410,27 @@ namespace ignite public: /** * Constructor. + * + * @param val1 Value 1. + * @param val2 Value 2. + * @param val3 Value 3. + * @param val4 Value 4. */ - Out4Operation() + Out4Operation(T1& val1, T2& val2, T3& val3, T4& val4) : + val1(val1), + val2(val2), + val3(val3), + val4(val4) { // No-op. } virtual void ProcessOutput(binary::BinaryReaderImpl& reader) { - val1 = reader.ReadTopObject<T1>(); - val2 = reader.ReadTopObject<T2>(); - val3 = reader.ReadTopObject<T3>(); - val4 = reader.ReadTopObject<T4>(); + reader.ReadTopObject<T1>(val1); + reader.ReadTopObject<T2>(val2); + reader.ReadTopObject<T3>(val3); + reader.ReadTopObject<T4>(val4); } virtual void SetNull() @@ -453,58 +441,18 @@ namespace ignite val4 = binary::BinaryUtils::GetDefaultValue<T4>(); } - /** - * Get value 1. - * - * @param Value 1. - */ - T1& Get1() - { - return val1; - } - - /** - * Get value 2. - * - * @param Value 2. - */ - T2& Get2() - { - return val2; - } - - /** - * Get value 3. - * - * @param Value 3. - */ - T3& Get3() - { - return val3; - } - - /** - * Get value 4. - * - * @param Value 4. - */ - T4& Get4() - { - return val4; - } - private: /** Value 1. */ - T1 val1; - + T1& val1; + /** Value 2. */ - T2 val2; + T2& val2; /** Value 3. */ - T3 val3; + T3& val3; /** Value 4. */ - T4 val4; + T4& val4; IGNITE_NO_COPY_ASSIGNMENT(Out4Operation) }; @@ -518,8 +466,11 @@ namespace ignite public: /** * Constructor. + * + * @param val Value. */ - OutMapOperation() + OutMapOperation(std::map<T1, T2>& val) : + val(val) { // No-op. } @@ -550,18 +501,9 @@ namespace ignite // No-op. } - /** - * Get value. - * - * @return Value. - */ - std::map<T1, T2> GetResult() - { - return val; - } private: /** Value. */ - std::map<T1, T2> val; + std::map<T1, T2>& val; IGNITE_NO_COPY_ASSIGNMENT(OutMapOperation) }; @@ -587,7 +529,7 @@ namespace ignite res.reserve(res.size() + cnt); - for (int i = 0; i < cnt; i++) + for (int i = 0; i < cnt; i++) { K key = reader.ReadTopObject<K>(); V val = reader.ReadTopObject<V>(); @@ -604,7 +546,7 @@ namespace ignite private: /** Entries. */ std::vector<ignite::cache::CacheEntry<K, V> >& res; - + IGNITE_NO_COPY_ASSIGNMENT(OutQueryGetAllOperation) }; @@ -645,7 +587,7 @@ namespace ignite private: /** Out iter. */ Iter iter; - + IGNITE_NO_COPY_ASSIGNMENT(OutQueryGetAllOperationIter) }; http://git-wip-us.apache.org/repos/asf/ignite/blob/40432b09/modules/platforms/cpp/odbc-test/src/connection_test.cpp ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/odbc-test/src/connection_test.cpp b/modules/platforms/cpp/odbc-test/src/connection_test.cpp index 5badda6..709ef61 100644 --- a/modules/platforms/cpp/odbc-test/src/connection_test.cpp +++ b/modules/platforms/cpp/odbc-test/src/connection_test.cpp @@ -84,12 +84,15 @@ struct ConnectionTestSuiteFixture: odbc::OdbcTestSuite /** * Extract code from ODBC error message. + * + * @param err Error. + * @return Code. */ - std::string ExtractErrorCode(std::string err) + static std::string ExtractErrorCode(const std::string& err) { std::string code; - int idx = err.find(':'); + size_t idx = err.find(':'); if ((idx != std::string::npos) && (idx > 0)) code = err.substr(0, idx);
