diff mbox series

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

Message ID 202102222032.20012.digital@joescat.com
State Superseded
Headers show
Series [FFmpeg-devel,1/4] avcodec/xpm: Minor speed increase to function hex_char_to_number()
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Jose Da Silva Feb. 23, 2021, 4:32 a.m. UTC
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@joescat.com>
---
 libavcodec/xpmdec.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

--
2.30.1
diff mbox series

Patch

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)