You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by "bneradt (via GitHub)" <gi...@apache.org> on 2025/12/05 22:40:54 UTC

[PR] Automatic caching of parsed STRING config values [trafficserver]

bneradt opened a new pull request, #12735:
URL: https://github.com/apache/trafficserver/pull/12735

   This introduces ParsedConfigCache in HttpConfig to automatically cache parsed results for STRING configs that require expensive parsing. When TSHttpTxnConfigStringSet() is called for configs like negative_caching_list, insert_forwarded, server_session_sharing.match, negative_revalidating_list, or host_res_data (ip_resolve), the parsing now happens once per unique value and is cached for subsequent calls. This optimization is transparent to plugins - they call TSHttpTxnConfigStringSet() as usual and automatically benefit from the caching.
   
   Fixes: #12292


-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2601002343


##########
include/proxy/http/HttpConfig.h:
##########
@@ -852,6 +856,72 @@ struct HttpConfigParams : public ConfigInfo {
   HttpConfigParams &operator=(const HttpConfigParams &) = delete;
 };
 
+/////////////////////////////////////////////////////////////
+//
+// class ParsedConfigCache
+//
+/////////////////////////////////////////////////////////////
+
+/** Cache for pre-parsed string config values.
+ *
+ * Some overridable STRING configs require parsing (e.g., status code lists,
+ * host resolution preferences). Parsing can be non-trivial. This cache stores
+ * parsed results so repeated calls to TSHttpTxnConfigStringSet() with the same
+ * value don't re-parse.
+ *
+ * The static lookup() method handles everything: check cache, parse if needed,
+ * store in cache, and return the result.
+ */
+class ParsedConfigCache
+{
+public:
+  /** Pre-parsed representations for configs that need special parsing. */
+  struct ParsedValue {
+    HostResData                 host_res_data{};
+    HttpStatusCodeList          status_code_list{};
+    HttpForwarded::OptionBitSet forwarded_bitset{};
+    MgmtByte                    server_session_sharing_match{0};
+    std::string                 conf_value_storage{}; // Owns the string data.
+  };
+
+  /** Return the parsed value for the configuration.
+   *
+   * On first call for a given (key, value) pair, parses the value and caches it.
+   * Subsequent calls return the cached result.
+   *
+   * @param key The config key being referenced.
+   * @param value The string value to parse.
+   * @return Reference to the cached parsed value.
+   */
+  static const ParsedValue &lookup(TSOverridableConfigKey key, std::string_view value);
+
+private:
+  ParsedConfigCache() = default;
+
+  // Enforce singleton pattern.
+  ParsedConfigCache(const ParsedConfigCache &)            = delete;
+  ParsedConfigCache &operator=(const ParsedConfigCache &) = delete;
+  ParsedConfigCache(ParsedConfigCache &&)                 = delete;
+  ParsedConfigCache &operator=(ParsedConfigCache &&)      = delete;
+
+  static ParsedConfigCache &instance();
+
+  const ParsedValue &lookup_impl(TSOverridableConfigKey key, std::string_view value);
+  ParsedValue        parse(TSOverridableConfigKey key, std::string_view value);
+
+  // Custom hash for the cache key.
+  struct CacheKeyHash {
+    std::size_t
+    operator()(const std::pair<TSOverridableConfigKey, std::string> &k) const
+    {
+      return std::hash<int>()(static_cast<int>(k.first)) ^ (std::hash<std::string>()(k.second) << 1);

Review Comment:
   Absolutely right.
   
   The 1-bit shift breaks the symmetry of XOR to avoid collisions when both hashes happen to be equal (which would XOR to 0) and to reduce collision patterns from the commutative property of XOR. The shift amount doesn't need to be larger than 1 — any non-zero shift achieves the asymmetry goal.
   



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt merged PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735


-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "masaori335 (via GitHub)" <gi...@apache.org>.
masaori335 commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2596695394


##########
include/proxy/http/HttpConfig.h:
##########
@@ -852,6 +856,72 @@ struct HttpConfigParams : public ConfigInfo {
   HttpConfigParams &operator=(const HttpConfigParams &) = delete;
 };
 
+/////////////////////////////////////////////////////////////
+//
+// class ParsedConfigCache
+//
+/////////////////////////////////////////////////////////////
+
+/** Cache for pre-parsed string config values.
+ *
+ * Some overridable STRING configs require parsing (e.g., status code lists,
+ * host resolution preferences). Parsing can be non-trivial. This cache stores
+ * parsed results so repeated calls to TSHttpTxnConfigStringSet() with the same
+ * value don't re-parse.
+ *
+ * The static lookup() method handles everything: check cache, parse if needed,
+ * store in cache, and return the result.
+ */
+class ParsedConfigCache
+{
+public:
+  /** Pre-parsed representations for configs that need special parsing. */
+  struct ParsedValue {
+    HostResData                 host_res_data{};
+    HttpStatusCodeList          status_code_list{};
+    HttpForwarded::OptionBitSet forwarded_bitset{};
+    MgmtByte                    server_session_sharing_match{0};
+    std::string                 conf_value_storage{}; // Owns the string data.
+  };
+
+  /** Return the parsed value for the configuration.
+   *
+   * On first call for a given (key, value) pair, parses the value and caches it.
+   * Subsequent calls return the cached result.
+   *
+   * @param key The config key being referenced.
+   * @param value The string value to parse.
+   * @return Reference to the cached parsed value.
+   */
+  static const ParsedValue &lookup(TSOverridableConfigKey key, std::string_view value);
+
+private:
+  ParsedConfigCache() = default;
+
+  // Enforce singleton pattern.
+  ParsedConfigCache(const ParsedConfigCache &)            = delete;
+  ParsedConfigCache &operator=(const ParsedConfigCache &) = delete;
+  ParsedConfigCache(ParsedConfigCache &&)                 = delete;
+  ParsedConfigCache &operator=(ParsedConfigCache &&)      = delete;
+
+  static ParsedConfigCache &instance();
+
+  const ParsedValue &lookup_impl(TSOverridableConfigKey key, std::string_view value);
+  ParsedValue        parse(TSOverridableConfigKey key, std::string_view value);
+
+  // Custom hash for the cache key.
+  struct CacheKeyHash {
+    std::size_t
+    operator()(const std::pair<TSOverridableConfigKey, std::string> &k) const
+    {
+      return std::hash<int>()(static_cast<int>(k.first)) ^ (std::hash<std::string>()(k.second) << 1);

Review Comment:
   I guess this 1 bit left-shift is trying to avoid collision. Because If hash of `k.first` and `k.second` is the same value, it'll be 0, right?
   
   ~I'm a bit wondering if we should shift more than 1 bit. The length of return value of `std::hash` is fixed?~



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "masaori335 (via GitHub)" <gi...@apache.org>.
masaori335 commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2596695394


##########
include/proxy/http/HttpConfig.h:
##########
@@ -852,6 +856,72 @@ struct HttpConfigParams : public ConfigInfo {
   HttpConfigParams &operator=(const HttpConfigParams &) = delete;
 };
 
+/////////////////////////////////////////////////////////////
+//
+// class ParsedConfigCache
+//
+/////////////////////////////////////////////////////////////
+
+/** Cache for pre-parsed string config values.
+ *
+ * Some overridable STRING configs require parsing (e.g., status code lists,
+ * host resolution preferences). Parsing can be non-trivial. This cache stores
+ * parsed results so repeated calls to TSHttpTxnConfigStringSet() with the same
+ * value don't re-parse.
+ *
+ * The static lookup() method handles everything: check cache, parse if needed,
+ * store in cache, and return the result.
+ */
+class ParsedConfigCache
+{
+public:
+  /** Pre-parsed representations for configs that need special parsing. */
+  struct ParsedValue {
+    HostResData                 host_res_data{};
+    HttpStatusCodeList          status_code_list{};
+    HttpForwarded::OptionBitSet forwarded_bitset{};
+    MgmtByte                    server_session_sharing_match{0};
+    std::string                 conf_value_storage{}; // Owns the string data.
+  };
+
+  /** Return the parsed value for the configuration.
+   *
+   * On first call for a given (key, value) pair, parses the value and caches it.
+   * Subsequent calls return the cached result.
+   *
+   * @param key The config key being referenced.
+   * @param value The string value to parse.
+   * @return Reference to the cached parsed value.
+   */
+  static const ParsedValue &lookup(TSOverridableConfigKey key, std::string_view value);
+
+private:
+  ParsedConfigCache() = default;
+
+  // Enforce singleton pattern.
+  ParsedConfigCache(const ParsedConfigCache &)            = delete;
+  ParsedConfigCache &operator=(const ParsedConfigCache &) = delete;
+  ParsedConfigCache(ParsedConfigCache &&)                 = delete;
+  ParsedConfigCache &operator=(ParsedConfigCache &&)      = delete;
+
+  static ParsedConfigCache &instance();
+
+  const ParsedValue &lookup_impl(TSOverridableConfigKey key, std::string_view value);
+  ParsedValue        parse(TSOverridableConfigKey key, std::string_view value);
+
+  // Custom hash for the cache key.
+  struct CacheKeyHash {
+    std::size_t
+    operator()(const std::pair<TSOverridableConfigKey, std::string> &k) const
+    {
+      return std::hash<int>()(static_cast<int>(k.first)) ^ (std::hash<std::string>()(k.second) << 1);

Review Comment:
   I guess this 1 bit left-shift is trying to avoid 0 by XOR, right?



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "masaori335 (via GitHub)" <gi...@apache.org>.
masaori335 commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2596686810


##########
include/proxy/http/HttpConfig.h:
##########
@@ -852,6 +856,72 @@ struct HttpConfigParams : public ConfigInfo {
   HttpConfigParams &operator=(const HttpConfigParams &) = delete;
 };
 
+/////////////////////////////////////////////////////////////
+//
+// class ParsedConfigCache
+//
+/////////////////////////////////////////////////////////////
+
+/** Cache for pre-parsed string config values.
+ *
+ * Some overridable STRING configs require parsing (e.g., status code lists,
+ * host resolution preferences). Parsing can be non-trivial. This cache stores
+ * parsed results so repeated calls to TSHttpTxnConfigStringSet() with the same
+ * value don't re-parse.
+ *
+ * The static lookup() method handles everything: check cache, parse if needed,
+ * store in cache, and return the result.
+ */
+class ParsedConfigCache
+{
+public:
+  /** Pre-parsed representations for configs that need special parsing. */
+  struct ParsedValue {
+    HostResData                 host_res_data{};
+    HttpStatusCodeList          status_code_list{};
+    HttpForwarded::OptionBitSet forwarded_bitset{};
+    MgmtByte                    server_session_sharing_match{0};
+    std::string                 conf_value_storage{}; // Owns the string data.
+  };
+
+  /** Return the parsed value for the configuration.
+   *
+   * On first call for a given (key, value) pair, parses the value and caches it.
+   * Subsequent calls return the cached result.
+   *
+   * @param key The config key being referenced.
+   * @param value The string value to parse.
+   * @return Reference to the cached parsed value.
+   */
+  static const ParsedValue &lookup(TSOverridableConfigKey key, std::string_view value);
+
+private:
+  ParsedConfigCache() = default;
+
+  // Enforce singleton pattern.
+  ParsedConfigCache(const ParsedConfigCache &)            = delete;
+  ParsedConfigCache &operator=(const ParsedConfigCache &) = delete;
+  ParsedConfigCache(ParsedConfigCache &&)                 = delete;
+  ParsedConfigCache &operator=(ParsedConfigCache &&)      = delete;
+
+  static ParsedConfigCache &instance();
+
+  const ParsedValue &lookup_impl(TSOverridableConfigKey key, std::string_view value);
+  ParsedValue        parse(TSOverridableConfigKey key, std::string_view value);
+
+  // Custom hash for the cache key.
+  struct CacheKeyHash {
+    std::size_t
+    operator()(const std::pair<TSOverridableConfigKey, std::string> &k) const
+    {
+      return std::hash<int>()(static_cast<int>(k.first)) ^ (std::hash<std::string>()(k.second) << 1);
+    }
+  };
+
+  std::unordered_map<std::pair<TSOverridableConfigKey, std::string>, ParsedValue, CacheKeyHash> _cache;
+  mutable std::shared_mutex                                                                     _mutex;

Review Comment:
   `ts::bravo::shared_mutex` might be faster? Because this map is read heavy.



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#issuecomment-3619055470

   [approve ci fedora]


-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "masaori335 (via GitHub)" <gi...@apache.org>.
masaori335 commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2596695394


##########
include/proxy/http/HttpConfig.h:
##########
@@ -852,6 +856,72 @@ struct HttpConfigParams : public ConfigInfo {
   HttpConfigParams &operator=(const HttpConfigParams &) = delete;
 };
 
+/////////////////////////////////////////////////////////////
+//
+// class ParsedConfigCache
+//
+/////////////////////////////////////////////////////////////
+
+/** Cache for pre-parsed string config values.
+ *
+ * Some overridable STRING configs require parsing (e.g., status code lists,
+ * host resolution preferences). Parsing can be non-trivial. This cache stores
+ * parsed results so repeated calls to TSHttpTxnConfigStringSet() with the same
+ * value don't re-parse.
+ *
+ * The static lookup() method handles everything: check cache, parse if needed,
+ * store in cache, and return the result.
+ */
+class ParsedConfigCache
+{
+public:
+  /** Pre-parsed representations for configs that need special parsing. */
+  struct ParsedValue {
+    HostResData                 host_res_data{};
+    HttpStatusCodeList          status_code_list{};
+    HttpForwarded::OptionBitSet forwarded_bitset{};
+    MgmtByte                    server_session_sharing_match{0};
+    std::string                 conf_value_storage{}; // Owns the string data.
+  };
+
+  /** Return the parsed value for the configuration.
+   *
+   * On first call for a given (key, value) pair, parses the value and caches it.
+   * Subsequent calls return the cached result.
+   *
+   * @param key The config key being referenced.
+   * @param value The string value to parse.
+   * @return Reference to the cached parsed value.
+   */
+  static const ParsedValue &lookup(TSOverridableConfigKey key, std::string_view value);
+
+private:
+  ParsedConfigCache() = default;
+
+  // Enforce singleton pattern.
+  ParsedConfigCache(const ParsedConfigCache &)            = delete;
+  ParsedConfigCache &operator=(const ParsedConfigCache &) = delete;
+  ParsedConfigCache(ParsedConfigCache &&)                 = delete;
+  ParsedConfigCache &operator=(ParsedConfigCache &&)      = delete;
+
+  static ParsedConfigCache &instance();
+
+  const ParsedValue &lookup_impl(TSOverridableConfigKey key, std::string_view value);
+  ParsedValue        parse(TSOverridableConfigKey key, std::string_view value);
+
+  // Custom hash for the cache key.
+  struct CacheKeyHash {
+    std::size_t
+    operator()(const std::pair<TSOverridableConfigKey, std::string> &k) const
+    {
+      return std::hash<int>()(static_cast<int>(k.first)) ^ (std::hash<std::string>()(k.second) << 1);

Review Comment:
   I guess this 1 bit left-shift is trying to avoid collision. Because If `static_cast<int>(k.first)` and `std::hash<std::string>()(k.second)` is the same value, it'll be 0, right?
   
   I'm a bit wondering if we should shift more than 1 bit.



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "masaori335 (via GitHub)" <gi...@apache.org>.
masaori335 commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2596695394


##########
include/proxy/http/HttpConfig.h:
##########
@@ -852,6 +856,72 @@ struct HttpConfigParams : public ConfigInfo {
   HttpConfigParams &operator=(const HttpConfigParams &) = delete;
 };
 
+/////////////////////////////////////////////////////////////
+//
+// class ParsedConfigCache
+//
+/////////////////////////////////////////////////////////////
+
+/** Cache for pre-parsed string config values.
+ *
+ * Some overridable STRING configs require parsing (e.g., status code lists,
+ * host resolution preferences). Parsing can be non-trivial. This cache stores
+ * parsed results so repeated calls to TSHttpTxnConfigStringSet() with the same
+ * value don't re-parse.
+ *
+ * The static lookup() method handles everything: check cache, parse if needed,
+ * store in cache, and return the result.
+ */
+class ParsedConfigCache
+{
+public:
+  /** Pre-parsed representations for configs that need special parsing. */
+  struct ParsedValue {
+    HostResData                 host_res_data{};
+    HttpStatusCodeList          status_code_list{};
+    HttpForwarded::OptionBitSet forwarded_bitset{};
+    MgmtByte                    server_session_sharing_match{0};
+    std::string                 conf_value_storage{}; // Owns the string data.
+  };
+
+  /** Return the parsed value for the configuration.
+   *
+   * On first call for a given (key, value) pair, parses the value and caches it.
+   * Subsequent calls return the cached result.
+   *
+   * @param key The config key being referenced.
+   * @param value The string value to parse.
+   * @return Reference to the cached parsed value.
+   */
+  static const ParsedValue &lookup(TSOverridableConfigKey key, std::string_view value);
+
+private:
+  ParsedConfigCache() = default;
+
+  // Enforce singleton pattern.
+  ParsedConfigCache(const ParsedConfigCache &)            = delete;
+  ParsedConfigCache &operator=(const ParsedConfigCache &) = delete;
+  ParsedConfigCache(ParsedConfigCache &&)                 = delete;
+  ParsedConfigCache &operator=(ParsedConfigCache &&)      = delete;
+
+  static ParsedConfigCache &instance();
+
+  const ParsedValue &lookup_impl(TSOverridableConfigKey key, std::string_view value);
+  ParsedValue        parse(TSOverridableConfigKey key, std::string_view value);
+
+  // Custom hash for the cache key.
+  struct CacheKeyHash {
+    std::size_t
+    operator()(const std::pair<TSOverridableConfigKey, std::string> &k) const
+    {
+      return std::hash<int>()(static_cast<int>(k.first)) ^ (std::hash<std::string>()(k.second) << 1);

Review Comment:
   I guess this 1 bit left-shift is trying to avoid collision. Because If `static_cast<int>(k.first)` and `std::hash<std::string>()(k.second)` is the same value, it'll be 0, right?
   
   I'm a bit wondering if we should shift more than 1 bit. The length of return value of `std::hash` is fixed?



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "masaori335 (via GitHub)" <gi...@apache.org>.
masaori335 commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2596689966


##########
include/proxy/http/HttpConfig.h:
##########
@@ -852,6 +856,72 @@ struct HttpConfigParams : public ConfigInfo {
   HttpConfigParams &operator=(const HttpConfigParams &) = delete;
 };
 
+/////////////////////////////////////////////////////////////
+//
+// class ParsedConfigCache
+//
+/////////////////////////////////////////////////////////////
+
+/** Cache for pre-parsed string config values.
+ *
+ * Some overridable STRING configs require parsing (e.g., status code lists,
+ * host resolution preferences). Parsing can be non-trivial. This cache stores
+ * parsed results so repeated calls to TSHttpTxnConfigStringSet() with the same
+ * value don't re-parse.
+ *
+ * The static lookup() method handles everything: check cache, parse if needed,
+ * store in cache, and return the result.
+ */
+class ParsedConfigCache
+{
+public:
+  /** Pre-parsed representations for configs that need special parsing. */
+  struct ParsedValue {

Review Comment:
   Kind of nit-picking, `union` or `std::variant` is better? Because we only use one of these members.



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2594163400


##########
src/api/InkAPI.cc:
##########
@@ -7527,23 +7536,22 @@ TSHttpTxnConfigStringSet(TSHttpTxn txnp, TSOverridableConfigKey conf, const char
     break;
   case TS_CONFIG_HTTP_NEGATIVE_CACHING_LIST:
     if (value && length > 0) {
-      OverridableHttpConfigParams *target      = &s->t_state.my_txn_conf();
-      target->negative_caching_list.conf_value = const_cast<char *>(value);
-      return _eval_conv(target, conf, value, length);
+      auto &parsed                                   = ParsedConfigCache::lookup(conf, std::string_view(value, length));
+      s->t_state.my_txn_conf().negative_caching_list = parsed.status_code_list;

Review Comment:
   These `ParsedConfigCache::lookup` are the high level view of where the value of this patch happens. Instead of calling the conversion logic for each of these "complex" strings (like the negative caching status list, in this example) every transaction, this parsed value retrieval is called through an interface that caches the results so that the compution is only done once.



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2600995549


##########
include/proxy/http/HttpConfig.h:
##########
@@ -852,6 +856,72 @@ struct HttpConfigParams : public ConfigInfo {
   HttpConfigParams &operator=(const HttpConfigParams &) = delete;
 };
 
+/////////////////////////////////////////////////////////////
+//
+// class ParsedConfigCache
+//
+/////////////////////////////////////////////////////////////
+
+/** Cache for pre-parsed string config values.
+ *
+ * Some overridable STRING configs require parsing (e.g., status code lists,
+ * host resolution preferences). Parsing can be non-trivial. This cache stores
+ * parsed results so repeated calls to TSHttpTxnConfigStringSet() with the same
+ * value don't re-parse.
+ *
+ * The static lookup() method handles everything: check cache, parse if needed,
+ * store in cache, and return the result.
+ */
+class ParsedConfigCache
+{
+public:
+  /** Pre-parsed representations for configs that need special parsing. */
+  struct ParsedValue {
+    HostResData                 host_res_data{};
+    HttpStatusCodeList          status_code_list{};
+    HttpForwarded::OptionBitSet forwarded_bitset{};
+    MgmtByte                    server_session_sharing_match{0};
+    std::string                 conf_value_storage{}; // Owns the string data.
+  };
+
+  /** Return the parsed value for the configuration.
+   *
+   * On first call for a given (key, value) pair, parses the value and caches it.
+   * Subsequent calls return the cached result.
+   *
+   * @param key The config key being referenced.
+   * @param value The string value to parse.
+   * @return Reference to the cached parsed value.
+   */
+  static const ParsedValue &lookup(TSOverridableConfigKey key, std::string_view value);
+
+private:
+  ParsedConfigCache() = default;
+
+  // Enforce singleton pattern.
+  ParsedConfigCache(const ParsedConfigCache &)            = delete;
+  ParsedConfigCache &operator=(const ParsedConfigCache &) = delete;
+  ParsedConfigCache(ParsedConfigCache &&)                 = delete;
+  ParsedConfigCache &operator=(ParsedConfigCache &&)      = delete;
+
+  static ParsedConfigCache &instance();
+
+  const ParsedValue &lookup_impl(TSOverridableConfigKey key, std::string_view value);
+  ParsedValue        parse(TSOverridableConfigKey key, std::string_view value);
+
+  // Custom hash for the cache key.
+  struct CacheKeyHash {
+    std::size_t
+    operator()(const std::pair<TSOverridableConfigKey, std::string> &k) const
+    {
+      return std::hash<int>()(static_cast<int>(k.first)) ^ (std::hash<std::string>()(k.second) << 1);
+    }
+  };
+
+  std::unordered_map<std::pair<TSOverridableConfigKey, std::string>, ParsedValue, CacheKeyHash> _cache;
+  mutable std::shared_mutex                                                                     _mutex;

Review Comment:
   Good idea. I forgot about bravo. 



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Automatic caching of parsed STRING config values [trafficserver]

Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on code in PR #12735:
URL: https://github.com/apache/trafficserver/pull/12735#discussion_r2600996257


##########
include/proxy/http/HttpConfig.h:
##########
@@ -852,6 +856,72 @@ struct HttpConfigParams : public ConfigInfo {
   HttpConfigParams &operator=(const HttpConfigParams &) = delete;
 };
 
+/////////////////////////////////////////////////////////////
+//
+// class ParsedConfigCache
+//
+/////////////////////////////////////////////////////////////
+
+/** Cache for pre-parsed string config values.
+ *
+ * Some overridable STRING configs require parsing (e.g., status code lists,
+ * host resolution preferences). Parsing can be non-trivial. This cache stores
+ * parsed results so repeated calls to TSHttpTxnConfigStringSet() with the same
+ * value don't re-parse.
+ *
+ * The static lookup() method handles everything: check cache, parse if needed,
+ * store in cache, and return the result.
+ */
+class ParsedConfigCache
+{
+public:
+  /** Pre-parsed representations for configs that need special parsing. */
+  struct ParsedValue {

Review Comment:
   True. Sematically, std::variant is nice because it communicates explicitly that only one is used at a time.



-- 
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: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org