Skip to content

Commit 28a5746

Browse files
committed
Use filename hashes for HTTP content lookup
Add a uint32_t hash field to FilesContent and populate it in the generated content table. The content generator (generate_content.cpp) now includes utils_hash.h and emits Fnv1a32Runtime(dir_entry->d_name, ...) values when creating entries. GetFileContent now computes the runtime FNV-1a hash for the requested file and compares it to content.hash instead of using strcmp, improving lookup performance and avoiding string comparisons.
1 parent 94d876d commit 28a5746

3 files changed

Lines changed: 28 additions & 20 deletions

File tree

lib-remoteconfig/http/content/content.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#endif /* !defined (CONFIG_HTTP_HTML_NO_DMX) && (defined(OUTPUT_DMX_SEND) || defined(OUTPUT_DMX_SEND_MULTI)) */
4545

4646
struct FilesContent {
47+
uint32_t hash;
4748
const char *file_name;
4849
const char *content;
4950
uint32_t content_length;
@@ -52,43 +53,43 @@ struct FilesContent {
5253

5354
inline constexpr struct FilesContent kHttpContent[] = {
5455
#if !defined (CONFIG_HTTP_HTML_NO_DMX) && (defined(OUTPUT_DMX_SEND) || defined(OUTPUT_DMX_SEND_MULTI))
55-
{ "dmx.js", dmx_js, 1357, static_cast<http::ContentTypes>(2) },
56+
{ 449885855,"dmx.js", dmx_js, 1357, static_cast<http::ContentTypes>(2) },
5657
#endif /* !defined (CONFIG_HTTP_HTML_NO_DMX) && (defined(OUTPUT_DMX_SEND) || defined(OUTPUT_DMX_SEND_MULTI)) */
5758
#if !defined (CONFIG_HTTP_HTML_NO_RTC) && !defined (DISABLE_RTC)
58-
{ "rtc.html", rtc_html, 1013, static_cast<http::ContentTypes>(0) },
59+
{ 4194541735,"rtc.html", rtc_html, 1013, static_cast<http::ContentTypes>(0) },
5960
#endif /* !defined (CONFIG_HTTP_HTML_NO_RTC) && !defined (DISABLE_RTC) */
60-
{ "static.js", static_js, 1252, static_cast<http::ContentTypes>(2) },
61-
{ "styles.css", styles_css, 409, static_cast<http::ContentTypes>(1) },
62-
{ "index.js", index_js, 1168, static_cast<http::ContentTypes>(2) },
63-
{ "date.js", date_js, 716, static_cast<http::ContentTypes>(2) },
61+
{ 2932864356,"static.js", static_js, 1252, static_cast<http::ContentTypes>(2) },
62+
{ 2557875310,"styles.css", styles_css, 409, static_cast<http::ContentTypes>(1) },
63+
{ 247271700,"index.js", index_js, 1168, static_cast<http::ContentTypes>(2) },
64+
{ 1602327546,"date.js", date_js, 716, static_cast<http::ContentTypes>(2) },
6465
#if defined (NODE_SHOWFILE)
65-
{ "showfile.html", showfile_html, 1386, static_cast<http::ContentTypes>(0) },
66+
{ 92830953,"showfile.html", showfile_html, 1386, static_cast<http::ContentTypes>(0) },
6667
#endif /* (NODE_SHOWFILE) */
67-
{ "default.js", default_js, 261, static_cast<http::ContentTypes>(2) },
68+
{ 135667591,"default.js", default_js, 261, static_cast<http::ContentTypes>(2) },
6869
#if !defined (CONFIG_HTTP_HTML_NO_RTC) && !defined (DISABLE_RTC)
69-
{ "rtc.js", rtc_js, 843, static_cast<http::ContentTypes>(2) },
70+
{ 2872131065,"rtc.js", rtc_js, 843, static_cast<http::ContentTypes>(2) },
7071
#endif /* !defined (CONFIG_HTTP_HTML_NO_RTC) && !defined (DISABLE_RTC) */
7172
#if !defined (CONFIG_HTTP_HTML_NO_RDM) && defined (RDM_CONTROLLER)
72-
{ "rdm.js", rdm_js, 1001, static_cast<http::ContentTypes>(2) },
73+
{ 3336539475,"rdm.js", rdm_js, 1001, static_cast<http::ContentTypes>(2) },
7374
#endif /* !defined (CONFIG_HTTP_HTML_NO_RDM) && defined (RDM_CONTROLLER) */
7475
#if !defined (CONFIG_HTTP_HTML_NO_TIME)
75-
{ "time.js", time_js, 402, static_cast<http::ContentTypes>(2) },
76+
{ 1797555997,"time.js", time_js, 402, static_cast<http::ContentTypes>(2) },
7677
#endif /* !defined (CONFIG_HTTP_HTML_NO_TIME) */
7778
#if !defined (CONFIG_HTTP_HTML_NO_PIXEL) && (defined(OUTPUT_DMX_PIXEL) || defined(OUTPUT_DMX_PIXEL_MULTI))
78-
{ "pixeltype.json", pixeltype_json, 1665, static_cast<http::ContentTypes>(3) },
79+
{ 2632765249,"pixeltype.json", pixeltype_json, 1665, static_cast<http::ContentTypes>(3) },
7980
#endif /* !defined (CONFIG_HTTP_HTML_NO_PIXEL) && (defined(OUTPUT_DMX_PIXEL) || defined(OUTPUT_DMX_PIXEL_MULTI)) */
8081
#if !defined (CONFIG_HTTP_HTML_NO_RDM) && defined (RDM_CONTROLLER)
81-
{ "rdm.html", rdm_html, 1142, static_cast<http::ContentTypes>(0) },
82+
{ 3794898249,"rdm.html", rdm_html, 1142, static_cast<http::ContentTypes>(0) },
8283
#endif /* !defined (CONFIG_HTTP_HTML_NO_RDM) && defined (RDM_CONTROLLER) */
83-
{ "index.html", index_html, 669, static_cast<http::ContentTypes>(0) },
84+
{ 4024653090,"index.html", index_html, 669, static_cast<http::ContentTypes>(0) },
8485
#if defined (NODE_SHOWFILE)
85-
{ "showfile.js", showfile_js, 1425, static_cast<http::ContentTypes>(2) },
86+
{ 4266521075,"showfile.js", showfile_js, 1425, static_cast<http::ContentTypes>(2) },
8687
#endif /* (NODE_SHOWFILE) */
8788
#if !defined (CONFIG_HTTP_HTML_NO_TIME)
88-
{ "time.html", time_html, 599, static_cast<http::ContentTypes>(0) },
89+
{ 3879424355,"time.html", time_html, 599, static_cast<http::ContentTypes>(0) },
8990
#endif /* !defined (CONFIG_HTTP_HTML_NO_TIME) */
9091
#if !defined (CONFIG_HTTP_HTML_NO_DMX) && (defined(OUTPUT_DMX_SEND) || defined(OUTPUT_DMX_SEND_MULTI))
91-
{ "dmx.html", dmx_html, 538, static_cast<http::ContentTypes>(0) },
92+
{ 1521353325,"dmx.html", dmx_html, 538, static_cast<http::ContentTypes>(0) },
9293
#endif /* !defined (CONFIG_HTTP_HTML_NO_DMX) && (defined(OUTPUT_DMX_SEND) || defined(OUTPUT_DMX_SEND_MULTI)) */
9394
};
9495

lib-remoteconfig/http/content/generate_content.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <cassert>
3333

3434
#include "http/http.h"
35+
#include "common/utils/utils_hash.h"
3536

3637
struct SupportedExtension
3738
{
@@ -50,6 +51,7 @@ static constexpr SupportedExtension kSupportedExtensions[] =
5051
static constexpr char kContentHeader[] =
5152
"\n"
5253
"struct FilesContent {\n"
54+
"\tuint32_t hash;\n"
5355
"\tconst char *file_name;\n"
5456
"\tconst char *content;\n"
5557
"\tuint32_t content_length;\n"
@@ -162,7 +164,7 @@ static void MakeConstantName(const char* file_name, char* constant_name, size_t
162164

163165
strncpy(constant_name, file_name, length);
164166
constant_name[length - 1] = '\0';
165-
167+
166168
auto* p = strchr(constant_name, '.');
167169

168170
if (p != nullptr)
@@ -344,7 +346,9 @@ int main() // NOLINT
344346
WriteFeatureGuardsBegin(file_content, dir_entry->d_name);
345347

346348
char buffer[256];
347-
auto i = snprintf(buffer, sizeof(buffer), "\t{ \"%s\", ", dir_entry->d_name);
349+
auto i = snprintf(buffer, sizeof(buffer), "\t{ %u,\"%s\", ",
350+
Fnv1a32Runtime(dir_entry->d_name, strlen(dir_entry->d_name)),
351+
dir_entry->d_name);
348352
assert(i > 0);
349353
assert(i < static_cast<int>(sizeof(buffer)));
350354

lib-remoteconfig/src/httpd/get_file_content.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,18 @@ const char* GetFileContent(const char* file_name, uint32_t& size, http::ContentT
149149
}
150150
#else
151151
#include "../http/content/content.h"
152+
#include "common/utils/utils_hash.h"
152153

153154
const char* GetFileContent(const char* file_name, uint32_t& size, http::ContentTypes& content_type)
154155
{
155156
DEBUG_ENTRY();
156157
DEBUG_PUTS(file_name);
158+
159+
const auto kHash = Fnv1a32Runtime(file_name, strlen(file_name));
157160

158161
for (auto& content : kHttpContent)
159162
{
160-
if (strcmp(file_name, content.file_name) == 0)
163+
if (kHash == content.hash)
161164
{
162165
size = content.content_length;
163166
content_type = content.content_type;

0 commit comments

Comments
 (0)