From 1c7f5c391dcb3a3f72886e2d0cf8b9321883902c Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 24 Jul 2023 17:28:32 +0800 Subject: [PATCH 1/4] update --- frozen/bits/pmh.h | 4 ++-- iguana/reflection.hpp | 36 +++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/frozen/bits/pmh.h b/frozen/bits/pmh.h index 2ca3fc6c..20fccae3 100644 --- a/frozen/bits/pmh.h +++ b/frozen/bits/pmh.h @@ -137,7 +137,7 @@ struct seed_or_index { private: static constexpr value_type MINUS_ONE = - std::numeric_limits::max(); + (std::numeric_limits::max)(); static constexpr value_type HIGH_BIT = ~(MINUS_ONE >> 1); value_type value_ = 0; @@ -199,7 +199,7 @@ pmh_tables constexpr make_pmh_tables(const carray &items, carray G; // Default constructed to "index 0" // H becomes the second hash table in the resulting pmh function - constexpr std::size_t UNUSED = std::numeric_limits::max(); + constexpr std::size_t UNUSED = (std::numeric_limits::max)(); carray H; H.fill(UNUSED); diff --git a/iguana/reflection.hpp b/iguana/reflection.hpp index a8f0dbb9..4a224eb4 100644 --- a/iguana/reflection.hpp +++ b/iguana/reflection.hpp @@ -17,12 +17,11 @@ #include #include -#include "frozen/string.h" -#include "frozen/unordered_map.h" - #include "detail/itoa.hpp" #include "detail/string_stream.hpp" #include "detail/traits.hpp" +#include "frozen/string.h" +#include "frozen/unordered_map.h" namespace iguana::detail { /******************************************/ @@ -560,9 +559,13 @@ namespace iguana::detail { } \ using size_type = \ std::integral_constant; \ - constexpr static std::string_view name() { \ + constexpr static std::string_view name() { return name_##STRUCT_NAME; } \ + constexpr static std::string_view struct_name() { \ return std::string_view(#STRUCT_NAME, sizeof(#STRUCT_NAME) - 1); \ } \ + constexpr static std::string_view fields() { \ + return fields_##STRUCT_NAME; \ + } \ constexpr static size_t value() { return size_type::value; } \ constexpr static std::array arr() { \ return arr_##STRUCT_NAME; \ @@ -571,9 +574,12 @@ namespace iguana::detail { return reflect_members{}; \ } -#define MAKE_META_DATA(STRUCT_NAME, N, ...) \ +#define MAKE_META_DATA(STRUCT_NAME, TABLE_NAME, N, ...) \ constexpr inline std::array arr_##STRUCT_NAME = { \ MARCO_EXPAND(MACRO_CONCAT(CON_STR, N)(__VA_ARGS__))}; \ + constexpr inline std::string_view fields_##STRUCT_NAME = { \ + MAKE_NAMES(__VA_ARGS__)}; \ + constexpr inline std::string_view name_##STRUCT_NAME = TABLE_NAME; \ MAKE_META_DATA_IMPL(STRUCT_NAME, \ MAKE_ARG_LIST(N, &STRUCT_NAME::FIELD, __VA_ARGS__)) @@ -623,6 +629,10 @@ get_iguana_struct_map_impl(const std::array &arr, } // namespace iguana::detail namespace iguana { +#define REFLECTION_WITH_NAME(STRUCT_NAME, TABLE_NAME, ...) \ + MAKE_META_DATA(STRUCT_NAME, TABLE_NAME, GET_ARG_COUNT(__VA_ARGS__), \ + __VA_ARGS__) + inline std::unordered_map< std::string_view, std::vector>> @@ -639,7 +649,8 @@ template inline constexpr auto get_iguana_struct_map() { } #define REFLECTION(STRUCT_NAME, ...) \ - MAKE_META_DATA(STRUCT_NAME, GET_ARG_COUNT(__VA_ARGS__), __VA_ARGS__) + MAKE_META_DATA(STRUCT_NAME, #STRUCT_NAME, GET_ARG_COUNT(__VA_ARGS__), \ + __VA_ARGS__) #define REFLECTION_EMPTY(STRUCT_NAME) MAKE_META_DATA_EMPTY(STRUCT_NAME) @@ -700,6 +711,12 @@ inline int add_custom_fields(std::string_view key, return 0; } +#ifdef _MSC_VER +#define IGUANA_UNIQUE_VARIABLE(str) MACRO_CONCAT(str, __COUNTER__) +#else +#define IGUANA_UNIQUE_VARIABLE(str) MACRO_CONCAT(str, __LINE__) +#endif + #define CUSTOM_FIELDS_IMPL(STRUCT_NAME, N, ...) \ inline auto IGUANA_UNIQUE_VARIABLE(STRUCT_NAME) = iguana::add_custom_fields( \ #STRUCT_NAME, {MARCO_EXPAND(MACRO_CONCAT(CON_STR, N)(__VA_ARGS__))}); @@ -798,6 +815,11 @@ template constexpr const std::string_view get_name() { return M::name(); } +template constexpr const std::string_view get_fields() { + using M = Reflect_members; + return M::fields(); +} + template constexpr std::enable_if_t::value, size_t> get_value() { using M = decltype(iguana_reflect_members(std::declval())); @@ -891,4 +913,4 @@ constexpr std::enable_if_t>::value> for_each(T &&t, std::make_index_sequence{}); } } // namespace iguana -#endif // IGUANA_REFLECTION_HPP \ No newline at end of file +#endif // IGUANA_REFLECTION_HPP From 3999329835c4d700b309037f14d1b5021e12d0e9 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 24 Jul 2023 17:34:03 +0800 Subject: [PATCH 2/4] update --- frozen/random.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frozen/random.h b/frozen/random.h index ffe8809f..139d7849 100644 --- a/frozen/random.h +++ b/frozen/random.h @@ -67,10 +67,10 @@ class linear_congruential_engine { state_ = modulo(tmp, std::integral_constant()); return state_; } - constexpr void discard(unsigned long long n) { - while (n--) - operator()(); - } + // constexpr void discard(unsigned long long n) { + // while (n--) + // operator()(); + // } static constexpr result_type min() { return increment == 0u ? 1u : 0u; } static constexpr result_type max() { return modulus - 1u; } friend constexpr bool operator==(linear_congruential_engine const &self, From e276cd44a34419cba3c595a48cce3b54a5cc6f51 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 24 Jul 2023 17:46:32 +0800 Subject: [PATCH 3/4] update --- frozen/random.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frozen/random.h b/frozen/random.h index 139d7849..81caa200 100644 --- a/frozen/random.h +++ b/frozen/random.h @@ -71,8 +71,8 @@ class linear_congruential_engine { // while (n--) // operator()(); // } - static constexpr result_type min() { return increment == 0u ? 1u : 0u; } - static constexpr result_type max() { return modulus - 1u; } + // static constexpr result_type min() { return increment == 0u ? 1u : 0u; } + // static constexpr result_type max() { return modulus - 1u; } friend constexpr bool operator==(linear_congruential_engine const &self, linear_congruential_engine const &other) { return self.state_ == other.state_; From 375dfacbf6734e46b6ea21be8c52a6672a09bb4d Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 24 Jul 2023 17:50:38 +0800 Subject: [PATCH 4/4] option SEQUENTIAL_PARSE --- CMakeLists.txt | 5 +++++ benchmark/json_benchmark.cpp | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c9dc967..182ac99d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,11 @@ message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}") add_definitions(-DTHROW_UNKNOWN_KEY) +option(ENABLE_SEQUENTIAL_PARSE "parse json sequential more efficient if the json fields sequences are the same with struct fields" OFF) +if (${ENABLE_SEQUENTIAL_PARSE}) + ADD_DEFINITIONS(-DSEQUENTIAL_PARSE) +endif () + option(HAS_RAPIDJSON "import rapidjson" OFF) if (${HAS_RAPIDJSON}) add_definitions(-DHAS_RAPIDJSON) diff --git a/benchmark/json_benchmark.cpp b/benchmark/json_benchmark.cpp index 4aeefc39..8aca099f 100644 --- a/benchmark/json_benchmark.cpp +++ b/benchmark/json_benchmark.cpp @@ -1,4 +1,3 @@ -#define SEQUENTIAL_PARSE #include "json_benchmark.h" class ScopedTimer {