cmcfarlen commented on code in PR #12592:
URL: https://github.com/apache/trafficserver/pull/12592#discussion_r2463151987


##########
src/proxy/hdrs/MIME.cc:
##########
@@ -51,17 +53,58 @@ using swoc::TextView;
  *                          C O N S T A N T S                          *
  *                                                                     *
  ***********************************************************************/
-static DFA *day_names_dfa   = nullptr;
-static DFA *month_names_dfa = nullptr;
 
-static constexpr const char *day_names[] = {
+static constexpr std::array<std::string_view, 7> day_names = {
   "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
 };
 
-static constexpr const char *month_names[] = {
+static constexpr std::array<std::string_view, 12> month_names = {
   "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", 
"Dec",
 };
 
+template <size_t count>
+static consteval std::array<uint32_t, count>
+make_packed(const std::array<std::string_view, count> &names)
+{
+  std::array<uint32_t, count> packed{};
+
+  auto tl = [](char c) -> char { return (c >= 'A' && c <= 'Z') ? (c + 32) : c; 
};
+
+  for (size_t i = 0; i < count; ++i) {
+    const auto    &sv = names[i];
+    const uint32_t c0 = tl(static_cast<unsigned char>(sv[0]));
+    const uint32_t c1 = tl(static_cast<unsigned char>(sv[1]));
+    const uint32_t c2 = tl(static_cast<unsigned char>(sv[2]));
+    packed[i]         = (c0 << 16) | (c1 << 8) | c2;
+  }
+  return packed;
+}
+
+static constexpr std::array<uint32_t, day_names.size()>   day_names_packed   = 
make_packed(day_names);
+static constexpr std::array<uint32_t, month_names.size()> month_names_packed = 
make_packed(month_names);
+
+// Case-insensitive match of first 3 characters of input string against array 
of names
+// Longer strings will match if their first 3 characters match - this is 
intentional for
+// matching non-standard day/month names like "Thursday" or "September".
+template <size_t count>
+__attribute__((always_inline)) static constexpr int
+match_3char_ci(const std::string_view s, const std::array<uint32_t, count> 
&names_packed)
+{
+  if (s.size() < 3) {
+    return -1;
+  }
+
+  auto           tl     = [](char c) -> char { return (c >= 'A' && c <= 'Z') ? 
(c + 32) : c; };
+  const uint32_t packed = (tl(s[0]) << 16) | (tl(s[1]) << 8) | tl(s[2]);
+
+  for (size_t i = 0; i < count; i++) {

Review Comment:
   Since the packed names are constexpr, the compiler knows the length and 
contents at compile time.  So it can just hardcode the comparisons.  No loops, 
just cmp and jmp.
   
   https://godbolt.org/z/Pzb7WT6Mr



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to