Skip to content

Commit

Permalink
perf: 优化log打印模板的实现
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Oct 19, 2023
1 parent 0ffedaf commit 93ef576
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 175 deletions.
21 changes: 9 additions & 12 deletions 3rdparty/include/meojson/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2528,11 +2528,13 @@ MEOJSON_INLINE const basic_value<string_t> invalid_value()

namespace _serialization_helper
{
template <typename T>
template <typename char_t, typename T>
class has_output_operator
{
using ostringstream_t = std::basic_ostringstream<char_t, std::char_traits<char_t>, std::allocator<char_t>>;

template <typename U>
static auto test(int) -> decltype(std::declval<std::ostream&>() << std::declval<U>(), std::true_type());
static auto test(int) -> decltype(std::declval<ostringstream_t&>() << std::declval<U>(), std::true_type());

template <typename U>
static std::false_type test(...);
Expand Down Expand Up @@ -2566,17 +2568,12 @@ namespace _serialization_helper
template <typename input_t, typename string_t>
MEOJSON_INLINE string_t to_stream_string(input_t&& arg)
{
if constexpr (has_output_operator<input_t>::value) {
using char_t = typename string_t::value_type;
using stringstream_t = std::basic_stringstream<char_t, std::char_traits<char_t>, std::allocator<char_t>>;
using char_t = typename string_t::value_type;
using ostringstream_t = std::basic_ostringstream<char_t, std::char_traits<char_t>, std::allocator<char_t>>;

stringstream_t ss;
ss << std::forward<input_t>(arg);
return std::move(ss).str();
}
else {
static_assert(!sizeof(input_t), "Unable to convert type to string, there is no operator <<.");
}
ostringstream_t os;
os << std::forward<input_t>(arg);
return std::move(os).str();
}

template <bool loose, typename input_t, typename string_t>
Expand Down
16 changes: 1 addition & 15 deletions source/MaaFramework/Vision/ColorMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,10 @@ class ColorMatcher : public VisionBase
ColorMatcherParam param_;
};

MAA_VISION_NS_END

MAA_NS_BEGIN

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::ColorMatcher::Result& res)
{
os << res.to_json().to_string();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::ColorMatcher::ResultsVec& resutls)
{
json::array root;
for (const auto& res : resutls) {
root.emplace_back(res.to_json());
}
os << root.to_string();
return os;
}

MAA_NS_END
MAA_VISION_NS_END
16 changes: 1 addition & 15 deletions source/MaaFramework/Vision/FeatureMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,10 @@ class FeatureMatcher : public VisionBase
std::shared_ptr<cv::Mat> template_;
};

MAA_VISION_NS_END

MAA_NS_BEGIN

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::FeatureMatcher::Result& res)
{
os << res.to_json().to_string();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::FeatureMatcher::ResultsVec& resutls)
{
json::array root;
for (const auto& res : resutls) {
root.emplace_back(res.to_json());
}
os << root.to_string();
return os;
}

MAA_NS_END
MAA_VISION_NS_END
16 changes: 1 addition & 15 deletions source/MaaFramework/Vision/NeuralNetworkClassifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,10 @@ class NeuralNetworkClassifier : public VisionBase
std::shared_ptr<Ort::Session> session_ = nullptr;
};

MAA_VISION_NS_END

MAA_NS_BEGIN

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::NeuralNetworkClassifier::Result& res)
{
os << res.to_json().to_string();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::NeuralNetworkClassifier::ResultsVec& resutls)
{
json::array root;
for (const auto& res : resutls) {
root.emplace_back(res.to_json());
}
os << root.to_string();
return os;
}

MAA_NS_END
MAA_VISION_NS_END
6 changes: 1 addition & 5 deletions source/MaaFramework/Vision/NeuralNetworkDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ class NeuralNetworkDetector : public VisionBase
std::shared_ptr<Ort::Session> session_ = nullptr;
};

MAA_VISION_NS_END

MAA_NS_BEGIN

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::NeuralNetworkDetector::Result& res)
{
os << res.to_json().to_string();
return os;
}

MAA_NS_END
MAA_VISION_NS_END
7 changes: 0 additions & 7 deletions source/MaaFramework/Vision/OCRer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ json::value OCRer::Result::to_json() const
return root;
}

std::ostream& operator<<(std::ostream& os, const OCRer::Result& res)
{
auto u8str = from_u16(res.text);
os << VAR_RAW(u8str) << VAR_RAW(res.box) << VAR_RAW(res.score);
return os;
}

OCRer::ResultsVec OCRer::analyze() const
{
auto start_time = std::chrono::steady_clock::now();
Expand Down
16 changes: 1 addition & 15 deletions source/MaaFramework/Vision/OCRer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,10 @@ class OCRer : public VisionBase
std::shared_ptr<fastdeploy::pipeline::PPOCRv3> ocrer_ = nullptr;
};

MAA_VISION_NS_END

MAA_NS_BEGIN

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::OCRer::Result& res)
{
os << res.to_json().to_string();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::OCRer::ResultsVec& resutls)
{
json::array root;
for (const auto& res : resutls) {
root.emplace_back(res.to_json());
}
os << root.to_string();
return os;
}

