From fb5bbde27741a5dd66a0828b8e61fdb5626ab710 Mon Sep 17 00:00:00 2001 From: antao Date: Mon, 24 Dec 2018 15:06:05 +0800 Subject: [PATCH] Modify something to improve performance --- lib/inc/drogon/HttpResponse.h | 2 +- lib/src/FileUpload.cc | 3 +-- lib/src/HttpResponseImpl.h | 2 +- lib/src/HttpServerContext.cc | 4 ++-- lib/src/ssl_funcs/Md5.cc | 5 ++--- lib/src/ssl_funcs/Md5.h | 22 ++++++++++------------ lib/tests/CMakeLists.txt | 2 +- lib/tests/Md5Test.cc | 6 ++++++ orm_lib/src/DbClientImpl.cc | 2 +- orm_lib/src/DbClientImpl.h | 2 +- orm_lib/src/TransactionImpl.cc | 4 ++-- orm_lib/src/mysql_impl/MysqlConnection.cc | 8 ++++---- trantor | 2 +- 13 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 lib/tests/Md5Test.cc diff --git a/lib/inc/drogon/HttpResponse.h b/lib/inc/drogon/HttpResponse.h index 561047ec..1d0bfee2 100755 --- a/lib/inc/drogon/HttpResponse.h +++ b/lib/inc/drogon/HttpResponse.h @@ -118,7 +118,7 @@ class HttpResponse virtual void setContentTypeCode(uint8_t type) = 0; - virtual void setContentTypeCodeAndCharacterSet(uint8_t type, const std::string charSet = "utf-8") = 0; + virtual void setContentTypeCodeAndCharacterSet(uint8_t type, const std::string &charSet = "utf-8") = 0; virtual uint8_t getContentTypeCode() = 0; diff --git a/lib/src/FileUpload.cc b/lib/src/FileUpload.cc index cf0bca03..f6831b38 100755 --- a/lib/src/FileUpload.cc +++ b/lib/src/FileUpload.cc @@ -221,7 +221,6 @@ const std::string HttpFile::getMd5() const MD5_Final(md5, &c); return binaryStringToHex(md5, 16); #else - Md5Encode encode; - return encode.Encode(_fileContent); + return Md5Encode::encode(_fileContent); #endif } diff --git a/lib/src/HttpResponseImpl.h b/lib/src/HttpResponseImpl.h index 14b8e22f..c641e7a6 100755 --- a/lib/src/HttpResponseImpl.h +++ b/lib/src/HttpResponseImpl.h @@ -76,7 +76,7 @@ class HttpResponseImpl : public HttpResponse setContentType(web_content_type_to_string(type)); } - virtual void setContentTypeCodeAndCharacterSet(uint8_t type, const std::string charSet = "utf-8") override + virtual void setContentTypeCodeAndCharacterSet(uint8_t type, const std::string &charSet = "utf-8") override { _contentType = type; setContentType(web_content_type_and_charset_to_string(type, charSet)); diff --git a/lib/src/HttpServerContext.cc b/lib/src/HttpServerContext.cc index 465b23aa..fca87569 100755 --- a/lib/src/HttpServerContext.cc +++ b/lib/src/HttpServerContext.cc @@ -208,7 +208,7 @@ void HttpServerContext::pushRquestToPipeLine(const HttpRequestPtr &req) } HttpRequestPtr HttpServerContext::getFirstRequest() const { - if (_requestPipeLine.size() > 0) + if (!_requestPipeLine.empty()) { return _requestPipeLine.front().first; } @@ -216,7 +216,7 @@ HttpRequestPtr HttpServerContext::getFirstRequest() const } HttpResponsePtr HttpServerContext::getFirstResponse() const { - if (_requestPipeLine.size() > 0) + if (!_requestPipeLine.empty()) { return _requestPipeLine.front().second; } diff --git a/lib/src/ssl_funcs/Md5.cc b/lib/src/ssl_funcs/Md5.cc index e0ec7329..17fd7fd6 100644 --- a/lib/src/ssl_funcs/Md5.cc +++ b/lib/src/ssl_funcs/Md5.cc @@ -231,7 +231,7 @@ std::string Md5Encode::GetHexStr(unsigned int num_str) // function: Encode // @param src_info:要加密的信息 // return :加密后的MD5值 -std::string Md5Encode::Encode(std::string src_info) +std::string Md5Encode::encode(const std::string &src_info) { ParamDynamic param; param.ua_ = kA; @@ -244,9 +244,8 @@ std::string Md5Encode::Encode(std::string src_info) param.vd_last_ = kD; std::string result; - const char *src_data = src_info.c_str(); char *out_data_ptr = NULL; - int total_byte = FillData(src_data, strlen(src_data), &out_data_ptr); + int total_byte = FillData(src_info.c_str(), src_info.length(), &out_data_ptr); //char * data_BIT_OF_GROUP = out_data_ptr; for (int i = 0; i < total_byte / (BIT_OF_GROUP / BIT_OF_BYTE); ++i) { diff --git a/lib/src/ssl_funcs/Md5.h b/lib/src/ssl_funcs/Md5.h index ba45f888..44c176a4 100644 --- a/lib/src/ssl_funcs/Md5.h +++ b/lib/src/ssl_funcs/Md5.h @@ -60,20 +60,18 @@ class Md5Encode }; public: - Md5Encode() - { - } - std::string Encode(std::string src_info); + + static std::string encode(const std::string &src_info); protected: - UInt32 CycleMoveLeft(UInt32 src_num, int bit_num_to_move); - UInt32 FillData(const char *in_data_ptr, int data_byte_len, char **out_data_ptr); - void RoundF(char *data_512_ptr, ParamDynamic ¶m); - void RoundG(char *data_512_ptr, ParamDynamic ¶m); - void RoundH(char *data_512_ptr, ParamDynamic ¶m); - void RoundI(char *data_512_ptr, ParamDynamic ¶m); - void RotationCalculate(char *data_512_ptr, ParamDynamic ¶m); - std::string GetHexStr(unsigned int num_str); + static UInt32 CycleMoveLeft(UInt32 src_num, int bit_num_to_move); + static void RoundF(char *data_512_ptr, ParamDynamic ¶m); + static void RoundG(char *data_512_ptr, ParamDynamic ¶m); + static void RoundH(char *data_512_ptr, ParamDynamic ¶m); + static void RoundI(char *data_512_ptr, ParamDynamic ¶m); + static void RotationCalculate(char *data_512_ptr, ParamDynamic ¶m); + static std::string GetHexStr(unsigned int num_str); + static UInt32 FillData(const char *in_data_ptr, int data_byte_len, char **out_data_ptr); private: // 幻数定义 diff --git a/lib/tests/CMakeLists.txt b/lib/tests/CMakeLists.txt index 781928ce..ce0c2d57 100755 --- a/lib/tests/CMakeLists.txt +++ b/lib/tests/CMakeLists.txt @@ -7,4 +7,4 @@ add_executable(class_name_test ClassNameTest.cc) add_executable(sha1_test Sha1Test.cc) add_executable(view_data_test HttpViewDataTest.cc) add_executable(orm_result_test OrmResultTest.cc) - +add_executable(md5_test Md5Test.cc ../src/ssl_funcs/Md5.cc) diff --git a/lib/tests/Md5Test.cc b/lib/tests/Md5Test.cc new file mode 100644 index 00000000..a26f27e0 --- /dev/null +++ b/lib/tests/Md5Test.cc @@ -0,0 +1,6 @@ +#include "../lib/src/ssl_funcs/Md5.h" +#include +int main() +{ + std::cout << Md5Encode::encode("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890") << std::endl; +} \ No newline at end of file diff --git a/orm_lib/src/DbClientImpl.cc b/orm_lib/src/DbClientImpl.cc index 4a1af5c2..93a33294 100644 --- a/orm_lib/src/DbClientImpl.cc +++ b/orm_lib/src/DbClientImpl.cc @@ -233,7 +233,7 @@ void DbClientImpl::handleNewTask(const DbConnectionPtr &connPtr) //Then check if there are some sql queries in the buffer { std::lock_guard guard(_bufferMutex); - if (_sqlCmdBuffer.size() > 0) + if (!_sqlCmdBuffer.empty()) { _busyConnections.insert(connPtr); //For new connections, this sentence is necessary auto cmd = _sqlCmdBuffer.front(); diff --git a/orm_lib/src/DbClientImpl.h b/orm_lib/src/DbClientImpl.h index d402566b..d8c576a6 100644 --- a/orm_lib/src/DbClientImpl.h +++ b/orm_lib/src/DbClientImpl.h @@ -78,7 +78,7 @@ private: QueryCallback _cb; ExceptPtrCallback _exceptCb; }; - std::list _sqlCmdBuffer; + std::deque _sqlCmdBuffer; std::mutex _bufferMutex; void handleNewTask(const DbConnectionPtr &conn); diff --git a/orm_lib/src/TransactionImpl.cc b/orm_lib/src/TransactionImpl.cc index a2764f8b..81bfea00 100644 --- a/orm_lib/src/TransactionImpl.cc +++ b/orm_lib/src/TransactionImpl.cc @@ -192,7 +192,7 @@ void TransactionImpl::execNewTask() if (!_isCommitedOrRolledback) { auto thisPtr = shared_from_this(); - if (_sqlCmdBuffer.size() > 0) + if (!_sqlCmdBuffer.empty()) { auto cmd = _sqlCmdBuffer.front(); _sqlCmdBuffer.pop_front(); @@ -223,7 +223,7 @@ void TransactionImpl::execNewTask() else { _isWorking = false; - if (_sqlCmdBuffer.size() > 0) + if (!_sqlCmdBuffer.empty()) { try { diff --git a/orm_lib/src/mysql_impl/MysqlConnection.cc b/orm_lib/src/mysql_impl/MysqlConnection.cc index c05fc392..2e04c4dd 100644 --- a/orm_lib/src/mysql_impl/MysqlConnection.cc +++ b/orm_lib/src/mysql_impl/MysqlConnection.cc @@ -37,11 +37,11 @@ Result makeResult(const std::shared_ptr &r = std::shared_ptr(new MYSQL, [](MYSQL *p) { + mysql_close(p); + })) { - _mysqlPtr = std::shared_ptr(new MYSQL, [](MYSQL *p) { - mysql_close(p); - }); mysql_init(_mysqlPtr.get()); mysql_options(_mysqlPtr.get(), MYSQL_OPT_NONBLOCK, 0); diff --git a/trantor b/trantor index 4f3bf10e..eb187880 160000 --- a/trantor +++ b/trantor @@ -1 +1 @@ -Subproject commit 4f3bf10eb996b3c44ee2d8f61a3add64d1fb6b1c +Subproject commit eb18788056164447715b6c2507a297b54ed519ba