Skip to content

Commit 93ef576

Browse files
committed
perf: 优化log打印模板的实现
1 parent 0ffedaf commit 93ef576

File tree

11 files changed

+20
-175
lines changed

11 files changed

+20
-175
lines changed

3rdparty/include/meojson/json.hpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -2528,11 +2528,13 @@ MEOJSON_INLINE const basic_value<string_t> invalid_value()
25282528

25292529
namespace _serialization_helper
25302530
{
2531-
template <typename T>
2531+
template <typename char_t, typename T>
25322532
class has_output_operator
25332533
{
2534+
using ostringstream_t = std::basic_ostringstream<char_t, std::char_traits<char_t>, std::allocator<char_t>>;
2535+
25342536
template <typename U>
2535-
static auto test(int) -> decltype(std::declval<std::ostream&>() << std::declval<U>(), std::true_type());
2537+
static auto test(int) -> decltype(std::declval<ostringstream_t&>() << std::declval<U>(), std::true_type());
25362538

25372539
template <typename U>
25382540
static std::false_type test(...);
@@ -2566,17 +2568,12 @@ namespace _serialization_helper
25662568
template <typename input_t, typename string_t>
25672569
MEOJSON_INLINE string_t to_stream_string(input_t&& arg)
25682570
{
2569-
if constexpr (has_output_operator<input_t>::value) {
2570-
using char_t = typename string_t::value_type;
2571-
using stringstream_t = std::basic_stringstream<char_t, std::char_traits<char_t>, std::allocator<char_t>>;
2571+
using char_t = typename string_t::value_type;
2572+
using ostringstream_t = std::basic_ostringstream<char_t, std::char_traits<char_t>, std::allocator<char_t>>;
25722573

2573-
stringstream_t ss;
2574-
ss << std::forward<input_t>(arg);
2575-
return std::move(ss).str();
2576-
}
2577-
else {
2578-
static_assert(!sizeof(input_t), "Unable to convert type to string, there is no operator <<.");
2579-
}
2574+
ostringstream_t os;
2575+
os << std::forward<input_t>(arg);
2576+
return std::move(os).str();
25802577
}
25812578

25822579
template <bool loose, typename input_t, typename string_t>

source/MaaFramework/Vision/ColorMatcher.h

+1-15
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,10 @@ class ColorMatcher : public VisionBase
3939
ColorMatcherParam param_;
4040
};
4141

42-
MAA_VISION_NS_END
43-
44-
MAA_NS_BEGIN
45-
4642
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::ColorMatcher::Result& res)
4743
{
4844
os << res.to_json().to_string();
4945
return os;
5046
}
5147

52-
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::ColorMatcher::ResultsVec& resutls)
53-
{
54-
json::array root;
55-
for (const auto& res : resutls) {
56-
root.emplace_back(res.to_json());
57-
}
58-
os << root.to_string();
59-
return os;
60-
}
61-
62-
MAA_NS_END
48+
MAA_VISION_NS_END

source/MaaFramework/Vision/FeatureMatcher.h

+1-15
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,10 @@ class FeatureMatcher : public VisionBase
6363
std::shared_ptr<cv::Mat> template_;
6464
};
6565

66-
MAA_VISION_NS_END
67-
68-
MAA_NS_BEGIN
69-
7066
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::FeatureMatcher::Result& res)
7167
{
7268
os << res.to_json().to_string();
7369
return os;
7470
}
7571

76-
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::FeatureMatcher::ResultsVec& resutls)
77-
{
78-
json::array root;
79-
for (const auto& res : resutls) {
80-
root.emplace_back(res.to_json());
81-
}
82-
os << root.to_string();
83-
return os;
84-
}
85-
86-
MAA_NS_END
72+
MAA_VISION_NS_END

source/MaaFramework/Vision/NeuralNetworkClassifier.h

+1-15
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,10 @@ class NeuralNetworkClassifier : public VisionBase
5252
std::shared_ptr<Ort::Session> session_ = nullptr;
5353
};
5454

55-
MAA_VISION_NS_END
56-
57-
MAA_NS_BEGIN
58-
5955
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::NeuralNetworkClassifier::Result& res)
6056
{
6157
os << res.to_json().to_string();
6258
return os;
6359
}
6460