MAA_NS_END
MAA_VISION_NS_END
16 changes: 1 addition & 15 deletions source/MaaFramework/Vision/TemplateComparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,10 @@ class TemplateComparator
TemplateComparatorParam param_;
};

MAA_VISION_NS_END

MAA_NS_BEGIN

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::TemplateComparator::Result& res)
{
os << res.to_json().to_string();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::TemplateComparator::ResultsVec& resutls)
{
json::array root;
for (const auto& res : resutls) {
root.emplace_back(res.to_json());
}
os << root.to_string();
return os;
}

MAA_NS_END
MAA_VISION_NS_END
16 changes: 1 addition & 15 deletions source/MaaFramework/Vision/TemplateMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,10 @@ class TemplateMatcher : public VisionBase
std::vector<std::shared_ptr<cv::Mat>> templates_;
};

MAA_VISION_NS_END

MAA_NS_BEGIN

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::TemplateMatcher::Result& res)
{
os << res.to_json().to_string();
return os;
}

inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::TemplateMatcher::ResultsVec& resutls)
{
json::array root;
for (const auto& res : resutls) {
root.emplace_back(res.to_json());
}
os << root.to_string();
return os;
}

MAA_NS_END
MAA_VISION_NS_END
6 changes: 1 addition & 5 deletions source/MaaFramework/Vision/VisionUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,10 @@ inline cv::Mat create_mask(const cv::Mat& image, const cv::Rect& roi)
return mask;
}

MAA_VISION_NS_END

MAA_NS_BEGIN

inline std::ostream& operator<<(std::ostream& os, const cv::Rect& rect)
{
os << "Rect(" << rect.x << ", " << rect.y << ", " << rect.width << ", " << rect.height << ")";
return os;
}

MAA_NS_END
MAA_VISION_NS_END
59 changes: 3 additions & 56 deletions source/include/Utils/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,7 @@ template <typename T>
std::ostream& operator<<(std::ostream& os, const std::optional<T>& v);

template <typename T>
concept has_stream_insertion_operator = requires { std::declval<std::ostream&>() << std::declval<T>(); };
template <typename T>
concept has_value_type = requires { typename std::decay_t<T>::value_type; };
template <typename T>
concept has_key_mapped_type = requires {
typename std::decay_t<T>::key_type;
typename std::decay_t<T>::mapped_type;
};
concept has_output_operator = requires { std::declval<std::ostream&>() << std::declval<T>(); };

class MAA_UTILS_API Logger
{
Expand Down Expand Up @@ -115,7 +108,7 @@ class MAA_UTILS_API Logger
else if constexpr (std::same_as<std::wstring, std::decay_t<T>>) {
return from_u16(std::forward<T>(value));
}
else if constexpr (has_stream_insertion_operator<T>) {
else if constexpr (has_output_operator<T>) {
return std::forward<T>(value);
}
else if constexpr (std::is_constructible_v<json::array, T>) {
Expand All @@ -124,54 +117,8 @@ class MAA_UTILS_API Logger
else if constexpr (std::is_constructible_v<json::object, T>) {
return json::object(std::forward<T>(value));
}
else if constexpr (MAA_RNS::ranges::input_range<T> && has_value_type<T> && !has_key_mapped_type<T>) {
using value_type = typename std::decay_t<T>::value_type;
using value_stream_type = decltype(to_stream(std::declval<value_type>()));

json::value jtmp;
for (auto&& val : value) {
if constexpr (std::is_constructible_v<json::value, value_stream_type>) {
jtmp.emplace(to_stream(std::forward<decltype(val)>(val)));
}
else {
std::stringstream val_ss;
val_ss << to_stream(std::forward<decltype(val)>(val));
jtmp.emplace(std::move(val_ss).str());
}
}
return jtmp;
}
else if constexpr (MAA_RNS::ranges::input_range<T> && has_value_type<T> && has_key_mapped_type<T>) {
using key_type = typename std::decay_t<T>::key_type;
using key_stream_type = decltype(to_stream(std::declval<key_type>()));
using mapped_type = typename std::decay_t<T>::mapped_type;
using mapped_stream_type = decltype(to_stream(std::declval<mapped_type>()));

json::value jtmp;
for (auto&& [key, val] : value) {
json::object::key_type strkey;
if constexpr (std::is_constructible_v<json::object::key_type, key_stream_type>) {
strkey = to_stream(key);
}
else {
std::stringstream key_ss;
key_ss << to_stream(key);
strkey = std::move(key_ss).str();
}

if constexpr (std::is_constructible_v<json::value, mapped_stream_type>) {
jtmp.emplace(std::move(strkey), to_stream(std::forward<decltype(val)>(val)));
}
else {
std::stringstream val_ss;
val_ss << to_stream(std::forward<decltype(val)>(val));
jtmp.emplace(std::move(strkey), std::move(val_ss).str());
}
}
return jtmp;
}
else {
static_assert(!sizeof(T), "Unsupported type");
return json::serialize<true>(std::forward<T>(value));
}
}

Expand Down

1 comment on commit 93ef576

@MistEO
Copy link
Member Author

@MistEO MistEO commented on 93ef576 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

优雅!永不过时!

Please sign in to comment.