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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]