Skip to content

Commit

Permalink
optimize struct_json performance
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jul 26, 2023
1 parent a3ad67e commit cd9eaed
Show file tree
Hide file tree
Showing 12 changed files with 709 additions and 558 deletions.
14 changes: 8 additions & 6 deletions include/ylt/struct_json/json_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ IGUANA_INLINE void from_json(T &value, It &&it, It &&end,
iguana::from_json(value, it, end, ec);
}

template <typename T, iguana::json_view View>
template <typename T, typename View>
IGUANA_INLINE void from_json(T &value, const View &view) {
iguana::from_json(value, view);
}

template <typename T, iguana::json_view View>
template <typename T, typename View>
IGUANA_INLINE void from_json(T &value, const View &view,
std::error_code &ec) noexcept {
iguana::from_json(value, view, ec);
}

template <typename T, iguana::json_byte Byte>
template <typename T, typename Byte>
IGUANA_INLINE void from_json(T &value, const Byte *data, size_t size) {
iguana::from_json(value, data, size);
}

template <typename T, iguana::json_byte Byte>
template <typename T, typename Byte>
IGUANA_INLINE void from_json(T &value, const Byte *data, size_t size,
std::error_code &ec) noexcept {
iguana::from_json(value, data, size, ec);
Expand All @@ -61,6 +61,8 @@ IGUANA_INLINE void from_json_file(T &value, const std::string &filename,
iguana::from_json_file(value, filename, ec);
}

using numeric_str = iguana::numeric_str;

// dom parse
using jvalue = iguana::jvalue;
using jarray = iguana::jarray;
Expand All @@ -76,12 +78,12 @@ inline void parse(jvalue &result, It &&it, It &&end, std::error_code &ec) {
iguana::parse(result, it, end, ec);
}

template <typename T, iguana::json_view View>
template <typename T, typename View>
inline void parse(T &result, const View &view) {
iguana::parse(result, view);
}

template <typename T, iguana::json_view View>
template <typename T, typename View>
inline void parse(T &result, const View &view, std::error_code &ec) {
iguana::parse(result, view, ec);
}
Expand Down
89 changes: 89 additions & 0 deletions include/ylt/thirdparty/iguana/define.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#pragma once

#include <array>
#include <bit>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>

#if defined __clang__
#define IGUANA_INLINE __attribute__((always_inline)) inline
#define IGUANA__INLINE_LAMBDA __attribute__((always_inline)) constexpr
Expand All @@ -10,3 +17,85 @@
#define IGUANA_INLINE __attribute__((always_inline)) inline
#define IGUANA__INLINE_LAMBDA constexpr __attribute__((always_inline))
#endif

#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely)
#define IGUANA_LIKELY [[likely]]
#define IGUANA_UNLIKELY [[unlikely]]
#else
#define IGUANA_LIKELY
#define IGUANA_UNLIKELY
#endif

#ifdef _MSC_VER

#if _MSC_VER >= 1920 && _MSVC_LANG >= 202002L
IGUANA_INLINE int countr_zero(unsigned long long x) {
return std::countr_zero(x);
}
template <typename T>
constexpr inline bool contiguous_iterator = std::contiguous_iterator<T>;

#else
IGUANA_INLINE int countr_zero(unsigned long long x) {
// x will never be zero in iguana
unsigned long pos;
_BitScanForward64(&pos, x);
return pos;
}

namespace std {
template <typename T>
using remove_cvref_t =
typename remove_cv<typename remove_reference<T>::type>::type;
}
template <typename T>
constexpr inline bool contiguous_iterator =
std::is_same_v<std::remove_cvref_t<T>,
typename std::vector<char>::iterator> ||
std::is_same_v<std::remove_cvref_t<T>,
typename std::vector<char>::const_iterator> ||
std::is_same_v<std::remove_cvref_t<T>, typename std::string::iterator> ||
std::is_same_v<std::remove_cvref_t<T>,
typename std::string::const_iterator> ||
std::is_same_v<std::remove_cvref_t<T>, char *> ||
std::is_same_v<std::remove_cvref_t<T>,
typename std::string_view::iterator> ||
std::is_same_v<std::remove_cvref_t<T>,
typename std::string_view::const_iterator>;
#endif

#else

#if __cplusplus >= 202002L
IGUANA_INLINE int countr_zero(unsigned long long x) {
return std::countr_zero(x);
}
template <typename T>
constexpr inline bool contiguous_iterator = std::contiguous_iterator<T>;
#else
IGUANA_INLINE int countr_zero(unsigned long long x) {
// x will never be zero in iguana
return __builtin_ctzll(x);
}
namespace std {
template <typename T>
using remove_cvref_t =
typename remove_cv<typename remove_reference<T>::type>::type;
}
template <typename T>
constexpr inline bool contiguous_iterator =
std::is_same_v<std::remove_cvref_t<T>,
typename std::vector<char>::iterator> ||
std::is_same_v<std::remove_cvref_t<T>,
typename std::vector<char>::const_iterator> ||
std::is_same_v<std::remove_cvref_t<T>, typename std::string::iterator> ||
std::is_same_v<std::remove_cvref_t<T>,
typename std::string::const_iterator> ||
std::is_same_v<std::remove_cvref_t<T>, char *> ||
std::is_same_v<std::remove_cvref_t<T>,
typename std::string_view::iterator> ||
std::is_same_v<std::remove_cvref_t<T>,
typename std::string_view::const_iterator>;
#endif

#endif
4 changes: 4 additions & 0 deletions include/ylt/thirdparty/iguana/detail/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
#include <type_traits>
#include <unordered_map>
#include <vector>

#include "iguana/define.h"

namespace iguana {

template <class T>
struct is_signed_intergral_like
: std::integral_constant<bool, (std::is_integral<T>::value) &&
Expand Down
36 changes: 17 additions & 19 deletions include/ylt/thirdparty/iguana/error_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace iguana {

class iguana_category : public std::error_category {
public:
public:
virtual const char *name() const noexcept override {
return "iguana::category";
}
Expand All @@ -24,8 +24,7 @@ class iguana_category : public std::error_category {
int add_message(const std::string &msg) {
if (auto it = err_map_.find(msg); it != err_map_.end()) {
return it->second;
}
else {
} else {
err_++;
err_map_.emplace(msg, err_);
return err_;
Expand Down Expand Up @@ -55,27 +54,26 @@ enum class dom_errc {
};

class iguana_dom_category : public std::error_category {
public:
public:
virtual const char *name() const noexcept override {
return "iguana::dom_category";
}
virtual std::string message(int err_val) const override {
switch (static_cast<dom_errc>(err_val)) {
case dom_errc::ok:
return "ok";
case dom_errc::wrong_type: {
auto it = detail_msg_map_.find(dom_errc::wrong_type);
if (it != detail_msg_map_.end()) {
return std::string("wrong type, ")
.append("real type is ")
.append(it->second);
}
else {
return "wrong type";
}
case dom_errc::ok:
return "ok";
case dom_errc::wrong_type: {
auto it = detail_msg_map_.find(dom_errc::wrong_type);
if (it != detail_msg_map_.end()) {
return std::string("wrong type, ")
.append("real type is ")
.append(it->second);
} else {
return "wrong type";
}
default:
return "(unrecognized error)";
}
default:
return "(unrecognized error)";
}
}

Expand All @@ -97,4 +95,4 @@ inline std::error_code make_error_code(iguana::dom_errc err,

return std::error_code((int)err, instance);
}
} // namespace iguana
} // namespace iguana
Loading

0 comments on commit cd9eaed

Please sign in to comment.