Module: Mesa
Branch: main
Commit: 0a68a94a513716884c4a8c46c543ffc8bcf8e9c7
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=0a68a94a513716884c4a8c46c543ffc8bcf8e9c7

Author: Tatsuyuki Ishi <ishitatsuy...@gmail.com>
Date:   Sun Jan 14 01:25:17 2024 +0900

util: Optimize mesa_hex_to_bytes

This function ends up getting called an awful lot when loading a large
Fossilize cache db, so let's replace strtol with something more
reasonable.

For a game with 97k shaders, this reduces instance creation time from
203ms to 52ms.

Reviewed-by: Marek Olšák <marek.ol...@amd.com>
Reviewed-by: Georg Lehmann <dadschoo...@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/27052>

---

 src/util/hex.h | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/util/hex.h b/src/util/hex.h
index 5bd618046e1..20a26a0e18a 100644
--- a/src/util/hex.h
+++ b/src/util/hex.h
@@ -23,8 +23,6 @@
 #ifndef UTIL_HEX_H
 #define UTIL_HEX_H
 
-#include <stdlib.h>
-
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -47,18 +45,24 @@ static inline char *mesa_bytes_to_hex(char *buf, const 
unsigned char *binary,
   return buf;
 }
 
+static inline int _mesa_hex_to_int(unsigned char c)
+{
+  return c - (c >= 'a' ? 'a' - 10 : '0');
+}
+
 /*
  * Read `len` pairs of hexadecimal digits from `hex` and write the values to
  * `binary` as `len` bytes.
+ * Hexadecimals must be lower case.
  */
 static inline void mesa_hex_to_bytes(unsigned char *buf, const char *hex,
-                                     unsigned len) {
+                                     unsigned len)
+{
   for (unsigned i = 0; i < len; i++) {
-    char tmp[3];
-    tmp[0] = hex[i * 2];
-    tmp[1] = hex[(i * 2) + 1];
-    tmp[2] = '\0';
-    buf[i] = strtol(tmp, NULL, 16);
+    int hi = _mesa_hex_to_int(hex[i * 2]);
+    int lo = _mesa_hex_to_int(hex[i * 2 + 1]);
+
+    buf[i] = (hi << 4) | lo;
   }
 }
 
@@ -66,4 +70,4 @@ static inline void mesa_hex_to_bytes(unsigned char *buf, 
const char *hex,
 }
 #endif
 
-#endif
\ No newline at end of file
+#endif

Reply via email to