]> git.ipfire.org Git - pakfire.git/commitdiff
strings: Use the branch prediction macros in potentially hot code
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Jan 2025 11:01:50 +0000 (11:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Jan 2025 11:01:50 +0000 (11:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/string.c

index 3061da75e2cd13d4ec43057491111febd85d7744..83bd6c22cde6f0067f3c3e2f6a59b60057526cb1 100644 (file)
@@ -46,11 +46,11 @@ int __pakfire_string_vformat(char* s, const size_t length, const char* format, v
        const ssize_t required = vsnprintf(s, length, format, args);
 
        // Catch any errors
-       if (required < 0)
+       if (unlikely(required < 0))
                return -errno;
 
        // Check if the entire string could be written
-       if ((size_t)required >= length)
+       if (unlikely((size_t)required >= length))
                return -ENOBUFS;
 
        // Success
@@ -59,7 +59,7 @@ int __pakfire_string_vformat(char* s, const size_t length, const char* format, v
 
 int __pakfire_string_set(char* s, const size_t length, const char* value) {
        // If value is NULL or an empty, we will overwrite the buffer with just zeros
-       if (!value) {
+       if (unlikely(!value)) {
                *s = '\0';
                return 0;
        }
@@ -81,11 +81,11 @@ int __pakfire_string_set(char* s, const size_t length, const char* value) {
 
 int __pakfire_string_setn(char* s, const size_t length, const char* value, const size_t l) {
        // Fail if we don't have enough space
-       if (l >= length)
+       if (unlikely(l >= length))
                return -ENOBUFS;
 
        // Copy the data
-       else if (l > 0)
+       if (likely(l > 0))
                memcpy(s, value, l);
 
        // Terminate the result buffer