Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Precompiled header for modules & Refine by PImpl to accelerate release build #5047

Merged
merged 18 commits into from
Jun 8, 2022
Merged
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ endif ()
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

option (USE_CCACHE "Set to OFF to disable ccache" ON)
if (USE_CCACHE)
set(NOT_USE_CCACHE 0)
else()
set(NOT_USE_CCACHE 1)
endif()

option(ENABLE_PCH "Enable `Precompiled header`" OFF)

include (cmake/find_ccache.cmake)

if (NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "None")
Expand Down
14 changes: 14 additions & 0 deletions cmake/find_ccache.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ if (USE_CCACHE AND CCACHE_FOUND AND NOT CMAKE_CXX_COMPILER_LAUNCHER MATCHES "cca
message ("${CCACHE_CONFIG}")
set_property (GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_FOUND})
set_property (GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE_FOUND})

if (ENABLE_PCH)
execute_process (COMMAND ${CCACHE_FOUND} --get-config sloppiness OUTPUT_VARIABLE _CCACHE_SLOPPINESS OUTPUT_STRIP_TRAILING_WHITESPACE)
string (FIND "${_CCACHE_SLOPPINESS}" "pch_defines" _CCACHE_SLOPPINESS_RES)
if (NOT _CCACHE_SLOPPINESS_RES STREQUAL "-1")
string (FIND "${_CCACHE_SLOPPINESS}" "time_macros" _CCACHE_SLOPPINESS_RES)
endif ()

if (_CCACHE_SLOPPINESS_RES STREQUAL "-1")
message(WARNING "`Precompiled header` won't be cached by ccache, sloppiness = `${CCACHE_SLOPPINESS}`,please execute `ccache -o sloppiness=pch_defines,time_macros`")
set (ENABLE_PCH FALSE CACHE BOOL "" FORCE)
endif ()
endif ()

