]> git.refcnt.org Git - colorize.git/commitdiff
Optimize merging for partial lines further
authorSteven Schubiger <stsc@refcnt.org>
Fri, 15 Jan 2016 14:40:32 +0000 (15:40 +0100)
committerSteven Schubiger <stsc@refcnt.org>
Fri, 15 Jan 2016 14:40:32 +0000 (15:40 +0100)
Completing partial lines (i.e., to ones without truncated escape sequences)
can be handled more efficiently by concatenating only the partial line
beginning at the last escape control character with the characters read
from the input stream.  Compared to previous code which concatenated the
*entire* partial line with the characters read, pass now the resulting
string of aforementioned concatenation separately to the escape sequence
cleaning function.

Even though it may appear as a minor optimisation, it is a /substantial/
improvement in terms of memory being allocated when cleaning lines.

colorize.c

index fc23d8bd720411c92383f64039235a6a6b308f8e..0ab2ad9926a8a5f4eac3552011340b9057b8047e 100644 (file)
@@ -214,7 +214,7 @@ static void process_file_arg (const char *, const char **, FILE **);
 static void skip_path_colors (const char *, const char *, const struct stat *);
 static void gather_color_names (const char *, bool *, struct color_name **);
 static void read_print_stream (bool, const struct color **, const char *, FILE *);
-static void merge_print_line (bool, const struct color **, const char *, const char *, FILE *);
+static void merge_print_line (const char *, const char *, FILE *);
 static void complete_part_line (const char *, char **, FILE *);
 static bool get_next_char (char *, const char **, FILE *, bool *);
 static void save_char (char, char **, size_t *, size_t *);
@@ -771,7 +771,7 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL
           {
             char *p;
             if ((clean || clean_all) && (p = strrchr (line, '\033')))
-              merge_print_line (bold, colors, line, p, stream);
+              merge_print_line (line, p, stream);
             else
               print_line (bold, colors, line, 0);
           }
@@ -779,27 +779,33 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL
 }
 
 static void
-merge_print_line (bool bold, const struct color **colors, const char *line, const char *p, FILE *stream)
+merge_print_line (const char *line, const char *p, FILE *stream)
 {
     char *buf = NULL;
-    char *merged_part_line = NULL;
-    const char *part_line;
+    char *merged_esc = NULL;
+    const char *esc = "";
+    const char char_restore = *p;
 
     complete_part_line (p + 1, &buf, stream);
 
     if (buf)
-      part_line = merged_part_line = str_concat (line, buf);
-    else
-      part_line = line;
-    free (buf);
+      {
+        /* form escape sequence */
+        esc = merged_esc = str_concat (p, buf);
+        /* shorten partial line accordingly */
+        *(char *)p = '\0';
+        free (buf);
+      }
 
 #ifdef TEST_MERGE_PART_LINE
-    printf ("%s", part_line);
+    printf ("%s%s", line, esc);
     fflush (stdout);
     _exit (EXIT_SUCCESS);
 #else
-    print_line (bold, colors, part_line, 0);
-    free (merged_part_line);
+    print_clean (line);
+    *(char *)p = char_restore;
+    print_clean (esc);
+    free (merged_esc);
 #endif
 }