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

[Reflection] Major rework for much faster compile-times #179

Merged
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### CXX_FLAGS
build --per_file_copt external/.*@-g0,-O3,-fno-sanitize=all,-fexceptions
build:clang --define=using_clang=true
build:clang --per_file_copt external/.*@-Wno-everything,-ftrivial-auto-var-init=uninitialized
build --copt -Werror
build --copt -fno-exceptions
Expand Down
64 changes: 59 additions & 5 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ config_setting(
define_values = {"enable_strip_include_prefix_instead_of_includes": "true"},
)

config_setting(
name = "is_clang_build",
define_values = {"using_clang": "true"},
)

cc_library(
name = "algorithm",
hdrs = ["include/fixed_containers/algorithm.hpp"],
Expand Down Expand Up @@ -857,7 +862,8 @@ cc_library(
":fixed_stack",
":fixed_vector",
":in_out",
":tuples",
":struct_decomposition",
":variadic_templates",
],
copts = ["-std=c++20"],
)
Expand Down Expand Up @@ -918,6 +924,22 @@ cc_library(
copts = ["-std=c++20"],
)

cc_library(
name = "struct_decomposition",
hdrs = [
"include/fixed_containers/struct_decomposition.hpp",
"include/fixed_containers/struct_decomposition_129_to_512.hpp",
"include/fixed_containers/struct_decomposition_513_to_768.hpp",
"include/fixed_containers/struct_decomposition_769_to_1024.hpp",
],
includes = includes_config(),
strip_include_prefix = strip_include_prefix_config(),
deps = [
":concepts",
],
copts = ["-std=c++20"],
)