65-
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::NeuralNetworkClassifier::ResultsVec& resutls)
66-
{
67-
json::array root;
68-
for (const auto& res : resutls) {
69-
root.emplace_back(res.to_json());
70-
}
71-
os << root.to_string();
72-
return os;
73-
}
74-
75-
MAA_NS_END
61+
MAA_VISION_NS_END

source/MaaFramework/Vision/NeuralNetworkDetector.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,10 @@ class NeuralNetworkDetector : public VisionBase
4848
std::shared_ptr<Ort::Session> session_ = nullptr;
4949
};
5050

51-
MAA_VISION_NS_END
52-
53-
MAA_NS_BEGIN
54-
5551
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::NeuralNetworkDetector::Result& res)
5652
{
5753
os << res.to_json().to_string();
5854
return os;
5955
}
6056

61-
MAA_NS_END
57+
MAA_VISION_NS_END

source/MaaFramework/Vision/OCRer.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ json::value OCRer::Result::to_json() const
1919
return root;
2020
}
2121

22-
std::ostream& operator<<(std::ostream& os, const OCRer::Result& res)
23-
{
24-
auto u8str = from_u16(res.text);
25-
os << VAR_RAW(u8str) << VAR_RAW(res.box) << VAR_RAW(res.score);
26-
return os;
27-
}
28-
2922
OCRer::ResultsVec OCRer::analyze() const
3023
{
3124
auto start_time = std::chrono::steady_clock::now();

source/MaaFramework/Vision/OCRer.h

+1-15
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,10 @@ class OCRer : public VisionBase
5959
std::shared_ptr<fastdeploy::pipeline::PPOCRv3> ocrer_ = nullptr;
6060
};
6161

62-
MAA_VISION_NS_END
63-
64-
MAA_NS_BEGIN
65-
6662
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::OCRer::Result& res)
6763
{
6864
os << res.to_json().to_string();
6965
return os;
7066
}
7167

72-
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::OCRer::ResultsVec& resutls)
73-
{
74-
json::array root;
75-
for (const auto& res : resutls) {
76-
root.emplace_back(res.to_json());
77-
}
78-
os << root.to_string();
79-
return os;
80-
}
81-
82-
MAA_NS_END
68+
MAA_VISION_NS_END

source/MaaFramework/Vision/TemplateComparator.h

+1-15
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,10 @@ class TemplateComparator
3737
TemplateComparatorParam param_;
3838
};
3939

40-
MAA_VISION_NS_END
41-
42-
MAA_NS_BEGIN
43-
4440
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::TemplateComparator::Result& res)
4541
{
4642
os << res.to_json().to_string();
4743
return os;
4844
}
4945

50-
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::TemplateComparator::ResultsVec& resutls)
51-
{
52-
json::array root;
53-
for (const auto& res : resutls) {
54-
root.emplace_back(res.to_json());
55-
}
56-
os << root.to_string();
57-
return os;
58-
}
59-
60-
MAA_NS_END
46+
MAA_VISION_NS_END

source/MaaFramework/Vision/TemplateMatcher.h

+1-15
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,10 @@ class TemplateMatcher : public VisionBase
4242
std::vector<std::shared_ptr<cv::Mat>> templates_;
4343
};
4444

45-
MAA_VISION_NS_END
46-
47-
MAA_NS_BEGIN
48-
4945
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::TemplateMatcher::Result& res)
5046
{
5147
os << res.to_json().to_string();
5248
return os;
5349
}
5450

55-
inline std::ostream& operator<<(std::ostream& os, const MAA_VISION_NS::TemplateMatcher::ResultsVec& resutls)
56-
{
57-
json::array root;
58-
for (const auto& res : resutls) {
59-
root.emplace_back(res.to_json());
60-
}
61-
os << root.to_string();
62-
return os;
63-
}
64-
65-
MAA_NS_END
51+
MAA_VISION_NS_END

source/MaaFramework/Vision/VisionUtils.hpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,10 @@ inline cv::Mat create_mask(const cv::Mat& image, const cv::Rect& roi)
192192
return mask;
193193
}
194194

