Skip to content

Commit 5240113

Browse files
authored
Optimize content type detection using C-strings
Optimize _setContentTypeFromPath method with a more efficient version (_setContentTypeFromPath_v3) that uses C-string comparison (strcmp) instead of String::endsWith. This change improves performance and reduces memory usage, Common web extensions are checked first to speed up typical lookups. Version 1 (original): 16.9789 μs per call Version 2 (strcmp): 6.7527 μs per call
1 parent 7d1afa3 commit 5240113

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

src/WebResponses.cpp

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,17 @@ size_t AsyncAbstractResponse::_fillBufferAndProcessTemplates(uint8_t *data, size
619619
* File Response
620620
* */
621621

622+
623+
/**
624+
* @brief Sets the content type based on the file path extension
625+
*
626+
* This method determines the appropriate MIME content type for a file based on its
627+
* file extension. It supports both external content type functions (if available)
628+
* and an internal mapping of common file extensions to their corresponding MIME types.
629+
*
630+
* @param path The file path string from which to extract the extension
631+
* @note The method modifies the internal _contentType member variable
632+
*/
622633
void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
623634
#if HAVE_EXTERN_GET_Content_Type_FUNCTION
624635
#ifndef ESP8266
@@ -628,48 +639,55 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
628639
#endif
629640
_contentType = getContentType(path);
630641
#else
631-
if (path.endsWith(T__html)) {
632-
_contentType = T_text_html;
633-
} else if (path.endsWith(T__htm)) {
642+
const char *cpath = path.c_str();
643+
const char *dot = strrchr(cpath, '.');
644+
645+
if (!dot) {
646+
_contentType = T_text_plain;
647+
return;
648+
}
649+
650+
if (strcmp(dot, T__html) == 0 || strcmp(dot, T__htm) == 0) {
634651
_contentType = T_text_html;
635-
} else if (path.endsWith(T__css)) {
652+
} else if (strcmp(dot, T__css) == 0) {
636653
_contentType = T_text_css;
637-
} else if (path.endsWith(T__json)) {
638-
_contentType = T_application_json;
639-
} else if (path.endsWith(T__js)) {
654+
} else if (strcmp(dot, T__js) == 0) {
640655
_contentType = T_application_javascript;
641-
} else if (path.endsWith(T__png)) {
656+
} else if (strcmp(dot, T__json) == 0) {
657+
_contentType = T_application_json;
658+
} else if (strcmp(dot, T__png) == 0) {
642659
_contentType = T_image_png;
643-
} else if (path.endsWith(T__gif)) {
644-
_contentType = T_image_gif;
645-
} else if (path.endsWith(T__jpg)) {
646-
_contentType = T_image_jpeg;
647-
} else if (path.endsWith(T__ico)) {
660+
} else if (strcmp(dot, T__ico) == 0) {
648661
_contentType = T_image_x_icon;
649-
} else if (path.endsWith(T__svg)) {
662+
} else if (strcmp(dot, T__svg) == 0) {
650663
_contentType = T_image_svg_xml;
651-
} else if (path.endsWith(T__eot)) {
652-
_contentType = T_font_eot;
653-
} else if (path.endsWith(T__woff)) {
654-
_contentType = T_font_woff;
655-
} else if (path.endsWith(T__woff2)) {
664+
} else if (strcmp(dot, T__jpg) == 0) {
665+
_contentType = T_image_jpeg;
666+
} else if (strcmp(dot, T__gif) == 0) {
667+
_contentType = T_image_gif;
668+
} else if (strcmp(dot, T__woff2) == 0) {
656669
_contentType = T_font_woff2;
657-
} else if (path.endsWith(T__ttf)) {
670+
} else if (strcmp(dot, T__woff) == 0) {
671+
_contentType = T_font_woff;
672+
} else if (strcmp(dot, T__ttf) == 0) {
658673
_contentType = T_font_ttf;
659-
} else if (path.endsWith(T__xml)) {
674+
} else if (strcmp(dot, T__eot) == 0) {
675+
_contentType = T_font_eot;
676+
} else if (strcmp(dot, T__xml) == 0) {
660677
_contentType = T_text_xml;
661-
} else if (path.endsWith(T__pdf)) {
678+
} else if (strcmp(dot, T__pdf) == 0) {
662679
_contentType = T_application_pdf;
663-
} else if (path.endsWith(T__zip)) {
680+
} else if (strcmp(dot, T__zip) == 0) {
664681
_contentType = T_application_zip;
665-
} else if (path.endsWith(T__gz)) {
682+
} else if (strcmp(dot, T__gz) == 0) {
666683
_contentType = T_application_x_gzip;
667684
} else {
668685
_contentType = T_text_plain;
669686
}
670687
#endif
671688
}
672689

690+
673691
/**
674692
* @brief Constructor for AsyncFileResponse that handles file serving with compression support
675693
*

0 commit comments

Comments
 (0)