Optimize web_server UrlMatch to avoid heap allocations (#9263)

This commit is contained in:
J. Nick Koston
2025-06-29 23:12:03 -05:00
committed by GitHub
parent 140ca070a2
commit 9890659f61
2 changed files with 186 additions and 125 deletions
File diff suppressed because it is too large Load Diff
+21 -3
View File
@@ -40,10 +40,28 @@ namespace web_server {
/// Internal helper struct that is used to parse incoming URLs
struct UrlMatch {
std::string domain; ///< The domain of the component, for example "sensor"
std::string id; ///< The id of the device that's being accessed, for example "living_room_fan"
std::string method; ///< The method that's being called, for example "turn_on"
const char *domain; ///< Pointer to domain within URL, for example "sensor"
const char *id; ///< Pointer to id within URL, for example "living_room_fan"
const char *method; ///< Pointer to method within URL, for example "turn_on"
uint8_t domain_len; ///< Length of domain string
uint8_t id_len; ///< Length of id string
uint8_t method_len; ///< Length of method string
bool valid; ///< Whether this match is valid
// Helper methods for string comparisons
bool domain_equals(const char *str) const {
return domain && domain_len == strlen(str) && memcmp(domain, str, domain_len) == 0;
}
bool id_equals(const std::string &str) const {
return id && id_len == str.length() && memcmp(id, str.c_str(), id_len) == 0;
}
bool method_equals(const char *str) const {
return method && method_len == strlen(str) && memcmp(method, str, method_len) == 0;
}
bool method_empty() const { return method_len == 0; }
};
#ifdef USE_WEBSERVER_SORTING