195-
MAA_VISION_NS_END
196-
197-
MAA_NS_BEGIN
198-
199195
inline std::ostream& operator<<(std::ostream& os, const cv::Rect& rect)
200196
{
201197
os << "Rect(" << rect.x << ", " << rect.y << ", " << rect.width << ", " << rect.height << ")";
202198
return os;
203199
}
204200

205-
MAA_NS_END
201+
MAA_VISION_NS_END

source/include/Utils/Logger.h

+3-56
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,7 @@ template <typename T>
3333
std::ostream& operator<<(std::ostream& os, const std::optional<T>& v);
3434

3535
template <typename T>
36-
concept has_stream_insertion_operator = requires { std::declval<std::ostream&>() << std::declval<T>(); };
37-
template <typename T>
38-
concept has_value_type = requires { typename std::decay_t<T>::value_type; };
39-
template <typename T>
40-
concept has_key_mapped_type = requires {
41-
typename std::decay_t<T>::key_type;
42-
typename std::decay_t<T>::mapped_type;
43-
};
36+
concept has_output_operator = requires { std::declval<std::ostream&>() << std::declval<T>(); };
4437

4538
class MAA_UTILS_API Logger
4639
{
@@ -115,7 +108,7 @@ class MAA_UTILS_API Logger
115108
else if constexpr (std::same_as<std::wstring, std::decay_t<T>>) {
116109
return from_u16(std::forward<T>(value));
117110
}
118-
else if constexpr (has_stream_insertion_operator<T>) {
111+
else if constexpr (has_output_operator<T>) {
119112
return std::forward<T>(value);
120113
}
121114
else if constexpr (std::is_constructible_v<json::array, T>) {
@@ -124,54 +117,8 @@ class MAA_UTILS_API Logger
124117
else if constexpr (std::is_constructible_v<json::object, T>) {
125118
return json::object(std::forward<T>(value));
126119
}
127-
else if constexpr (MAA_RNS::ranges::input_range<T> && has_value_type<T> && !has_key_mapped_type<T>) {
128-
using value_type = typename std::decay_t<T>::value_type;
129-
using value_stream_type = decltype(to_stream(std::declval<value_type>()));
130-
131-
json::value jtmp;
132-
for (auto&& val : value) {
133-
if constexpr (std::is_constructible_v<json::value, value_stream_type>) {
134-
jtmp.emplace(to_stream(std::forward<decltype(val)>(val)));
135-
}
136-
else {
137-
std::stringstream val_ss;
138-
val_ss << to_stream(std::forward<decltype(val)>(val));
139-
jtmp.emplace(std::move(val_ss).str());
140-
}
141-
}
142-
return jtmp;
143-
}
144-
else if constexpr (MAA_RNS::ranges::input_range<T> && has_value_type<T> && has_key_mapped_type<T>) {
145-
using key_type = typename std::decay_t<T>::key_type;
146-
using key_stream_type = decltype(to_stream(std::declval<key_type>()));
147-
using mapped_type = typename std::decay_t<T>::mapped_type;
148-
using mapped_stream_type = decltype(to_stream(std::declval<mapped_type>()));
149-
150-
json::value jtmp;
151-
for (auto&& [key, val] : value) {
152-
json::object::key_type strkey;
153-
if constexpr (std::is_constructible_v<json::object::key_type, key_stream_type>) {
154-
strkey = to_stream(key);
155-
}
156-
else {
157-
std::stringstream key_ss;
158-
key_ss << to_stream(key);
159-
strkey = std::move(key_ss).str();
160-
}
161-
162-
if constexpr (std::is_constructible_v<json::value, mapped_stream_type>) {
163-
jtmp.emplace(std::move(strkey), to_stream(std::forward<decltype(val)>(val)));
164-
}
165-
else {
166-
std::stringstream val_ss;
167-
val_ss << to_stream(std::forward<decltype(val)>(val));
168-
jtmp.emplace(std::move(strkey), std::move(val_ss).str());
169-
}
170-
}
171-
return jtmp;
172-
}
173120
else {
174-
static_assert(!sizeof(T), "Unsupported type");
121+
return json::serialize<true>(std::forward<T>(value));
175122
}
176123
}
177124

0 commit comments

Comments
 (0)