[FFmpeg-devel] [PATCH 4/4] avcodec/xpm: Minor speed increase for mod_strcspn() use string pointer

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


Incrementing pointer *ps once is faster than computing string[i] for each
time we need it.

Bug fix: For the remote possibility of a crazy-long comment section that
overflows int, this fix can also return a value larger than sizeof(int).

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

diff --git a/libavcodec/xpmdec.c b/libavcodec/xpmdec.c
index be5277e637..9a50bb6a71 100644
--- a/libavcodec/xpmdec.c
+++ b/libavcodec/xpmdec.c
@@ -211,28 +211,27 @@ static unsigned int hex_char_to_number(uint8_t x)
  */
 static size_t mod_strcspn(const char *string, const char *reject)
 {
-    const char *pr;
-    int i;
+    const char *ps, *pr;

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

 static uint32_t color_string_to_rgba(const char *p, int len)
--
2.30.1



More information about the ffmpeg-devel mailing list