From 09dcb18381b2b837cfe28a629907a120e66f5fe2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 14 Nov 2018 16:42:27 +0100 Subject: [PATCH] shared: use NMStrBuf in _nm_utils_enum_to_str_full() Just for showcase and to hit the code from the unit-tests that we have. Also, just to show, the following runs about 25 % faster than before, which isn't bad for such a simple replacement. { GType gtype = nm_test_general_color_flags_get_type (); const int N_RUN = 1000000; int i_run; guint8 c = 0; for (i_run = 0; i_run < N_RUN; i_run++) { gs_free char *str = NULL; str = _nm_utils_enum_to_str_full (gtype, i_run % 10, ",", NULL); c += str[0]; } return c % 3; } $ perf stat -r 200 -B libnm-core/tests/test-general Before: Performance counter stats for 'libnm-core/tests/test-general' (200 runs): 204.48 msec task-clock:u # 0.997 CPUs utilized ( +- 0.53% ) 0 context-switches:u # 0.000 K/sec 0 cpu-migrations:u # 0.000 K/sec 267 page-faults:u # 0.001 M/sec ( +- 0.05% ) 702,987,494 cycles:u # 3.438 GHz ( +- 0.54% ) 1,698,874,415 instructions:u # 2.42 insn per cycle ( +- 0.00% ) 410,394,229 branches:u # 2006.970 M/sec ( +- 0.00% ) 1,770,484 branch-misses:u # 0.43% of all branches ( +- 0.40% ) 0.20502 +- 0.00108 seconds time elapsed ( +- 0.53% ) After: Performance counter stats for 'libnm-core/tests/test-general' (200 runs): 155.71 msec task-clock:u # 0.996 CPUs utilized ( +- 0.50% ) 0 context-switches:u # 0.000 K/sec 0 cpu-migrations:u # 0.000 K/sec 266 page-faults:u # 0.002 M/sec ( +- 0.05% ) 539,994,118 cycles:u # 3.468 GHz ( +- 0.49% ) 1,116,016,733 instructions:u # 2.07 insn per cycle ( +- 0.00% ) 283,974,158 branches:u # 1823.760 M/sec ( +- 0.00% ) 1,377,786 branch-misses:u # 0.49% of all branches ( +- 0.43% ) 0.156255 +- 0.000786 seconds time elapsed ( +- 0.50% ) --- shared/nm-glib-aux/nm-enum-utils.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/shared/nm-glib-aux/nm-enum-utils.c b/shared/nm-glib-aux/nm-enum-utils.c index e92797fdd1..854eda6e5a 100644 --- a/shared/nm-glib-aux/nm-enum-utils.c +++ b/shared/nm-glib-aux/nm-enum-utils.c @@ -6,6 +6,7 @@ #include "nm-default.h" #include "nm-enum-utils.h" +#include "nm-str-buf.h" /*****************************************************************************/ @@ -142,12 +143,14 @@ _nm_utils_enum_to_str_full (GType type, else return g_strdup (enum_value->value_nick); } else if (G_IS_FLAGS_CLASS (klass)) { - GFlagsValue *flags_value; - GString *str = g_string_sized_new (16); unsigned uvalue = (unsigned) value; + GFlagsValue *flags_value; + NMStrBuf strbuf; flags_separator = flags_separator ?: " "; + nm_str_buf_init (&strbuf, 16, FALSE); + for ( ; value_infos && value_infos->nick; value_infos++) { nm_assert (_enum_is_valid_flags_nick (value_infos->nick)); @@ -160,9 +163,9 @@ _nm_utils_enum_to_str_full (GType type, continue; } - if (str->len) - g_string_append (str, flags_separator); - g_string_append (str, value_infos->nick); + if (strbuf.len) + nm_str_buf_append (&strbuf, flags_separator); + nm_str_buf_append (&strbuf, value_infos->nick); uvalue &= ~((unsigned) value_infos->value); if (uvalue == 0) { /* we printed all flags. Done. */ @@ -172,20 +175,20 @@ _nm_utils_enum_to_str_full (GType type, do { flags_value = g_flags_get_first_value (G_FLAGS_CLASS (klass), uvalue); - if (str->len) - g_string_append (str, flags_separator); + if (strbuf.len) + nm_str_buf_append (&strbuf, flags_separator); if ( !flags_value || !_enum_is_valid_flags_nick (flags_value->value_nick)) { if (uvalue) - g_string_append_printf (str, "0x%x", uvalue); + nm_str_buf_append_printf (&strbuf, "0x%x", uvalue); break; } - g_string_append (str, flags_value->value_nick); + nm_str_buf_append (&strbuf, flags_value->value_nick); uvalue &= ~flags_value->value; } while (uvalue); flags_done: - return g_string_free (str, FALSE); + return nm_str_buf_finalize (&strbuf, NULL); } g_return_val_if_reached (NULL);