summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Hervey <edward@centricular.com>2022-12-01 08:16:45 +0100
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>2022-12-02 06:02:48 +0000
commit9f9e6257769f8c1f6a9c0abd9790bc53eba46193 (patch)
treeb7e6f57a789a2f23b3a1faa77cd4d598aa604da9
parent4b96294f19e5f6f6423e1a08119b443d7fa6ab13 (diff)
downloadgstreamer-9f9e6257769f8c1f6a9c0abd9790bc53eba46193.tar.gz
gstinfo: Optimize color escape code creation
When coloring is in use, those escape codes are going to be created many times for almost all debug lines. Don't create plenty of temporary allocations, and instead build the escape code ourselves statically Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3498>
-rw-r--r--subprojects/gstreamer/gst/gstinfo.c58
1 files changed, 36 insertions, 22 deletions
diff --git a/subprojects/gstreamer/gst/gstinfo.c b/subprojects/gstreamer/gst/gstinfo.c
index 3d81015465..b46bc15d04 100644
--- a/subprojects/gstreamer/gst/gstinfo.c
+++ b/subprojects/gstreamer/gst/gstinfo.c
@@ -1044,6 +1044,37 @@ gst_info_printf_pointer_extension_func (const char *format, void *ptr)
return s;
}
+/* Allocation-less generator of color escape code. Provide a 20 byte write
+ * area */
+static void
+_construct_term_color (guint colorinfo, gchar write_area[20])
+{
+ guint offset;
+
+ memcpy (write_area, "\033[00", 4);
+ offset = 4;
+
+ if (colorinfo & GST_DEBUG_BOLD) {
+ memcpy (write_area + offset, ";01", 3);
+ offset += 3;
+ }
+ if (colorinfo & GST_DEBUG_UNDERLINE) {
+ memcpy (write_area + offset, ";04", 3);
+ offset += 3;
+ }
+ if (colorinfo & GST_DEBUG_FG_MASK) {
+ memcpy (write_area + offset, ";3", 2);
+ write_area[offset + 2] = '0' + (colorinfo & GST_DEBUG_FG_MASK);
+ offset += 3;
+ }
+ if (colorinfo & GST_DEBUG_BG_MASK) {
+ memcpy (write_area + offset, ";4", 2);
+ write_area[offset + 2] = '0' + ((colorinfo & GST_DEBUG_BG_MASK) >> 4);
+ offset += 3;
+ }
+ strncpy (write_area + offset, "m", 2);
+}
+
/**
* gst_debug_construct_term_color:
* @colorinfo: the color info
@@ -1058,26 +1089,11 @@ gst_info_printf_pointer_extension_func (const char *format, void *ptr)
gchar *
gst_debug_construct_term_color (guint colorinfo)
{
- GString *color;
-
- color = g_string_new ("\033[00");
+ gchar tmp_color[20];
- if (colorinfo & GST_DEBUG_BOLD) {
- g_string_append_len (color, ";01", 3);
- }
- if (colorinfo & GST_DEBUG_UNDERLINE) {
- g_string_append_len (color, ";04", 3);
- }
- if (colorinfo & GST_DEBUG_FG_MASK) {
- g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
- }
- if (colorinfo & GST_DEBUG_BG_MASK) {
- g_string_append_printf (color, ";4%1d",
- (colorinfo & GST_DEBUG_BG_MASK) >> 4);
- }
- g_string_append_c (color, 'm');
+ _construct_term_color (colorinfo, tmp_color);
- return g_string_free (color, FALSE);
+ return g_strdup (tmp_color);
}
/**
@@ -1374,13 +1390,12 @@ gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
#endif
/* colors, non-windows */
- gchar *color = NULL;
+ gchar color[20];
const gchar *clear;
gchar pidcolor[10];
const gchar *levelcolor;
- color = gst_debug_construct_term_color (gst_debug_category_get_color
- (category));
+ _construct_term_color (gst_debug_category_get_color (category), color);
clear = "\033[00m";
g_sprintf (pidcolor, "\033[%02dm", pid % 6 + 31);
levelcolor = levelcolormap[level];
@@ -1393,7 +1408,6 @@ gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
clear, message_str);
FFLUSH_DEBUG (log_file);
#undef PRINT_FMT
- g_free (color);
#ifdef G_OS_WIN32
} else {
/* colors, windows. */