Commit 3d158435 authored by jan.koester's avatar jan.koester
Browse files

perf opt

parent e9174e66
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ enable_testing ()

set(LIBV "1.0.0")
set(Upstream_VERSION 1.0.0)
set (BLOCKSIZE 16384 CACHE STRING "Block size from Network Packages")
set (BLOCKSIZE 65536 CACHE STRING "Block size from Network Packages")
set (DEFAULT_UPLOADSIZE 4e+6 CACHE STRING "Block size for Uploads")

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
+39 −11
Original line number Diff line number Diff line
@@ -1689,6 +1689,18 @@ const std ::string &libhttppp::HttpResponse::getContentType() const{

size_t libhttppp::HttpResponse::printHeader(std::vector<char> &buffer){

  // Pre-estimate header size to avoid repeated reallocations
  size_t estimatedSize = _Version.size() + 1 + _State.size() + 2;
  for(HeaderData *curdat=getfirstHeaderData(); curdat; curdat=curdat->nextHeaderData()){
    estimatedSize += curdat->getkey().size() + 2; // "key: "
    for(HeaderData::Values *cval=curdat->getfirstValue(); cval; cval=cval->nextvalue()){
      estimatedSize += cval->getvalue().size() + 2; // "value; "
    }
    estimatedSize += 2; // "\r\n"
  }
  estimatedSize += 2; // trailing "\r\n"
  buffer.reserve(buffer.size() + estimatedSize);

  auto append=[&buffer](const std::string &data){
    buffer.insert(buffer.end(), data.begin(), data.end());
  };
@@ -2777,10 +2789,10 @@ void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data) {
  boundaryLine.insert(boundaryLine.end(), _Boundary.begin(), _Boundary.end());
  const size_t B = boundaryLine.size();

  // Make a working copy (your original did this)
  netplus::condata<char> req;
  std::copy(data.begin(), data.end(),
            std::inserter<netplus::condata<char>>(req, req.begin()));
  // Work directly on the original data via const reference instead of
  // making a full copy. This avoids an O(n) allocation + copy for
  // potentially very large upload bodies (multi-MB files).
  const netplus::condata<char> &req = data;

  // Helper to compare a slice with boundaryLine
  auto matchBoundaryAt = [&](size_t pos) -> bool {
@@ -2888,15 +2900,27 @@ void libhttppp::HttpForm::_parseMulitpart(const netplus::condata<char> &data) {
  }
}

void libhttppp::HttpForm::_parseMultiSection(netplus::condata<char> &data,size_t start, size_t end){
void libhttppp::HttpForm::_parseMultiSection(const netplus::condata<char> &data,size_t start, size_t end){

  if(data.empty())
    return;

  size_t findel=data.search("\r\n\r\n",start),findelsize=4;
  // Local const-safe search helper (replaces non-const data.search())
  auto searchFrom = [&data](const char *word, size_t from) -> size_t {
    size_t wlen = std::strlen(word);
    if (wlen == 0 || data.size() < wlen) return std::string::npos;
    size_t limit = data.size() - wlen;
    for (size_t i = from; i <= limit; ++i) {
      if (std::memcmp(data.data() + i, word, wlen) == 0)
        return i;
    }
    return std::string::npos;
  };

  size_t findel=searchFrom("\r\n\r\n",start),findelsize=4;

  if(findel==std::string::npos){
    findel=data.search("\n\n",start);
    findel=searchFrom("\n\n",start);
    findelsize=2;
  }

@@ -2939,8 +2963,9 @@ void libhttppp::HttpForm::_parseMultiSection(netplus::condata<char> &data,size_t
          break;
        }
      }
      content._Value.reserve(lineEnd - ctlt + 1);
      std::copy(data.begin()+ctlt,data.begin()+lineEnd,
                std::inserter<std::vector<char>>(content._Value,content._Value.begin()));
                std::back_inserter(content._Value));
      content._Value.push_back('\0');

      MultipartFormData._lastData->addContent(content);
@@ -3014,9 +3039,12 @@ void libhttppp::HttpForm::_parseMultiSection(netplus::condata<char> &data,size_t
      }
    }

    if(MultipartFormData._lastData)
      std::copy(data.begin()+(findel+findelsize),data.begin()+end,std::inserter<std::vector<char>>(MultipartFormData._lastData->Value,
                                                                                                  MultipartFormData._lastData->Value.begin()));
    if(MultipartFormData._lastData){
      size_t valueLen = end - (findel + findelsize);
      MultipartFormData._lastData->Value.reserve(valueLen);
      std::copy(data.begin()+(findel+findelsize),data.begin()+end,
                std::back_inserter(MultipartFormData._lastData->Value));
    }
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -461,7 +461,7 @@ namespace libhttppp {

    /*multiform*/
    void               _parseMulitpart(const netplus::condata<char> &data);
    void               _parseMultiSection(netplus::condata<char> &data,size_t start, size_t end);
    void               _parseMultiSection(const netplus::condata<char> &data,size_t start, size_t end);
    std::vector<char>  _Boundary;
    
    /*both methods*/
+12 −14
Original line number Diff line number Diff line
@@ -342,6 +342,8 @@ void libhttppp::HttpEvent::_dispatchH2Stream(HttpRequest &cureq,

        // Capture body data from tempreq's SendData
        std::string body;
        if (content_length > 0)
            body.reserve(content_length);
        if (!tempreq.SendData.empty()) {
            body.assign(tempreq.SendData.data(), tempreq.SendData.size());
            tempreq.SendData.clear();
@@ -355,18 +357,15 @@ void libhttppp::HttpEvent::_dispatchH2Stream(HttpRequest &cureq,
            tempreq.h2state().expectedContentLength = content_length;
            tempreq.SendData.pos = 0;

            size_t max_iterations = content_length / 64 + 4096;
            size_t max_iterations = content_length / BLOCKSIZE + 4096;
            for (size_t i = 0;
                 i < max_iterations && body.size() < content_length;
                 ++i) {
                size_t sb = tempreq.SendData.size();
                ResponseEvent(tempreq, tid, args);
                size_t sa = tempreq.SendData.size();
                if (sa > sb) {
                    body.append(tempreq.SendData.data() + sb, sa - sb);
                    tempreq.SendData.erase(
                        tempreq.SendData.begin() + sb,
                        tempreq.SendData.begin() + sa);
                if (sa > 0) {
                    body.append(tempreq.SendData.data(), sa);
                    tempreq.SendData.clear();
                } else {
                    break; // No more data available
                }
@@ -648,6 +647,8 @@ void libhttppp::HttpEvent::Http3StreamEvent(netplus::socket *sock,

        // Capture body data from SendData
        std::string body;
        if (content_length > 0)
            body.reserve(content_length);
        if (!tempreq.SendData.empty()) {
            body.assign(tempreq.SendData.data(), tempreq.SendData.size());
            tempreq.SendData.clear();
@@ -659,18 +660,15 @@ void libhttppp::HttpEvent::Http3StreamEvent(netplus::socket *sock,
        // If streaming, collect all remaining data
        if (is_streaming) {
            tempreq.SendData.pos = 0;
            size_t max_iter = content_length / 64 + 4096;
            size_t max_iter = content_length / BLOCKSIZE + 4096;
            for (size_t i = 0;
                 i < max_iter && body.size() < content_length;
                 ++i) {
                size_t sb = tempreq.SendData.size();
                ResponseEvent(tempreq, 0, 0);
                size_t sa = tempreq.SendData.size();
                if (sa > sb) {
                    body.append(tempreq.SendData.data() + sb, sa - sb);
                    tempreq.SendData.erase(
                        tempreq.SendData.begin() + sb,
                        tempreq.SendData.begin() + sa);
                if (sa > 0) {
                    body.append(tempreq.SendData.data(), sa);
                    tempreq.SendData.clear();
                } else {
                    break;
                }