Skip to content

Commit cfc7af2

Browse files
authored
[Benchmark] avoid Result mem copy for cpp benchmark (#1203)
* avoid mem copy for cpp benchmark * set CMAKE_BUILD_TYPE to Release
1 parent 870551f commit cfc7af2

File tree

3 files changed

+115
-58
lines changed

3 files changed

+115
-58
lines changed

CMakeLists.txt

+15-13
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ include(${PROJECT_SOURCE_DIR}/cmake/utils.cmake)
3737
# Set C++11 as standard for the whole project
3838
if(NOT MSVC)
3939
set(CMAKE_CXX_STANDARD 11)
40-
set(CMAKE_CXX_FLAGS "-Wno-format")
40+
set(CMAKE_CXX_FLAGS "-Wno-format -g0 -O3")
4141
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)
4242
endif(NOT MSVC)
4343

@@ -153,6 +153,8 @@ get_osx_architecture()
153153

154154
##################################### Building: FastDeploy C++ SDK #######################################
155155
add_definitions(-DFASTDEPLOY_LIB)
156+
# set CMAKE_BUILD_TYPE to Release
157+
add_definitions(-DCMAKE_BUILD_TYPE=Release)
156158
# configure files before glob sources.
157159
configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h)
158160
configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/pybind/main.cc.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/pybind/main.cc)
@@ -466,7 +468,7 @@ if(ANDROID)
466468
list(APPEND DEPEND_LIBS ${log-lib})
467469
if(WITH_LITE_STATIC)
468470
# need omp for static Paddle Lite lib
469-
set(WITH_OPENMP ON CACHE BOOL "Force WITH_OPENMP=ON while WITH_LITE_STATIC=ON" FORCE)
471+
set(WITH_OPENMP ON CACHE BOOL "Force WITH_OPENMP=ON while WITH_LITE_STATIC=ON" FORCE)
470472
message(STATUS "Force WITH_OPENMP=${WITH_OPENMP} while WITH_LITE_STATIC=ON")
471473
endif()
472474
if(WITH_OPENMP)
@@ -482,13 +484,13 @@ if(ANDROID AND WITH_JAVA)
482484
endif()
483485

484486
if(ANDROID AND WITH_STATIC_LIB)
485-
# Here, we use a dummy target (fastdelpoy_dummy)
487+
# Here, we use a dummy target (fastdelpoy_dummy)
486488
# to form a build dependency tree for fastdeploy_static lib.
487489
add_library(fastdelpoy_dummy STATIC ${ALL_DEPLOY_SRCS})
488-
# Still add ${DEPEND_LIBS} for cmake to form link_libraries
489-
# property tree for a static library.
490+
# Still add ${DEPEND_LIBS} for cmake to form link_libraries
491+
# property tree for a static library.
490492
target_link_libraries(fastdelpoy_dummy ${DEPEND_LIBS})
491-
# Build fastdelpoy_dummy when the third-party
493+
# Build fastdelpoy_dummy when the third-party
492494
# libraries (opencv, paddle lite, flycv) are ready.
493495
add_dependencies(fastdelpoy_dummy ${LIBRARY_NAME})
494496
# Add WITH_STATIC_LIB compile definitions, see lite_backend.cc.
@@ -541,9 +543,9 @@ if(WIN32)
541543
RUNTIME DESTINATION lib
542544
)
543545
elseif(ANDROID)
544-
if(WITH_STATIC_LIB)
546+
if(WITH_STATIC_LIB)
545547
install(
546-
FILES
548+
FILES
547549
${CMAKE_CURRENT_BINARY_DIR}/libfastdeploy_static.a
548550
DESTINATION lib/${ANDROID_ABI}
549551
)
@@ -553,11 +555,11 @@ elseif(ANDROID)
553555
LIBRARY DESTINATION lib/${ANDROID_ABI}
554556
)
555557
endif()
556-
# Install omp into fastdeploy lib dir if WITH_OPENMP=ON
558+
# Install omp into fastdeploy lib dir if WITH_OPENMP=ON
557559
# and WITH_LITE_STATIC=OFF.
558560
if(WITH_OPENMP AND (NOT WITH_LITE_STATIC) AND OpenMP_CXX_FOUND AND ENABLE_OPENMP_SHARED)
559561
install(
560-
FILES
562+
FILES
561563
${OpenMP_CXX_LIBRARIES}
562564
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${ANDROID_ABI}
563565
)
@@ -594,7 +596,7 @@ else()
594596
# The headers and libs of opencv must be install.
595597
if(ENABLE_VISION)
596598
if(WITH_OPENCV_STATIC AND WITH_STATIC_LIB)
597-
# Only need to install headers while building
599+
# Only need to install headers while building
598600
# FastDeploy static lib. (TODO:qiuyanjun)
599601
install(
600602
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/opencv/sdk/native/jni/include
@@ -621,14 +623,14 @@ else()
621623
)
622624
endif()
623625
endif()
624-
# fast_tokenizer's static lib is not avaliable now!
626+
# fast_tokenizer's static lib is not avaliable now!
625627
# may support some days later(TODO:qiuyanjun)
626628
if(ENABLE_TEXT)
627629
install(
628630
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/fast_tokenizer
629631
DESTINATION ${CMAKE_INSTALL_PREFIX}/third_libs/install
630632
)
631-
endif()
633+
endif()
632634
# some libs may not to install while in static mode
633635
if(ENABLE_LITE_BACKEND)
634636
if(WITH_LITE_STATIC)