else ()
message (STATUS "Not using ccache ${CCACHE_FOUND}, USE_CCACHE=${USE_CCACHE}")
endif ()
32 changes: 32 additions & 0 deletions dbms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ target_include_directories (clickhouse_common_io BEFORE PRIVATE ${COMMON_INCLUDE
# https://cmake.org/pipermail/cmake/2016-May/063400.html
target_link_libraries (clickhouse_common_io PUBLIC ${TIFLASH_XXHASH_LIBRARY})

function(add_target_pch context target)
if (ENABLE_PCH)
message(STATUS "Add PCH `${context}` for target `${target}`")
target_precompile_headers(${target} PRIVATE ${context})
endif ()
if(${ARGC} GREATER 2)
add_target_pch(${context} ${ARGN})
endif()
endfunction()

if (ENABLE_TESTS)
include (${TiFlash_SOURCE_DIR}/cmake/find_gtest.cmake)

Expand Down Expand Up @@ -297,6 +307,8 @@ if (ENABLE_TESTS)
target_compile_options(gtests_dbms PRIVATE -Wno-unknown-pragmas -Wno-deprecated-copy)
add_check(gtests_dbms)

add_target_pch("pch-dbms.h" gtests_dbms)

grep_bench_sources(${TiFlash_SOURCE_DIR}/dbms dbms_bench_sources)
add_executable(bench_dbms EXCLUDE_FROM_ALL
${dbms_bench_sources}
Expand Down Expand Up @@ -342,3 +354,23 @@ if (TEST_COVERAGE AND CMAKE_BUILD_TYPE STREQUAL "Debug")
)
endif ()
endif ()

# dbms
add_target_pch("pch-dbms.h" dbms flash_service)
add_target_pch("pch-common.h" clickhouse_common_io clickhouse_functions clickhouse_aggregate_functions)
add_target_pch("pch-common.h" clickhouse_parsers clickhouse_storages_system dt-workload-lib clickhouse-server-lib)

# common
add_target_pch("pch-kvpb.h" kv_client)

add_target_pch("pch-stl.h" ${Boost_SYSTEM_LIBRARY} cctz ${RE2_LIBRARY} ${RE2_ST_LIBRARY})

# grpc
add_target_pch("$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/pch-stl.h>" grpc grpc++)

# pb
add_target_pch("pch-stl.h" libprotobuf kvproto tipb libprotoc)

# poco
add_target_pch("pch-stl.h" Net Crypto Util Data NetSSL)
add_target_pch("$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/pch-stl.h>" XML Foundation JSON)
23 changes: 23 additions & 0 deletions dbms/pch-common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2022 PingCAP, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <Common/Exception.h>
#include <Common/FmtUtils.h>
#include <Common/StackTrace.h>
#include <common/StringRef.h>
#include <common/types.h>

#include "pch-stl.h"
18 changes: 18 additions & 0 deletions dbms/pch-dbms.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2022 PingCAP, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "pch-common.h"
#include "pch-kvpb.h"
26 changes: 26 additions & 0 deletions dbms/pch-kvpb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 PingCAP, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

/**
There may be unexpected behaviors for `ccache` to deal with PCH which includes those header files generated by tools:
'xxx' has been modified since the precompiled header 'xxx' was built: mtime changed
`Precompiled header includes xxx.h, which has a new mtime`
*/
#include <kvproto/enginepb.pb.h>
#include <kvproto/pdpb.grpc.pb.h>
#include <kvproto/tikvpb.grpc.pb.h>

#include "pch-stl.h"
34 changes: 34 additions & 0 deletions dbms/pch-stl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2022 PingCAP, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <fstream>
#include <functional>
#include <future>
#include <map>
#include <memory>
#include <mutex>
#include <ostream>
#include <queue>
#include <random>
#include <shared_mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
10 changes: 4 additions & 6 deletions dbms/src/Common/FmtUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#pragma once

#include <common/StringRef.h>
#include <fmt/core.h>
#include <fmt/format.h>

namespace DB
Expand All @@ -32,9 +30,9 @@ class FmtBuffer
return *this;
}

FmtBuffer & append(StringRef s)
FmtBuffer & append(std::string_view s)
{
buffer.append(s.data, s.data + s.size);
buffer.append(s.data(), s.data() + s.size());
return *this;
}

Expand All @@ -55,7 +53,7 @@ class FmtBuffer
FmtBuffer & joinStr(
Iter first,
Iter end,
StringRef delimiter)
std::string_view delimiter)
{
auto func = [](const auto & s, FmtBuffer & fb) {
fb.append(s);
Expand All @@ -68,7 +66,7 @@ class FmtBuffer
Iter first,
Iter end,
FF && toStringFunc, // void (const auto &, FmtBuffer &)
StringRef delimiter)
std::string_view delimiter)
{
if (first == end)
return *this;
Expand Down
77 changes: 75 additions & 2 deletions dbms/src/Common/TiFlashException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@

namespace DB
{
struct TiFlashErrorRegistry::Errors : std::map<std::pair<std::string, std::string>, TiFlashError>
{
};

TiFlashErrorRegistry::Errors & TiFlashErrorRegistry::errors()
{
return *inner_data;
}

TiFlashErrorRegistry::Errors & TiFlashErrorRegistry::errors() const
{
return *inner_data;
}

TiFlashErrorRegistry::TiFlashErrorRegistry()
: inner_data(new Errors{})
{
initialize();
}

TiFlashErrorRegistry::~TiFlashErrorRegistry()
{
delete inner_data;
inner_data = nullptr;
}

void TiFlashErrorRegistry::initialize()
{
// Used to check uniqueness of classes
Expand Down Expand Up @@ -46,9 +72,9 @@ void TiFlashErrorRegistry::initialize()
void TiFlashErrorRegistry::registerError(const std::string & error_class, const std::string & error_code, const std::string & description, const std::string & workaround, const std::string & message_template)
{
TiFlashError error{error_class, error_code, description, workaround, message_template};
if (all_errors.find({error_class, error_code}) == all_errors.end())
if (errors().find({error_class, error_code}) == errors().end())
{
all_errors.emplace(std::make_pair(error_class, error_code), std::move(error));
errors().emplace(std::make_pair(error_class, error_code), std::move(error));
}
else
{
Expand Down Expand Up @@ -77,4 +103,51 @@ std::string TiFlashException::standardText() const
return text;
}

std::optional<TiFlashError> TiFlashErrorRegistry::get(const std::string & error_class, const std::string & error_code) const
{
auto error = errors().find({error_class, error_code});
if (error != errors().end())
{
return error->second;
}
else
{
return {};
}
}
std::optional<TiFlashError> TiFlashErrorRegistry::get(const std::string & error_class, int error_code) const
{
return get(error_class, std::to_string(error_code));
}

std::vector<TiFlashError> TiFlashErrorRegistry::allErrors() const
{
std::vector<TiFlashError> res;
res.reserve(errors().size());
for (const auto & error : errors())
{
res.push_back(error.second);
}
return res;
}

TiFlashError TiFlashErrorRegistry::simpleGet(const std::string & error_class, const std::string & error_code)
{
auto & m_instance = instance();
auto error = m_instance.get(error_class, error_code);
if (error.has_value())
{
return error.value();
}
else
{
throw Exception("Unregistered TiFlashError: FLASH:" + error_class + ":" + error_code);
}
}
TiFlashError TiFlashErrorRegistry::simpleGet(const std::string & error_class, int error_code)
{
return simpleGet(error_class, std::to_string(error_code));
}


} // namespace DB
Loading