Skip to content

Commit

Permalink
Merge pull request #272 from traceon/various-perf-optimizations
Browse files Browse the repository at this point in the history
Various perf optimizations
  • Loading branch information
Enmk authored Mar 2, 2020
2 parents d783b44 + a0b51ad commit 11eaded
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 55 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
path = contrib/googletest
url = https://github.com/google/googletest.git
branch = master
[submodule "contrib/folly"]
path = contrib/folly
url = https://github.com/facebook/folly.git
branch = master
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ cmake_dependent_option (CH_ODBC_ENABLE_TESTING "Enable test targets" ON "BUILD_T
option (CH_ODBC_PREFER_BUNDLED_THIRD_PARTIES "Prefer bundled over system variants of third party libraries" ON)
cmake_dependent_option (CH_ODBC_PREFER_BUNDLED_POCO "Prefer bundled over system variants of Poco library" ON "CH_ODBC_PREFER_BUNDLED_THIRD_PARTIES" OFF)
cmake_dependent_option (CH_ODBC_PREFER_BUNDLED_SSL "Prefer bundled over system variants of SSL library" ON "CH_ODBC_PREFER_BUNDLED_POCO" OFF)
cmake_dependent_option (CH_ODBC_PREFER_BUNDLED_FOLLY "Prefer bundled over system variants of Folly library" ON "CH_ODBC_PREFER_BUNDLED_THIRD_PARTIES" OFF)
cmake_dependent_option (CH_ODBC_PREFER_BUNDLED_GOOGLETEST "Prefer bundled over system variants of Google Test library" ON "CH_ODBC_PREFER_BUNDLED_THIRD_PARTIES" OFF)
cmake_dependent_option (CH_ODBC_PREFER_BUNDLED_NANODBC "Prefer bundled over system variants of nanodbc library" ON "CH_ODBC_PREFER_BUNDLED_THIRD_PARTIES" OFF)
option (CH_ODBC_RUNTIME_LINK_STATIC "Link with compiler and language runtime statically" OFF)
Expand Down Expand Up @@ -131,6 +132,11 @@ if (NOT CH_ODBC_PREFER_BUNDLED_POCO)
find_package (Poco COMPONENTS Foundation Net NetSSL)
endif ()

if (NOT CH_ODBC_PREFER_BUNDLED_FOLLY)
message (WARNING "Folly: using system variant of the library currently not supported")
# find_package (Folly)
endif ()

if (CH_ODBC_ENABLE_TESTING)
if (NOT CH_ODBC_PREFER_BUNDLED_GOOGLETEST)
find_package (GTest)
Expand Down
9 changes: 9 additions & 0 deletions contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ if (MSVC)
endif ()
endif ()

# if (CH_ODBC_PREFER_BUNDLED_FOLLY OR NOT Folly_FOUND)
# if (NOT Folly_FOUND AND NOT CH_ODBC_PREFER_BUNDLED_FOLLY)
# message (WARNING "Folly: unable to find system Folly, falling back to using the bundled variant of the library")
# endif ()
#
# add_subdirectory (folly)
set (FOLLY_INLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/folly" CACHE INTERNAL "")
# endif ()

if (CH_ODBC_ENABLE_TESTING)
if (CH_ODBC_PREFER_BUNDLED_GOOGLETEST OR NOT GTEST_FOUND)
if (NOT GTEST_FOUND AND NOT CH_ODBC_PREFER_BUNDLED_GOOGLETEST)
Expand Down
1 change: 1 addition & 0 deletions contrib/folly
Submodule folly added at 9d9dad
1 change: 1 addition & 0 deletions driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ if (UNICODE)
endif ()

target_include_directories (${libname}-impl
PUBLIC ${FOLLY_INLUDE_DIR} # TODO: switch to linking with folly lib target?
PUBLIC ${PROJECT_BINARY_DIR}
PUBLIC ${PROJECT_SOURCE_DIR}
)
Expand Down
31 changes: 15 additions & 16 deletions driver/format/ODBCDriver2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "driver/format/ODBCDriver2.h"

ODBCDriver2ResultSet::ODBCDriver2ResultSet(std::istream & stream, std::unique_ptr<ResultMutator> && mutator)
ODBCDriver2ResultSet::ODBCDriver2ResultSet(AmortizedIStreamReader & stream, std::unique_ptr<ResultMutator> && mutator)
: ResultSet(stream, std::move(mutator))
{
std::int32_t num_header_rows = 0;
Expand Down Expand Up @@ -57,7 +57,7 @@ ODBCDriver2ResultSet::ODBCDriver2ResultSet(std::istream & stream, std::unique_pt
}

bool ODBCDriver2ResultSet::readNextRow(Row & row) {
if (raw_stream.peek() == EOF)
if (stream.eof())
return false;

for (std::size_t i = 0; i < row.fields.size(); ++i) {
Expand All @@ -68,32 +68,31 @@ bool ODBCDriver2ResultSet::readNextRow(Row & row) {
}

void ODBCDriver2ResultSet::readSize(std::int32_t & dest) {
constexpr auto size = sizeof(dest);
raw_stream.read(reinterpret_cast<char *>(&dest), size);

if (raw_stream.gcount() != size)
throw std::runtime_error("Incomplete result received, expected size: " + std::to_string(size));
stream.read(reinterpret_cast<char *>(&dest), sizeof(std::int32_t));
}

void ODBCDriver2ResultSet::readValue(std::string & dest, bool * is_null) {
std::int32_t size = 0;
readSize(size);

if (size >= 0) {
dest.resize(size); // TODO: switch to uninitializing resize().
resize_without_initialization(dest, size);

if (is_null)
*is_null = false;

if (size > 0) {
raw_stream.read(dest.data(), size);

if (raw_stream.gcount() != size)
throw std::runtime_error("Incomplete result received, expected size: " + std::to_string(size));
try {
stream.read(dest.data(), size);
}
catch (...) {
dest.clear();
throw;
}
}
}
else /*if (size == -1) */{
dest.resize(0);
dest.clear();

if (is_null)
*is_null = true;
Expand Down Expand Up @@ -235,10 +234,10 @@ void ODBCDriver2ResultSet::readValue(std::string & src, DataSourceType<DataSourc
return value_manip::from_value<std::string>::template to_value<DataSourceType<DataSourceTypeId::UUID>>::convert(src, dest);
}

ODBCDriver2ResultReader::ODBCDriver2ResultReader(std::istream & stream, std::unique_ptr<ResultMutator> && mutator)
: ResultReader(stream, std::move(mutator))
ODBCDriver2ResultReader::ODBCDriver2ResultReader(std::istream & raw_stream, std::unique_ptr<ResultMutator> && mutator)
: ResultReader(raw_stream, std::move(mutator))
{
if (stream.peek() == EOF)
if (stream.eof())
return;

result_set = std::make_unique<ODBCDriver2ResultSet>(stream, releaseMutator());
Expand Down
4 changes: 2 additions & 2 deletions driver/format/ODBCDriver2.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ODBCDriver2ResultSet
: public ResultSet
{
public:
explicit ODBCDriver2ResultSet(std::istream & stream, std::unique_ptr<ResultMutator> && mutator);
explicit ODBCDriver2ResultSet(AmortizedIStreamReader & stream, std::unique_ptr<ResultMutator> && mutator);
virtual ~ODBCDriver2ResultSet() override = default;

protected:
Expand Down Expand Up @@ -61,7 +61,7 @@ class ODBCDriver2ResultReader
: public ResultReader
{
public:
explicit ODBCDriver2ResultReader(std::istream & stream, std::unique_ptr<ResultMutator> && mutator);
explicit ODBCDriver2ResultReader(std::istream & raw_stream, std::unique_ptr<ResultMutator> && mutator);
virtual ~ODBCDriver2ResultReader() override = default;

virtual bool advanceToNextResultSet() override;
Expand Down
39 changes: 16 additions & 23 deletions driver/format/RowBinaryWithNamesAndTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <ctime>

RowBinaryWithNamesAndTypesResultSet::RowBinaryWithNamesAndTypesResultSet(std::istream & stream, std::unique_ptr<ResultMutator> && mutator)
RowBinaryWithNamesAndTypesResultSet::RowBinaryWithNamesAndTypesResultSet(AmortizedIStreamReader & stream, std::unique_ptr<ResultMutator> && mutator)
: ResultSet(stream, std::move(mutator))
{
std::uint64_t num_columns = 0;
Expand Down Expand Up @@ -35,7 +35,7 @@ RowBinaryWithNamesAndTypesResultSet::RowBinaryWithNamesAndTypesResultSet(std::is
}

bool RowBinaryWithNamesAndTypesResultSet::readNextRow(Row & row) {
if (raw_stream.peek() == EOF)
if (stream.eof())
return false;

for (std::size_t i = 0; i < row.fields.size(); ++i) {
Expand All @@ -53,10 +53,7 @@ void RowBinaryWithNamesAndTypesResultSet::readSize(std::uint64_t & res) {
std::uint8_t shift = 0;

while (true) {
auto byte = raw_stream.get();

if (raw_stream.fail() || byte == EOF)
throw std::runtime_error("Incomplete result received, expected: at least 1 more byte");
const int byte = stream.get();

const std::uint64_t chunk = (byte & 0b01111111);
const std::uint64_t segment = (chunk << shift);
Expand All @@ -80,11 +77,7 @@ void RowBinaryWithNamesAndTypesResultSet::readSize(std::uint64_t & res) {
}

void RowBinaryWithNamesAndTypesResultSet::readValue(bool & dest) {
auto byte = raw_stream.get();

if (raw_stream.fail() || byte == EOF)
throw std::runtime_error("Incomplete result received, expected size: 1");

const int byte = stream.get();
dest = (byte != 0);
}

Expand All @@ -95,11 +88,15 @@ void RowBinaryWithNamesAndTypesResultSet::readValue(std::string & res) {
}

void RowBinaryWithNamesAndTypesResultSet::readValue(std::string & dest, const std::uint64_t size) {
dest.resize(size); // TODO: switch to uninitializing resize().
raw_stream.read(dest.data(), dest.size());
resize_without_initialization(dest, size);

if (raw_stream.gcount() != dest.size())
throw std::runtime_error("Incomplete result received, expected size: " + std::to_string(size));
try {
stream.read(dest.data(), dest.size());
}
catch (...) {
dest.clear();
throw;
}
}

void RowBinaryWithNamesAndTypesResultSet::readValue(Field & dest, ColumnInfo & column_info) {
Expand Down Expand Up @@ -285,11 +282,7 @@ void RowBinaryWithNamesAndTypesResultSet::readValue(DataSourceType<DataSourceTyp
char buf[16];

static_assert(sizeof(dest.value) == lengthof(buf));

raw_stream.read(buf, lengthof(buf));

if (raw_stream.gcount() != lengthof(buf))
throw std::runtime_error("Incomplete result received, expected size: " + std::to_string(lengthof(buf)));
stream.read(buf, lengthof(buf));

auto * ptr = buf;

Expand All @@ -300,10 +293,10 @@ void RowBinaryWithNamesAndTypesResultSet::readValue(DataSourceType<DataSourceTyp
std::copy(ptr, ptr + lengthof(dest.value.Data4), std::make_reverse_iterator(dest.value.Data4 + lengthof(dest.value.Data4)));
}

RowBinaryWithNamesAndTypesResultReader::RowBinaryWithNamesAndTypesResultReader(std::istream & stream, std::unique_ptr<ResultMutator> && mutator)
: ResultReader(stream, std::move(mutator))
RowBinaryWithNamesAndTypesResultReader::RowBinaryWithNamesAndTypesResultReader(std::istream & raw_stream, std::unique_ptr<ResultMutator> && mutator)
: ResultReader(raw_stream, std::move(mutator))
{
if (stream.peek() == EOF)
if (stream.eof())
return;

result_set = std::make_unique<RowBinaryWithNamesAndTypesResultSet>(stream, releaseMutator());
Expand Down
10 changes: 3 additions & 7 deletions driver/format/RowBinaryWithNamesAndTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RowBinaryWithNamesAndTypesResultSet
: public ResultSet
{
public:
explicit RowBinaryWithNamesAndTypesResultSet(std::istream & stream, std::unique_ptr<ResultMutator> && mutator);
explicit RowBinaryWithNamesAndTypesResultSet(AmortizedIStreamReader & stream, std::unique_ptr<ResultMutator> && mutator);
virtual ~RowBinaryWithNamesAndTypesResultSet() override = default;

protected:
Expand All @@ -23,11 +23,7 @@ class RowBinaryWithNamesAndTypesResultSet

template <typename T>
void readPOD(T & dest) {
constexpr auto size = sizeof(T);
raw_stream.read(reinterpret_cast<char *>(&dest), size);

if (raw_stream.gcount() != size)
throw std::runtime_error("Incomplete result received, expected size: " + std::to_string(size));
stream.read(reinterpret_cast<char *>(&dest), sizeof(T));
}

void readValue(Field & dest, ColumnInfo & column_info);
Expand Down Expand Up @@ -73,7 +69,7 @@ class RowBinaryWithNamesAndTypesResultReader
: public ResultReader
{
public:
explicit RowBinaryWithNamesAndTypesResultReader(std::istream & stream, std::unique_ptr<ResultMutator> && mutator);
explicit RowBinaryWithNamesAndTypesResultReader(std::istream & raw_stream, std::unique_ptr<ResultMutator> && mutator);
virtual ~RowBinaryWithNamesAndTypesResultReader() override = default;

virtual bool advanceToNextResultSet() override;
Expand Down
8 changes: 4 additions & 4 deletions driver/result_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ SQLRETURN Row::extractField(std::size_t column_idx, BindingInfo & binding_info)
return fields[column_idx].extract(binding_info);
}

ResultSet::ResultSet(std::istream & stream, std::unique_ptr<ResultMutator> && mutator)
: raw_stream(stream)
ResultSet::ResultSet(AmortizedIStreamReader & str, std::unique_ptr<ResultMutator> && mutator)
: stream(str)
, result_mutator(std::move(mutator))
, string_pool(1000000)
, row_pool(1000000)
Expand Down Expand Up @@ -296,8 +296,8 @@ void ResultSet::retireRow(Row && row) {
row_pool.put(std::move(row));
}

ResultReader::ResultReader(std::istream & stream, std::unique_ptr<ResultMutator> && mutator)
: raw_stream(stream)
ResultReader::ResultReader(std::istream & raw_stream, std::unique_ptr<ResultMutator> && mutator)
: stream(raw_stream)
, result_mutator(std::move(mutator))
{
}
Expand Down
6 changes: 3 additions & 3 deletions driver/result_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ResultMutator {

class ResultSet {
public:
explicit ResultSet(std::istream & stream, std::unique_ptr<ResultMutator> && mutator);
explicit ResultSet(AmortizedIStreamReader & str, std::unique_ptr<ResultMutator> && mutator);

virtual ~ResultSet();

Expand All @@ -111,7 +111,7 @@ class ResultSet {
virtual bool readNextRow(Row & row) = 0;

protected:
std::istream & raw_stream;
AmortizedIStreamReader & stream;
std::unique_ptr<ResultMutator> result_mutator;
std::vector<ColumnInfo> columns_info;
std::deque<Row> row_set;
Expand Down Expand Up @@ -139,7 +139,7 @@ class ResultReader {
virtual bool advanceToNextResultSet() = 0;

protected:
std::istream & raw_stream;
AmortizedIStreamReader stream;
std::unique_ptr<ResultMutator> result_mutator;
std::unique_ptr<ResultSet> result_set;
};
Expand Down
Loading

0 comments on commit 11eaded

Please sign in to comment.