aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Steven Schubiger <stsc@refcnt.org> 2016-01-15 15:40:32 +0100
committerGravatar Steven Schubiger <stsc@refcnt.org> 2016-01-15 15:40:32 +0100
commit025bf75f863750d29f7c7b523c2ad92f5bb62832 (patch)
tree5259d7c2181f63c49d64f8a02efb5ca1a330f6bb
parentdbbbe0626872bd88a0262a8545b81a1b5eeceaa4 (diff)
downloadcolorize-025bf75f863750d29f7c7b523c2ad92f5bb62832.tar.gz
colorize-025bf75f863750d29f7c7b523c2ad92f5bb62832.tar.bz2
Optimize merging for partial lines further
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.
-rw-r--r--colorize.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/colorize.c b/colorize.c
index fc23d8b..0ab2ad9 100644
--- a/colorize.c
+++ b/colorize.c
@@ -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
}