Skip to content

Commit

Permalink
Added experimental inline vtable for polymorphic (jbcoe#79)
Browse files Browse the repository at this point in the history
Alternative implementation of polymorphic uses a hand-rolled vtable to try and improve performance.

Enable experimental header by defining XYZ_POLYMORPHIC_USES_EXPERIMENTAL_INLINE_VTABLE and including polymorphic.h
  • Loading branch information
jbcoe authored Oct 17, 2023
1 parent 681303f commit cec23b5
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
test --test_output=errors

build --cxxopt='-std=c++20'

build:asan --strip=never
Expand Down
56 changes: 56 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ cc_library(
copts = ["-Iexternal/value_types/"],
)

cc_library(
name = "polymorphic_inline_vtable",
defines = ["XYZ_POLYMORPHIC_USES_EXPERIMENTAL_INLINE_VTABLE"],
hdrs = [
"experimental/polymorphic_inline_vtable.h",
"polymorphic.h",
],
copts = ["-Iexternal/value_types/"],
)

cc_test(
name = "indirect_test",
size = "small",
Expand Down Expand Up @@ -74,3 +84,49 @@ cc_binary(
"@com_google_benchmark//:benchmark_main",
],
)

cc_binary(
name = "polymorphic_benchmark",
srcs = [
"polymorphic_benchmark.cc",
],
deps = [
":polymorphic",
"@com_google_benchmark//:benchmark_main",
],
)

cc_test(
name = "polymorphic_inline_vtable_test",
size = "small",
srcs = ["polymorphic_test.cc"],
deps = [
"polymorphic_inline_vtable",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "polymorphic_consteval_inline_vtable",
srcs = ["polymorphic_consteval.cc"],
deps = [
"polymorphic_inline_vtable",
],
)

build_test(
name = "polymorphic_consteval_inline_vtable_build_test",
targets = [
"polymorphic_consteval",
],
)

cc_binary(
name = "polymorphic_benchmark_inline_vtable",
srcs = [
"polymorphic_benchmark.cc",
],
deps = [
":polymorphic_inline_vtable",
"@com_google_benchmark//:benchmark_main",
],
)
79 changes: 64 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ target_include_directories(value_types
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_sources(value_types
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/indirect.h>
Expand All @@ -36,7 +35,6 @@ target_sources(value_types
# $<BUILD_INTERFACE:$<$<CXX_COMPILER_ID:MSVC>:${CMAKE_CURRENT_SOURCE_DIR}/indirect_value.natvis>>
# $<INSTALL_INTERFACE:$<$<BOOL:${ENABLE_INCLUDE_NATVIS}>:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}/indirect_value.natvis>>
)

target_compile_features(value_types
INTERFACE
cxx_std_23
Expand All @@ -48,12 +46,10 @@ target_include_directories(indirect
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_sources(indirect
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/indirect.h>
)

target_compile_features(indirect
INTERFACE
cxx_std_23
Expand All @@ -65,17 +61,36 @@ target_include_directories(polymorphic
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_sources(polymorphic
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/polymorphic.h>
)

target_compile_features(polymorphic
INTERFACE
cxx_std_23
)

add_library(polymorphic_inline_vtable INTERFACE)
target_include_directories(polymorphic_inline_vtable
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_sources(polymorphic_inline_vtable
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/polymorphic.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/experimental/polymorphic_inline_vtable.h>
)
target_compile_features(polymorphic_inline_vtable
INTERFACE
cxx_std_23
)
target_compile_definitions(polymorphic_inline_vtable
INTERFACE
-DXYZ_POLYMORPHIC_USES_EXPERIMENTAL_INLINE_VTABLE
)


add_library(value_types::value_types ALIAS value_types)

if (${CPP_VALUE_TYPES_IS_NOT_SUBPROJECT})
Expand All @@ -97,15 +112,13 @@ if (${CPP_VALUE_TYPES_IS_NOT_SUBPROJECT})
FetchContent_MakeAvailable(benchmark)

add_library(common_compiler_settings INTERFACE)

target_compile_options(common_compiler_settings
INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:/EHsc>
$<$<CXX_COMPILER_ID:MSVC>:/W4>
$<$<CXX_COMPILER_ID:MSVC>:/bigobj>
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>:-Werror;-Wall;-Wno-self-assign-overloaded;-Wno-unknown-warning-option>
)

set_target_properties(common_compiler_settings PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
Expand All @@ -117,7 +130,6 @@ if (${CPP_VALUE_TYPES_IS_NOT_SUBPROJECT})
PRIVATE
indirect_test.cc
)

target_link_libraries(indirect_test
PRIVATE
indirect
Expand All @@ -130,26 +142,61 @@ if (${CPP_VALUE_TYPES_IS_NOT_SUBPROJECT})
PRIVATE
polymorphic_test.cc
)

target_link_libraries(polymorphic_test
PRIVATE
polymorphic
GTest::gtest_main
common_compiler_settings
)

add_executable(polymorphic_inline_vtable_test "")
target_sources(polymorphic_inline_vtable_test
PRIVATE
polymorphic_test.cc
)
target_link_libraries(polymorphic_inline_vtable_test
PRIVATE
polymorphic_inline_vtable
GTest::gtest_main
common_compiler_settings
)

add_executable(value_types_benchmark "")
target_sources(value_types_benchmark
PRIVATE
indirect_benchmark.cc
polymorphic_benchmark.cc
)
target_link_libraries(value_types_benchmark
PRIVATE
polymorphic
indirect
benchmark::benchmark_main
common_compiler_settings
PRIVATE
polymorphic
indirect
benchmark::benchmark_main
common_compiler_settings
)

add_executable(polymorphic_benchmark "")
target_sources(polymorphic_benchmark
PRIVATE
polymorphic_benchmark.cc
)
target_link_libraries(polymorphic_benchmark
PRIVATE
polymorphic
benchmark::benchmark_main
common_compiler_settings
)

add_executable(polymorphic_inline_vtable_benchmark "")
target_sources(polymorphic_inline_vtable_benchmark
PRIVATE
polymorphic_benchmark.cc
)
target_link_libraries(polymorphic_inline_vtable_benchmark
PRIVATE
polymorphic_inline_vtable
benchmark::benchmark_main
common_compiler_settings
)

if (ENABLE_SANITIZERS)
Expand Down Expand Up @@ -207,6 +254,7 @@ if (${CPP_VALUE_TYPES_IS_NOT_SUBPROJECT})
include(GoogleTest)
gtest_discover_tests(indirect_test)
gtest_discover_tests(polymorphic_test)
gtest_discover_tests(polymorphic_inline_vtable_test)

if (ENABLE_CODE_COVERAGE)
FetchContent_Declare(
Expand All @@ -224,6 +272,7 @@ if (${CPP_VALUE_TYPES_IS_NOT_SUBPROJECT})
find_package(codecov)
add_coverage(indirect_test)
add_coverage(polymorphic_test)
add_coverage(polymorphic_inline_vtable_test)
list(APPEND LCOV_REMOVE_PATTERNS "'/usr/*'")
coverage_evaluate()
endif()
Expand Down
Loading

0 comments on commit cec23b5

Please sign in to comment.