From 5e560ff9d795587fd1ecfb0c8f60d4d73039cb67 Mon Sep 17 00:00:00 2001 From: Mike Kot Date: Fri, 1 Oct 2021 16:55:01 +0200 Subject: [PATCH] fix --- CMakeLists.txt | 13 ++++---- base/common/CachedFn.h | 9 +++-- src/Common/examples/CMakeLists.txt | 3 -- src/Common/examples/simple_cache.cpp | 22 ------------ src/Common/tests/gtest_cached_fn.cpp | 50 ++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 37 deletions(-) delete mode 100644 src/Common/examples/simple_cache.cpp create mode 100644 src/Common/tests/gtest_cached_fn.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e0fa6d8e197..9c8903d853c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,7 @@ if (CMAKE_GENERATOR STREQUAL "Ninja" AND NOT DISABLE_COLORED_BUILD) set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fdiagnostics-color=always") endif () +include (cmake/check_flags.cmake) include (cmake/add_warning.cmake) if (NOT MSVC) @@ -166,7 +167,8 @@ if (COMPILER_CLANG) set(COMPILER_FLAGS "${COMPILER_FLAGS} -gdwarf-aranges") endif () - if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0.0) + if (HAS_USE_CTOR_HOMING) + # For more info see https://blog.llvm.org/posts/2021-04-05-constructor-homing-for-debug-info/ if (CMAKE_BUILD_TYPE_UC STREQUAL "DEBUG" OR CMAKE_BUILD_TYPE_UC STREQUAL "RELWITHDEBINFO") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -fuse-ctor-homing") set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Xclang -fuse-ctor-homing") @@ -192,7 +194,7 @@ endif () # Make sure the final executable has symbols exported set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic") -find_program (OBJCOPY_PATH NAMES "llvm-objcopy" "llvm-objcopy-12" "llvm-objcopy-11" "llvm-objcopy-10" "llvm-objcopy-9" "llvm-objcopy-8" "objcopy") +find_program (OBJCOPY_PATH NAMES "llvm-objcopy" "llvm-objcopy-13" "llvm-objcopy-12" "llvm-objcopy-11" "llvm-objcopy-10" "llvm-objcopy-9" "llvm-objcopy-8" "objcopy") if (NOT OBJCOPY_PATH AND OS_DARWIN) find_program (BREW_PATH NAMES "brew") @@ -379,7 +381,7 @@ if (COMPILER_CLANG) endif () # Always prefer llvm tools when using clang. For instance, we cannot use GNU ar when llvm LTO is enabled - find_program (LLVM_AR_PATH NAMES "llvm-ar" "llvm-ar-12" "llvm-ar-11" "llvm-ar-10" "llvm-ar-9" "llvm-ar-8") + find_program (LLVM_AR_PATH NAMES "llvm-ar" "llvm-ar-13" "llvm-ar-12" "llvm-ar-11" "llvm-ar-10" "llvm-ar-9" "llvm-ar-8") if (LLVM_AR_PATH) message(STATUS "Using llvm-ar: ${LLVM_AR_PATH}.") @@ -388,7 +390,7 @@ if (COMPILER_CLANG) message(WARNING "Cannot find llvm-ar. System ar will be used instead. It does not work with ThinLTO.") endif () - find_program (LLVM_RANLIB_PATH NAMES "llvm-ranlib" "llvm-ranlib-12" "llvm-ranlib-11" "llvm-ranlib-10" "llvm-ranlib-9" "llvm-ranlib-8") + find_program (LLVM_RANLIB_PATH NAMES "llvm-ranlib" "llvm-ranlib-13" "llvm-ranlib-12" "llvm-ranlib-11" "llvm-ranlib-10" "llvm-ranlib-9" "llvm-ranlib-8") if (LLVM_RANLIB_PATH) message(STATUS "Using llvm-ranlib: ${LLVM_RANLIB_PATH}.") @@ -629,9 +631,6 @@ include_directories(${ConfigIncludePath}) # Add as many warnings as possible for our own code. include (cmake/warnings.cmake) -# Check if needed compiler flags are supported -include (cmake/check_flags.cmake) - add_subdirectory (base) add_subdirectory (src) add_subdirectory (programs) diff --git a/base/common/CachedFn.h b/base/common/CachedFn.h index 6ecae0b88b3..7c3af0443d8 100644 --- a/base/common/CachedFn.h +++ b/base/common/CachedFn.h @@ -5,13 +5,12 @@ #include "FnTraits.h" /** - * Cache for a functor that decays to a pointer-to-function. - * The size is unlimited. Values are stored permanently and never evicted. - * Suitable only for simplest cases. - * + * Caching proxy for a functor that decays to a pointer-to-function. + * Saves pairs (func args, func result on args). + * Cache size is unlimited. Cache items are evicted only on manual drop. * Invocation/update is O(log(saved cache values)). * - * @example CachedFn<&my_func> cached; cached(arg); + * See Common/tests/cached_fn.cpp for examples. */ template struct CachedFn diff --git a/src/Common/examples/CMakeLists.txt b/src/Common/examples/CMakeLists.txt index 64d28fec5c2..e72681621cb 100644 --- a/src/Common/examples/CMakeLists.txt +++ b/src/Common/examples/CMakeLists.txt @@ -19,9 +19,6 @@ target_link_libraries (parallel_aggregation2 PRIVATE dbms) add_executable (int_hashes_perf int_hashes_perf.cpp) target_link_libraries (int_hashes_perf PRIVATE clickhouse_common_io) -add_executable (simple_cache simple_cache.cpp) -target_link_libraries (simple_cache PRIVATE common) - add_executable (compact_array compact_array.cpp) target_link_libraries (compact_array PRIVATE clickhouse_common_io) diff --git a/src/Common/examples/simple_cache.cpp b/src/Common/examples/simple_cache.cpp deleted file mode 100644 index b6bcfc0b932..00000000000 --- a/src/Common/examples/simple_cache.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - - -static int func(int x, int y) -{ - std::cerr << x << " + " << y << "\n"; - return x + y; -} - - -int main(int, char **) -{ - CachedFn<&func> func_cached; - - std::cerr << func_cached(1, 2) << "\n"; - std::cerr << func_cached(1, 2) << "\n"; - std::cerr << func_cached(1, 2) << "\n"; - std::cerr << func_cached(3, 4) << "\n"; - std::cerr << func_cached(3, 4) << "\n"; - std::cerr << func_cached(3, 4) << "\n"; -} diff --git a/src/Common/tests/gtest_cached_fn.cpp b/src/Common/tests/gtest_cached_fn.cpp new file mode 100644 index 00000000000..51e77018038 --- /dev/null +++ b/src/Common/tests/gtest_cached_fn.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +using namespace std::chrono_literals; + +constexpr int add(int x, int y) +{ + return x + y; +} + +static int longFunction(int x, int y) +{ + std::this_thread::sleep_for(1s); + return x + y; +} + +TEST(CachedFn, Basic) +{ + CachedFn<&add> fn; + + const int res = fn(1, 2); + EXPECT_EQ(fn(1, 2), res); + + auto f = [](int x, int y) { return x - y; }; + CachedFn<+f> fn2; + + const int res2 = fn2(1, 2); + EXPECT_EQ(fn2(1, 2), res2); +} + +TEST(CachedFn, CachingResults) +{ + CachedFn<&longFunction> fn; + + for (int x = 0; x < 2; ++x) + { + for (int y = 0; y < 2; ++y) + { + const int res = fn(x, y); + const time_t start = time(nullptr); + + for (int count = 0; count < 1000; ++count) + EXPECT_EQ(fn(x, y), res); + + EXPECT_LT(time(nullptr) - start, 10); + } + } +}