cc_library(
name = "struct_view",
hdrs = ["include/fixed_containers/struct_view.hpp"],
Expand Down Expand Up @@ -947,10 +969,7 @@ cc_library(

cc_library(
name = "tuples",
hdrs = [
"include/fixed_containers/tuples.hpp",
"include/fixed_containers/tuples_as_tuple_view.hpp"
],
hdrs = ["include/fixed_containers/tuples.hpp"],
includes = includes_config(),
strip_include_prefix = strip_include_prefix_config(),
deps = [
Expand Down Expand Up @@ -979,6 +998,14 @@ cc_library(
copts = ["-std=c++20"],
)

cc_library(
name = "variadic_templates",
hdrs = ["include/fixed_containers/variadic_templates.hpp"],
includes = includes_config(),
strip_include_prefix = strip_include_prefix_config(),
copts = ["-std=c++20"],
)

cc_library(
name = "enums_test_common",
hdrs = ["test/enums_test_common.hpp"],
Expand Down Expand Up @@ -1705,6 +1732,21 @@ cc_test(
copts = ["-std=c++20"],
)

cc_test(
name = "reflection_big_struct_test",
srcs = ["test/reflection_big_struct_test.cpp"],
deps = [
":reflection",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
copts = ["-std=c++20"] +
select({
"//conditions:default": [],
":is_clang_build": ["-fbracket-depth=1024"],
}),
)

cc_test(
name = "reflection_test",
srcs = ["test/reflection_test.cpp"],
Expand Down Expand Up @@ -1743,6 +1785,18 @@ cc_test(
copts = ["-std=c++20"],
)

cc_test(
name = "struct_decomposition_codegen",
srcs = ["test/struct_decomposition_codegen.cpp"],
deps = [
":fixed_string",
":fixed_vector",
":int_math",
":struct_decomposition",
],
copts = ["-std=c++20"],
)

cc_test(
name = "struct_view_test",
srcs = ["test/struct_view_test.cpp"],
Expand Down
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,28 @@ if(BUILD_TESTS)
add_test_dependencies(pair_view_test)
add_executable(queue_adapter_test test/queue_adapter_test.cpp)
add_test_dependencies(queue_adapter_test)
add_executable(reflection_big_struct_test test/reflection_big_struct_test.cpp)
add_test_dependencies(reflection_big_struct_test)
add_executable(reflection_test test/reflection_test.cpp)
add_test_dependencies(reflection_test)
add_executable(stack_adapter_test test/stack_adapter_test.cpp)
add_test_dependencies(stack_adapter_test)
add_executable(string_literal_test test/string_literal_test.cpp)
add_test_dependencies(string_literal_test)
add_executable(struct_decomposition_codegen test/struct_decomposition_codegen.cpp)
add_test_dependencies(struct_decomposition_codegen)
add_executable(struct_view_test test/struct_view_test.cpp)
add_test_dependencies(struct_view_test)
add_executable(tuples_test test/tuples_test.cpp)
add_test_dependencies(tuples_test)
add_executable(type_name_test test/type_name_test.cpp)
add_test_dependencies(type_name_test)
add_executable(variadic_templates_test test/variadic_templates_test.cpp)
add_test_dependencies(variadic_templates_test)

if(${USING_CLANG})
target_compile_options(reflection_big_struct_test PRIVATE -fbracket-depth=1024)
endif()
endif()

option(FIXED_CONTAINERS_OPT_INSTALL "Enable install target" ${PROJECT_IS_TOP_LEVEL})
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ CC=g++-11 bazel build --config=gcc ...
CC=g++-11 bazel test --config=gcc :all_tests
```

### clang-tidy
Run with:
```
run-clang-tidy -p . -extra-arg-before="-DFIXED_CONTAINERS_CLANG_TIDY_RUNNING"
```
The macro is needed to avoid analysis on some particularly slow places.

## Tested Compilers

- Clang 13
Expand Down
34 changes: 19 additions & 15 deletions include/fixed_containers/fixed_map_raw_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ class FixedMapRawView
class Iterator
{
public:

// I use Entry so we can match UnorderedMapRawView's API where,
// *it returns an object with .key() and .value() methods.
class Entry {

class Entry
{
private:
FixedRedBlackTreeRawView::Iterator base_iterator_;
std::size_t value_offset_;
Expand All @@ -38,24 +37,25 @@ class FixedMapRawView
std::size_t max_size_bytes,
Compactness compactness,
StorageType storage_type,
bool end = false) noexcept:
base_iterator_(ptr,
element_size_bytes,
max_size_bytes,
compactness,
storage_type,
end),
value_offset_(value_offset_bytes) {}
bool end = false) noexcept
: base_iterator_(
ptr, element_size_bytes, max_size_bytes, compactness, storage_type, end)
, value_offset_(value_offset_bytes)
{
}

Entry(): base_iterator_(), value_offset_(0) {}
Entry()
: base_iterator_()
, value_offset_(0)
{
}

[[nodiscard]] const std::byte* key() const { return *base_iterator_; }

[[nodiscard]] const std::byte* value() const
{
return std::next(*base_iterator_, static_cast<std::ptrdiff_t>(value_offset_));
}

};

private:
Expand All @@ -82,15 +82,19 @@ class FixedMapRawView
bool end = false) noexcept
: entry_(ptr,
align_up(key_size_bytes, value_align_bytes),
align_up(align_up(key_size_bytes, value_align_bytes) + value_size_bytes, key_align_bytes),
align_up(align_up(key_size_bytes, value_align_bytes) + value_size_bytes,
key_align_bytes),
max_size_bytes,
compactness,
storage_type,
end)
{
}

Iterator() noexcept: entry_() {}
Iterator() noexcept
: entry_()
{
}
Iterator(const Iterator&) noexcept = default;
Iterator(Iterator&&) noexcept = default;
Iterator& operator=(const Iterator&) noexcept = default;
Expand Down
9 changes: 8 additions & 1 deletion include/fixed_containers/fixed_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ class FixedString
null_terminate(loc);
}

template <class InputIt>
constexpr FixedString& append(
const CharT* char_ptr,
const std_transition::source_location& loc = std_transition::source_location::current())
{
return append(std::string_view{char_ptr}, loc);
}
template <class InputIt>
constexpr FixedString& append(
InputIt first,
Expand Down Expand Up @@ -339,7 +346,7 @@ class FixedString
}
constexpr FixedString& operator+=(const CharT* char_ptr)
{
return append(std::string_view{char_ptr}, std_transition::source_location::current());
return append(char_ptr, std_transition::source_location::current());
}
constexpr FixedString& operator+=(std::initializer_list<CharT> ilist)
{
Expand Down
17 changes: 12 additions & 5 deletions include/fixed_containers/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include "fixed_containers/fixed_stack.hpp"
#include "fixed_containers/fixed_vector.hpp"
#include "fixed_containers/in_out.hpp"
#include "fixed_containers/tuples.hpp"
#include "fixed_containers/struct_decomposition.hpp"
#include "fixed_containers/variadic_templates.hpp"

#include <concepts>
#include <memory>
Expand Down Expand Up @@ -240,10 +241,16 @@ template <typename T, typename Func>
constexpr void for_each_field(T&& instance, Func&& func)
{
constexpr const auto& FIELD_NAMES = field_names_of<T>();
auto tuple_view = tuples::as_tuple_view<FIELD_NAMES.size()>(instance);
tuples::for_each_entry(tuple_view,
[&func]<typename Field>(std::size_t index, Field&& field)
{ func(FIELD_NAMES.at(index), std::forward<Field>(field)); });
struct_decomposition::to_parameter_pack<FIELD_NAMES.size()>(
instance,
[&func]<typename... Args>(Args&&... args) -> bool
{
variadic_templates::for_each_entry(
[&]<typename Field>(std::size_t index, Field&& field)
{ func(FIELD_NAMES.at(index), std::forward<Field>(field)); },
std::forward<Args>(args)...);
return true;
});
}

} // namespace fixed_containers::reflection
Loading
Loading