]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blobdiff - src/coloredstderr.c
Use static list of descriptors to reduce mallocs.
[coloredstderr/coloredstderr.git] / src / coloredstderr.c
index ccefdf34a31c9da821a58e868008a303e5310c14..008da266bee76fbbc51efc7280edfe5dcf41fa47 100644 (file)
@@ -66,7 +66,10 @@ static int check_handle_fd(int fd) {
     if (!initialized) {
         init_from_environment();
     }
-    if (tracked_fds_list_count == 0) {
+
+    /* tracked_fds_find() is most likely faster than calling isatty(),
+     * therefore check if we are tracking this file descriptor first. */
+    if (!tracked_fds_find(fd)) {
         return 0;
     }
 
@@ -76,7 +79,7 @@ static int check_handle_fd(int fd) {
         return 0;
     }
 
-    return tracked_fds_find(fd);
+    return 1;
 }
 
 static void dup_fd(int oldfd, int newfd) {
@@ -87,9 +90,6 @@ static void dup_fd(int oldfd, int newfd) {
     if (!initialized) {
         init_from_environment();
     }
-    if (tracked_fds_list_count == 0) {
-        return;
-    }
 
     /* We are already tracking this file descriptor, add newfd to the list as
      * it will reference the same descriptor. */
@@ -112,9 +112,6 @@ static void close_fd(int fd) {
     if (!initialized) {
         init_from_environment();
     }
-    if (tracked_fds_list_count == 0) {
-        return;
-    }
 
     tracked_fds_remove(fd);
 }