fastdeploy/vision/common/result.cc

+68-37
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
namespace fastdeploy {
1717
namespace vision {
1818

19-
void ClassifyResult::Clear() {
19+
void ClassifyResult::Free() {
2020
std::vector<int32_t>().swap(label_ids);
2121
std::vector<float>().swap(scores);
2222
}
2323

24+
void ClassifyResult::Clear() {
25+
label_ids.clear();
26+
scores.clear();
27+
}
28+
2429
std::string ClassifyResult::Str() {
2530
std::string out;
2631
out = "ClassifyResult(\nlabel_ids: ";
@@ -47,11 +52,16 @@ void Mask::Reserve(int size) { data.reserve(size); }
4752

4853
void Mask::Resize(int size) { data.resize(size); }
4954

50-
void Mask::Clear() {
55+
void Mask::Free() {
5156
std::vector<uint8_t>().swap(data);
5257
std::vector<int64_t>().swap(shape);
5358
}
5459

60+
void Mask::Clear() {
61+
data.clear();
62+
shape.clear();
63+
}
64+
5565
std::string Mask::Str() {
5666
std::string out = "Mask(";
5767
size_t ndim = shape.size();
@@ -94,26 +104,38 @@ DetectionResult& DetectionResult::operator=(DetectionResult&& other) {
94104
return *this;
95105
}
96106

97-
void DetectionResult::Clear() {
107+
void DetectionResult::Free() {
98108
std::vector<std::array<float, 4>>().swap(boxes);
99109
std::vector<float>().swap(scores);
100110
std::vector<int32_t>().swap(label_ids);
101111
std::vector<Mask>().swap(masks);
102112
contain_masks = false;
103113
}
104114

115+
void DetectionResult::Clear() {
116+
boxes.clear();
117+
scores.clear();
118+
label_ids.clear();
119+
masks.clear();
120+
contain_masks = false;
121+
}
122+
105123
void DetectionResult::Reserve(int size) {
106124
boxes.reserve(size);
107125
scores.reserve(size);
108126
label_ids.reserve(size);
109-
masks.reserve(size);
127+
if (contain_masks) {
128+
masks.reserve(size);
129+
}
110130
}
111131

112132
void DetectionResult::Resize(int size) {
113133
boxes.resize(size);
114134
scores.resize(size);
115135
label_ids.resize(size);
116-
masks.resize(size);
136+
if (contain_masks) {
137+
masks.resize(size);
138+
}
117139
}
118140

119141
std::string DetectionResult::Str() {
@@ -139,12 +161,18 @@ std::string DetectionResult::Str() {
139161
return out;
140162
}
141163

142-
void KeyPointDetectionResult::Clear() {
164+
void KeyPointDetectionResult::Free() {
143165
std::vector<std::array<float, 2>>().swap(keypoints);
144166
std::vector<float>().swap(scores);
145167
num_joints = -1;
146168
}
147169

170+
void KeyPointDetectionResult::Clear() {
171+
keypoints.clear();
172+
scores.clear();
173+
num_joints = -1;
174+
}
175+
148176
void KeyPointDetectionResult::Reserve(int size) { keypoints.reserve(size); }
149177

150178
void KeyPointDetectionResult::Resize(int size) { keypoints.resize(size); }
@@ -155,8 +183,8 @@ std::string KeyPointDetectionResult::Str() {
155183
out = "KeyPointDetectionResult: [x, y, conf]\n";
156184
for (size_t i = 0; i < keypoints.size(); ++i) {
157185
out = out + std::to_string(keypoints[i][0]) + "," +
158-
std::to_string(keypoints[i][1]) + ", " +
159-
std::to_string(scores[i]) + "\n";
186+
std::to_string(keypoints[i][1]) + ", " + std::to_string(scores[i]) +
187+
"\n";
160188
}
161189
out += "num_joints:" + std::to_string(num_joints) + "\n";
162190
return out;
@@ -170,22 +198,22 @@ void OCRResult::Clear() {
170198
cls_labels.clear();
171199
}
172200

173-
void MOTResult::Clear(){
201+
void MOTResult::Clear() {
174202
boxes.clear();
175203
ids.clear();
176204
scores.clear();
177205
class_ids.clear();
178206
}
179207

180-
std::string MOTResult::Str(){
208+
std::string MOTResult::Str() {
181209
std::string out;
182-
out = "MOTResult:\nall boxes counts: "+std::to_string(boxes.size())+"\n";
210+
out = "MOTResult:\nall boxes counts: " + std::to_string(boxes.size()) + "\n";
183211
out += "[xmin\tymin\txmax\tymax\tid\tscore]\n";
184212
for (size_t i = 0; i < boxes.size(); ++i) {
185-
out = out + "["+ std::to_string(boxes[i][0]) + "\t" +
213+
out = out + "[" + std::to_string(boxes[i][0]) + "\t" +
186214
std::to_string(boxes[i][1]) + "\t" + std::to_string(boxes[i][2]) +
187-
"\t" + std::to_string(boxes[i][3]) + "\t" +
188-
std::to_string(ids[i]) + "\t" + std::to_string(scores[i]) + "]\n";
215+
"\t" + std::to_string(boxes[i][3]) + "\t" + std::to_string(ids[i]) +
216+
"\t" + std::to_string(scores[i]) + "]\n";
189217
}
190218
return out;
191219
}
@@ -197,13 +225,20 @@ FaceDetectionResult::FaceDetectionResult(const FaceDetectionResult& res) {
197225
landmarks_per_face = res.landmarks_per_face;
198226
}
199227

200-
void FaceDetectionResult::Clear() {
228+
void FaceDetectionResult::Free() {
201229
std::vector<std::array<float, 4>>().swap(boxes);
202230
std::vector<float>().swap(scores);
203231
std::vector<std::array<float, 2>>().swap(landmarks);
204232
landmarks_per_face = 0;
205233
}
206234

235+
void FaceDetectionResult::Clear() {
236+
boxes.clear();
237+
scores.clear();
238+
landmarks.clear();
239+
landmarks_per_face = 0;
240+
}
241+
207242
void FaceDetectionResult::Reserve(int size) {
208243
boxes.reserve(size);
209244
scores.reserve(size);
@@ -257,23 +292,22 @@ std::string FaceDetectionResult::Str() {
257292
return out;
258293
}
259294

260-
void FaceAlignmentResult::Clear() {
295+
void FaceAlignmentResult::Free() {
261296
std::vector<std::array<float, 2>>().swap(landmarks);
262297
}
263298

264-
void FaceAlignmentResult::Reserve(int size) {
265-
landmarks.resize(size);
266-
}
299+
void FaceAlignmentResult::Clear() { landmarks.clear(); }
267300

268-
void FaceAlignmentResult::Resize(int size) {
269-
landmarks.resize(size);
270-
}
301+
void FaceAlignmentResult::Reserve(int size) { landmarks.resize(size); }
302+
303+
void FaceAlignmentResult::Resize(int size) { landmarks.resize(size); }
271304

272305
std::string FaceAlignmentResult::Str() {
273306
std::string out;
274307

275308
out = "FaceAlignmentResult: [x, y]\n";
276-
out = out + "There are " +std::to_string(landmarks.size()) + " landmarks, the top 10 are listed as below:\n";
309+
out = out + "There are " + std::to_string(landmarks.size()) +
310+
" landmarks, the top 10 are listed as below:\n";
277311
int landmarks_size = landmarks.size();
278312
size_t result_length = std::min(10, landmarks_size);
279313
for (size_t i = 0; i < result_length; ++i) {
@@ -355,7 +389,9 @@ FaceRecognitionResult::FaceRecognitionResult(const FaceRecognitionResult& res) {
355389
embedding.assign(res.embedding.begin(), res.embedding.end());
356390
}
357391

358-
void FaceRecognitionResult::Clear() { std::vector<float>().swap(embedding); }
392+
void FaceRecognitionResult::Free() { std::vector<float>().swap(embedding); }
393+
394+
void FaceRecognitionResult::Clear() { embedding.clear(); }
359395

360396
void FaceRecognitionResult::Reserve(int size) { embedding.reserve(size); }
361397

@@ -536,28 +572,23 @@ std::string OCRResult::Str() {
536572
return no_result;
537573
}
538574

539-
void HeadPoseResult::Clear() {
540-
std::vector<float>().swap(euler_angles);
541-
}
575+
void HeadPoseResult::Free() { std::vector<float>().swap(euler_angles); }
542576

543-
void HeadPoseResult::Reserve(int size) {
544-
euler_angles.resize(size);
545-
}
577+
void HeadPoseResult::Clear() { euler_angles.clear(); }
546578

547-
void HeadPoseResult::Resize(int size) {
548-
euler_angles.resize(size);
549-
}
579+
void HeadPoseResult::Reserve(int size) { euler_angles.resize(size); }
580+
581+
void HeadPoseResult::Resize(int size) { euler_angles.resize(size); }
550582

551583
std::string HeadPoseResult::Str() {
552584
std::string out;
553585

554586
out = "HeadPoseResult: [yaw, pitch, roll]\n";
555-
out = out + "yaw: " + std::to_string(euler_angles[0]) + "\n" +
556-
"pitch: " + std::to_string(euler_angles[1]) + "\n" +
557-
"roll: " + std::to_string(euler_angles[2]) + "\n";
587+
out = out + "yaw: " + std::to_string(euler_angles[0]) + "\n" + "pitch: " +
588+
std::to_string(euler_angles[1]) + "\n" + "roll: " +
589+
std::to_string(euler_angles[2]) + "\n";
558590
return out;
559591
}
560592

561-
562593
} // namespace vision
563594
} // namespace fastdeploy

0 commit comments

Comments
 (0)