[FFmpeg-devel] [PATCH 2/4] avcodec/xpm: Minor speed increase for mod_strcspn() {string, reject}==0

Jose Da Silva digital at joescat.com
Tue Feb 23 06:32:14 EET 2021


Test string==0 once before looping. Avoid testing inside the loop.
Test reject==0 once before looping. Avoid testing inside the loop.

Signed-off-by: Jose Da Silva <digital at joescat.com>
---
 libavcodec/xpmdec.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/libavcodec/xpmdec.c b/libavcodec/xpmdec.c
index 5aab46c52d..92a568041c 100644
--- a/libavcodec/xpmdec.c
+++ b/libavcodec/xpmdec.c
@@ -213,22 +213,24 @@ static size_t mod_strcspn(const char *string, const char *reject)
 {
     int i, j;

-    for (i = 0; string && string[i]; i++) {
+    if (string == 0)
+        return 0;
+    for (i = 0; string[i]; i++) {
         if (string[i] == '/' && string[i+1] == '*') {
             i += 2;
-            while ( string && string[i] && (string[i] != '*' || string[i+1] != '/') )
+            while (string[i] && (string[i] != '*' || string[i+1] != '/'))
                 i++;
             i++;
         } else if (string[i] == '/' && string[i+1] == '/') {
             i += 2;
-            while ( string && string[i] && string[i] != '\n' )
+            while (string[i] && string[i] != '\n')
                 i++;
-        } else {
-            for (j = 0; reject && reject[j]; j++) {
+        } else if (reject) {
+            for (j = 0; reject[j]; j++) {
                 if (string[i] == reject[j])
                     break;
             }
-            if (reject && reject[j])
+            if (reject[j])
                 break;
         }
     }
--
2.30.1



More information about the ffmpeg-devel mailing list