Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-122803

QHttpHeaders optimisation: delay name normalization

    XMLWordPrintable

Details

    • Task
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • None
    • Network: HTTP
    • None

    Description

      This issue stems from this review comment.

      User may use the string (QAnyStringView) function overloads, and internally QHttpHeaders checks if the provided string can be mapped to a WellKnownHeader enum.

      Currently the code first normalizes the value, and then attempts to map that value to WellKnownHeader. The mapping-attempt could be done before value normalization => we don't unnecessarily normalize the value and allocate memory for it.

      However implementing this optimisation is not trivial. The straightforward approach:

      struct HeaderName
      {
      	 // ...
          explicit HeaderName(QAnyStringView name)
          {
              if (auto h = HeaderName::toWellKnownHeader(name)) // [A] pass directly as QASV
                  data = *h;
              else
                  data = normalizedName(name);
          }
      
          // Returns an enum corresponding with the 'name' if possible. Uses binary search (O(logN)).
          static std::optional<QHttpHeaders::WellKnownHeader> toWellKnownHeader(QAnyStringView name) noexcept
          {
              auto indexesBegin = std::cbegin(orderedHeaderNameIndexes);
              auto indexesEnd = std::cend(orderedHeaderNameIndexes);
      
              auto result = std::lower_bound(indexesBegin, indexesEnd, name, ByIndirectHeaderName{});
      
      		  // [B] modify to case insensitive comparison 
              if (result != indexesEnd
                  && QAnyStringView::compare(name, headerNames.viewAt(*result), Qt::CaseInsensitive) == 0) {
                  return static_cast<QHttpHeaders::WellKnownHeader>(*result);
              }
              return std::nullopt;
          }
          // ...
      };
      
      struct ByIndirectHeaderName
      {
      	// [C] QASV operator() overload:
         constexpr bool operator()(quint8 lhs, QAnyStringView rhs) const noexcept
         {
              return QAnyStringView::compare(headerNames.viewAt(lhs), rhs, Qt::CaseInsensitive) < 0;
         }
      };
      

      works, but according to tst_bench_qhttpheaders, the performance actually decreases because we do case-insensitive comparisons. So some additional mechanism would be needed.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            cnn Qt Core & Network
            vuokko Juha Vuolle
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes